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

Last change on this file was 12513, checked in by ehuelsmann, 15 years ago

Remove 'private' keyword to eliminate the Java requirement

for the compiler to generate synthetic accessors: functions that
don't appear in the source but do appear in the class file.

Patch by: Douglas Miles <dmiles _at_ users.sf.net>

  • 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 12513 2010-03-02 22:35:36Z 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 static org.armedbear.lisp.Lisp.*;
37
38import java.io.File;
39import java.util.Map;
40import java.util.HashMap;
41
42public class RuntimeClass
43{
44    static Map<String,RuntimeClass> classes = new HashMap<String,RuntimeClass>();
45
46    private Map<String,Function> methods = new HashMap<String,Function>();
47
48    // ### %jnew-runtime-class
49    // %jnew-runtime-class class-name &rest method-names-and-defs
50    private static final Primitive _JNEW_RUNTIME_CLASS =
51        new Primitive("%jnew-runtime-class", PACKAGE_JAVA, false, "class-name &rest method-names-and-defs")
52    {
53        @Override
54        public LispObject execute(LispObject[] args)
55        {
56            int length = args.length;
57            if (length < 3 || length % 2 != 1)
58                return error(new WrongNumberOfArgumentsException(this));
59        RuntimeClass rc = new RuntimeClass();
60        String className = args[0].getStringValue();
61            for (int i = 1; i < length; i = i+2) {
62                String methodName = args[i].getStringValue();
63                rc.addLispMethod(methodName, (Function)args[i+1]);
64        }
65            classes.put(className, rc);
66        return T;
67        }
68    };
69
70    // ### jredefine-method
71    // %jredefine-method class-name method-name method-def
72    private static final Primitive _JREDEFINE_METHOD =
73        new Primitive("%jredefine-method", PACKAGE_JAVA, false,
74                      "class-name method-name method-def")
75    {
76        @Override
77        public LispObject execute(LispObject className, LispObject methodName,
78                                  LispObject methodDef)
79
80        {
81
82      String cn = className.getStringValue();
83      String mn = methodName.getStringValue();
84      Function def = (Function) methodDef;
85      RuntimeClass rc = null;
86      if (classes.containsKey(cn)) {
87                rc = (RuntimeClass) classes.get(cn);
88                rc.addLispMethod(mn, def);
89                return T;
90      }
91      else {
92                error(new LispError("undefined Java class: " + cn));
93                return NIL;
94      }
95        }
96    };
97
98    // ### %load-java-class-from-byte-array
99    private static final Primitive _LOAD_JAVA_CLASS_FROM_BYTE_ARRAY =
100        new Primitive("%load-java-class-from-byte-array", PACKAGE_JAVA, false,
101                      "classname bytearray")
102    {
103        @Override
104        public LispObject execute(LispObject className, LispObject classBytes)
105
106        {
107            String cn = className.getStringValue();
108        String pn = cn.substring(0,cn.lastIndexOf('.'));
109        byte[] cb = (byte[]) classBytes.javaInstance();
110            try {
111                JavaClassLoader loader = JavaClassLoader.getPersistentInstance(pn);
112                Class c = loader.loadClassFromByteArray(cn, cb);
113                if (c != null) {
114                    return T;
115                }
116            }
117            catch (VerifyError e) {
118                return error(new LispError("class verification failed: " +
119                                            e.getMessage()));
120            }
121            catch (LinkageError e) {
122                return error(new LispError("class could not be linked: " +
123                                            e.getMessage()));
124            }
125            return error(
126                new LispError("unable to load ".concat(cn)));
127        }
128    };
129
130    public static final LispObject evalC(LispObject function,
131                                         LispObject args,
132                                         Environment env,
133                                         LispThread thread)
134
135    {
136        return evalCall(function, args, env, thread);
137    }
138
139    public static RuntimeClass getRuntimeClass(String className) {
140        return (RuntimeClass) classes.get(className);
141    }
142
143    public Function getLispMethod(String methodName) {
144        return (Function) methods.get(methodName);
145    }
146
147    void addLispMethod(String methodName, Function def) {
148        methods.put(methodName, def);
149    }
150
151    public static final LispObject makeLispObject(Object obj)
152    {
153        return new JavaObject(obj);
154    }
155
156    public static final Fixnum makeLispObject(byte i)
157    {
158        return Fixnum.getInstance(i);
159    }
160
161    public static final Fixnum makeLispObject(short i)
162    {
163        return Fixnum.getInstance(i);
164    }
165
166    public static final Fixnum makeLispObject(int i)
167    {
168        return Fixnum.getInstance(i);
169    }
170
171    public static final LispInteger makeLispObject(long i)
172    {
173        return Bignum.getInstance(i);
174    }
175
176    public static final SingleFloat makeLispObject(float i)
177    {
178        return new SingleFloat(i);
179    }
180
181    public static final DoubleFloat makeLispObject(double i)
182    {
183        return new DoubleFloat(i);
184    }
185
186    public static final LispCharacter makeLispObject(char i)
187    {
188        return LispCharacter.getInstance(i);
189    }
190
191    public static final LispObject makeLispObject(boolean i)
192    {
193        return i ? T : NIL;
194    }
195}
Note: See TracBrowser for help on using the repository browser.