source: branches/0.17.x/abcl/src/org/armedbear/lisp/RuntimeClass.java

Last change on this file was 12254, checked in by ehuelsmann, 16 years ago

Remove 'throws ConditionThrowable?' method annotations:

it's an unchecked exception now, so no need to declare it thrown.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1/*
2 * RuntimeClass.java
3 *
4 * Copyright (C) 2004 Peter Graves
5 * $Id: RuntimeClass.java 12254 2009-11-06 20:07:54Z ehuelsmann $
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 *
21 * As a special exception, the copyright holders of this library give you
22 * permission to link this library with independent modules to produce an
23 * executable, regardless of the license terms of these independent
24 * modules, and to copy and distribute the resulting executable under
25 * terms of your choice, provided that you also meet, for each linked
26 * independent module, the terms and conditions of the license of that
27 * module.  An independent module is a module which is not derived from
28 * or based on this library.  If you modify this library, you may extend
29 * this exception to your version of the library, but you are not
30 * obligated to do so.  If you do not wish to do so, delete this
31 * exception statement from your version.
32 */
33
34package org.armedbear.lisp;
35
36import java.io.File;
37import java.util.Map;
38import java.util.HashMap;
39
40public class RuntimeClass extends Lisp
41{
42    private static Map<String,RuntimeClass> classes = new HashMap<String,RuntimeClass>();
43
44    private Map<String,Function> methods = new HashMap<String,Function>();
45
46    // ### %jnew-runtime-class
47    // %jnew-runtime-class class-name &rest method-names-and-defs
48    private static final Primitive _JNEW_RUNTIME_CLASS =
49        new Primitive("%jnew-runtime-class", PACKAGE_JAVA, false, "class-name &rest method-names-and-defs")
50    {
51        @Override
52        public LispObject execute(LispObject[] args)
53        {
54            int length = args.length;
55            if (length < 3 || length % 2 != 1)
56                return error(new WrongNumberOfArgumentsException(this));
57        RuntimeClass rc = new RuntimeClass();
58        String className = args[0].getStringValue();
59            for (int i = 1; i < length; i = i+2) {
60                String methodName = args[i].getStringValue();
61                rc.addLispMethod(methodName, (Function)args[i+1]);
62        }
63            classes.put(className, rc);
64        return T;
65        }
66    };
67
68    // ### jredefine-method
69    // %jredefine-method class-name method-name method-def
70    private static final Primitive _JREDEFINE_METHOD =
71        new Primitive("%jredefine-method", PACKAGE_JAVA, false,
72                      "class-name method-name method-def")
73    {
74        @Override
75        public LispObject execute(LispObject className, LispObject methodName,
76                                  LispObject methodDef)
77
78        {
79
80      String cn = className.getStringValue();
81      String mn = methodName.getStringValue();
82      Function def = (Function) methodDef;
83      RuntimeClass rc = null;
84      if (classes.containsKey(cn)) {
85                rc = (RuntimeClass) classes.get(cn);
86                rc.addLispMethod(mn, def);
87                return T;
88      }
89      else {
90                error(new LispError("undefined Java class: " + cn));
91                return NIL;
92      }
93        }
94    };
95
96    // ### %load-java-class-from-byte-array
97    private static final Primitive _LOAD_JAVA_CLASS_FROM_BYTE_ARRAY =
98        new Primitive("%load-java-class-from-byte-array", PACKAGE_JAVA, false,
99                      "classname bytearray")
100    {
101        @Override
102        public LispObject execute(LispObject className, LispObject classBytes)
103
104        {
105            String cn = className.getStringValue();
106        String pn = cn.substring(0,cn.lastIndexOf('.'));
107        byte[] cb = (byte[]) classBytes.javaInstance();
108            try {
109                JavaClassLoader loader = JavaClassLoader.getPersistentInstance(pn);
110                Class c = loader.loadClassFromByteArray(cn, cb);
111                if (c != null) {
112                    return T;
113                }
114            }
115            catch (VerifyError e) {
116                return error(new LispError("class verification failed: " +
117                                            e.getMessage()));
118            }
119            catch (LinkageError e) {
120                return error(new LispError("class could not be linked: " +
121                                            e.getMessage()));
122            }
123            catch (Throwable t) {
124                Debug.trace(t);
125            }
126            return error(
127                new LispError("unable to load ".concat(cn)));
128        }
129    };
130
131    public static final LispObject evalC(LispObject function,
132                                         LispObject args,
133                                         Environment env,
134                                         LispThread thread)
135
136    {
137        return evalCall(function, args, env, thread);
138    }
139
140    public static RuntimeClass getRuntimeClass(String className) {
141        return (RuntimeClass) classes.get(className);
142    }
143
144    public Function getLispMethod(String methodName) {
145        return (Function) methods.get(methodName);
146    }
147
148    private void addLispMethod(String methodName, Function def) {
149        methods.put(methodName, def);
150    }
151
152    public static final LispObject makeLispObject(Object obj)
153    {
154        return new JavaObject(obj);
155    }
156
157    public static final Fixnum makeLispObject(byte i)
158    {
159        return Fixnum.getInstance(i);
160    }
161
162    public static final Fixnum makeLispObject(short i)
163    {
164        return Fixnum.getInstance(i);
165    }
166
167    public static final Fixnum makeLispObject(int i)
168    {
169        return Fixnum.getInstance(i);
170    }
171
172    public static final LispInteger makeLispObject(long i)
173    {
174        return Bignum.getInstance(i);
175    }
176
177    public static final SingleFloat makeLispObject(float i)
178    {
179        return new SingleFloat(i);
180    }
181
182    public static final DoubleFloat makeLispObject(double i)
183    {
184        return new DoubleFloat(i);
185    }
186
187    public static final LispCharacter makeLispObject(char i)
188    {
189        return LispCharacter.getInstance(i);
190    }
191
192    public static final LispObject makeLispObject(boolean i)
193    {
194        return i ? T : NIL;
195    }
196}
Note: See TracBrowser for help on using the repository browser.