KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > RuntimeClass


1 /*
2  * RuntimeClass.java
3  *
4  * Copyright (C) 2004 Peter Graves
5  * $Id: RuntimeClass.java,v 1.7 2004/08/24 18:22:36 asimon Exp $
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
22 package org.armedbear.lisp;
23
24 import java.io.File JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 public class RuntimeClass extends Lisp
29 {
30     private static Map JavaDoc classes = new HashMap JavaDoc();
31
32     private Map JavaDoc methods = new HashMap JavaDoc();
33
34     // ### %jnew-runtime-class
35
// %jnew-runtime-class class-name &rest method-names-and-defs
36
private static final Primitive _JNEW_RUNTIME_CLASS =
37         new Primitive("%jnew-runtime-class", PACKAGE_JAVA, false, "class-name &rest method-names-and-defs")
38     {
39         public LispObject execute(LispObject[] args) throws ConditionThrowable
40         {
41             int length = args.length;
42             if (length < 3 || length % 2 != 1)
43                 return signal(new WrongNumberOfArgumentsException(this));
44         RuntimeClass rc = new RuntimeClass();
45         String JavaDoc className = args[0].getStringValue();
46             for (int i = 1; i < length; i = i+2) {
47                 String JavaDoc methodName = args[i].getStringValue();
48                 rc.addLispMethod(methodName, (Function)args[i+1]);
49         }
50             classes.put(className, rc);
51         return T;
52         }
53     };
54
55     // ### jredefine-method
56
// %jredefine-method class-name method-name method-def
57
private static final Primitive3 _JREDEFINE_METHOD =
58         new Primitive3("%jredefine-method", PACKAGE_JAVA, false, "class-name method-name method-def")
59     {
60         public LispObject execute(LispObject className, LispObject methodName, LispObject methodDef)
61             throws ConditionThrowable
62         {
63
64         String JavaDoc cn = className.getStringValue();
65         String JavaDoc mn = methodName.getStringValue();
66         Function def = (Function) methodDef;
67         RuntimeClass rc = null;
68         if (classes.containsKey(cn)) {
69                 rc = (RuntimeClass) classes.get(cn);
70                 rc.addLispMethod(mn, def);
71                 return T;
72         }
73         else {
74                 signal(new LispError("undefined Java class: " + cn));
75                 return NIL;
76         }
77         }
78     };
79
80
81     // ### %load-java-class-from-byte-array
82
private static final Primitive2 _LOAD_JAVA_CLASS_FROM_BYTE_ARRAY =
83         new Primitive2("%load-java-class-from-byte-array", PACKAGE_JAVA, false, "classname bytearray")
84     {
85         public LispObject execute(LispObject className, LispObject classBytes) throws ConditionThrowable
86         {
87             String JavaDoc cn = className.getStringValue();
88         String JavaDoc pn = cn.substring(0,cn.lastIndexOf('.'));
89         byte[] cb = (byte[]) classBytes.javaInstance();
90             try {
91                 JavaClassLoader loader = JavaClassLoader.getPersistentInstance(pn);
92                 Class JavaDoc c = loader.loadClassFromByteArray(cn, cb);
93                 if (c != null) {
94                     return T;
95                 }
96             }
97             catch (VerifyError JavaDoc e) {
98                 return signal(new LispError("class verification failed: " +
99                                             e.getMessage()));
100             }
101             catch (LinkageError JavaDoc e) {
102                 return signal(new LispError("class could not be linked: " +
103                                             e.getMessage()));
104             }
105             catch (Throwable JavaDoc t) {
106                 Debug.trace(t);
107             }
108             return signal(
109                 new LispError("unable to load ".concat(cn)));
110         }
111     };
112
113     public static RuntimeClass getRuntimeClass(String JavaDoc className) {
114         return (RuntimeClass) classes.get(className);
115     }
116
117     public Function getLispMethod(String JavaDoc methodName) {
118         return (Function) methods.get(methodName);
119     }
120
121     private void addLispMethod(String JavaDoc methodName, Function def) {
122         methods.put(methodName, def);
123     }
124
125     public static final LispObject makeLispObject(Object JavaDoc obj) throws ConditionThrowable
126     {
127         return new JavaObject(obj);
128     }
129
130     public static final Fixnum makeLispObject(byte i) throws ConditionThrowable
131     {
132         return new Fixnum(i);
133     }
134
135     public static final Fixnum makeLispObject(short i) throws ConditionThrowable
136     {
137         return new Fixnum(i);
138     }
139
140     public static final Fixnum makeLispObject(int i) throws ConditionThrowable
141     {
142         return new Fixnum(i);
143     }
144
145     public static final Bignum makeLispObject(long i) throws ConditionThrowable
146     {
147         return new Bignum(i);
148     }
149
150     public static final LispFloat makeLispObject(float i) throws ConditionThrowable
151     {
152         return new LispFloat(i);
153     }
154
155     public static final LispFloat makeLispObject(double i) throws ConditionThrowable
156     {
157         return new LispFloat(i);
158     }
159
160     public static final LispCharacter makeLispObject(char i) throws ConditionThrowable
161     {
162         return LispCharacter.getInstance(i);
163     }
164
165     public static final LispObject makeLispObject(boolean i) throws ConditionThrowable
166     {
167         return i ? T : NIL;
168     }
169 }
170
Popular Tags