source: trunk/abcl/src/org/armedbear/lisp/RuntimeClass.java @ 12450

Last change on this file since 12450 was 12298, checked in by ehuelsmann, 15 years ago

Full source scan of "catch (Throwable";

remove lots of instances, or make the catch statement more
specific, e.g. replace Throwable by IOException.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.4 KB
RevLine 
[5391]1/*
2 * RuntimeClass.java
3 *
4 * Copyright (C) 2004 Peter Graves
[11297]5 * $Id: RuntimeClass.java 12298 2009-12-18 21:50:54Z ehuelsmann $
[5391]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.
[11391]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.
[5391]32 */
33
34package org.armedbear.lisp;
35
[12288]36import static org.armedbear.lisp.Lisp.*;
37
[5391]38import java.io.File;
39import java.util.Map;
40import java.util.HashMap;
41
[12290]42public class RuntimeClass
[5451]43{
[11281]44    private static Map<String,RuntimeClass> classes = new HashMap<String,RuntimeClass>();
[5391]45
[11281]46    private Map<String,Function> methods = new HashMap<String,Function>();
[5391]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    {
[11488]53        @Override
[12254]54        public LispObject execute(LispObject[] args)
[5391]55        {
56            int length = args.length;
57            if (length < 3 || length % 2 != 1)
[11158]58                return error(new WrongNumberOfArgumentsException(this));
[11281]59        RuntimeClass rc = new RuntimeClass();
60        String className = args[0].getStringValue();
[5391]61            for (int i = 1; i < length; i = i+2) {
[5892]62                String methodName = args[i].getStringValue();
[5391]63                rc.addLispMethod(methodName, (Function)args[i+1]);
[11281]64        }
[5391]65            classes.put(className, rc);
[11281]66        return T;
[5391]67        }
68    };
69
70    // ### jredefine-method
71    // %jredefine-method class-name method-name method-def
[8080]72    private static final Primitive _JREDEFINE_METHOD =
73        new Primitive("%jredefine-method", PACKAGE_JAVA, false,
74                      "class-name method-name method-def")
[5391]75    {
[11488]76        @Override
[8080]77        public LispObject execute(LispObject className, LispObject methodName,
78                                  LispObject methodDef)
[12254]79
[5391]80        {
81
[5892]82      String cn = className.getStringValue();
83      String mn = methodName.getStringValue();
[5391]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 {
[11158]92                error(new LispError("undefined Java class: " + cn));
[5391]93                return NIL;
94      }
95        }
96    };
97
98    // ### %load-java-class-from-byte-array
[8084]99    private static final Primitive _LOAD_JAVA_CLASS_FROM_BYTE_ARRAY =
100        new Primitive("%load-java-class-from-byte-array", PACKAGE_JAVA, false,
[10908]101                      "classname bytearray")
[5391]102    {
[11488]103        @Override
[8080]104        public LispObject execute(LispObject className, LispObject classBytes)
[12254]105
[5391]106        {
[5892]107            String cn = className.getStringValue();
[11281]108        String pn = cn.substring(0,cn.lastIndexOf('.'));
109        byte[] cb = (byte[]) classBytes.javaInstance();
[5391]110            try {
[7488]111                JavaClassLoader loader = JavaClassLoader.getPersistentInstance(pn);
[5391]112                Class c = loader.loadClassFromByteArray(cn, cb);
113                if (c != null) {
114                    return T;
115                }
116            }
117            catch (VerifyError e) {
[11158]118                return error(new LispError("class verification failed: " +
[5391]119                                            e.getMessage()));
120            }
121            catch (LinkageError e) {
[11158]122                return error(new LispError("class could not be linked: " +
[5391]123                                            e.getMessage()));
124            }
[11158]125            return error(
[5391]126                new LispError("unable to load ".concat(cn)));
127        }
128    };
129
[10908]130    public static final LispObject evalC(LispObject function,
131                                         LispObject args,
132                                         Environment env,
133                                         LispThread thread)
[12254]134
[10908]135    {
136        return evalCall(function, args, env, thread);
137    }
138
[5391]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    }
[5451]146
[5391]147    private void addLispMethod(String methodName, Function def) {
148        methods.put(methodName, def);
149    }
150
[12254]151    public static final LispObject makeLispObject(Object obj)
[5391]152    {
[7488]153        return new JavaObject(obj);
[5391]154    }
155
[12254]156    public static final Fixnum makeLispObject(byte i)
[5391]157    {
[11714]158        return Fixnum.getInstance(i);
[5391]159    }
160
[12254]161    public static final Fixnum makeLispObject(short i)
[5391]162    {
[11714]163        return Fixnum.getInstance(i);
[5391]164    }
165
[12254]166    public static final Fixnum makeLispObject(int i)
[5391]167    {
[11714]168        return Fixnum.getInstance(i);
[5391]169    }
170
[12254]171    public static final LispInteger makeLispObject(long i)
[5391]172    {
[11722]173        return Bignum.getInstance(i);
[5391]174    }
175
[12254]176    public static final SingleFloat makeLispObject(float i)
[5391]177    {
[10908]178        return new SingleFloat(i);
[5391]179    }
180
[12254]181    public static final DoubleFloat makeLispObject(double i)
[5391]182    {
[8784]183        return new DoubleFloat(i);
[5391]184    }
185
[12254]186    public static final LispCharacter makeLispObject(char i)
[5391]187    {
188        return LispCharacter.getInstance(i);
189    }
190
[12254]191    public static final LispObject makeLispObject(boolean i)
[5391]192    {
193        return i ? T : NIL;
194    }
195}
Note: See TracBrowser for help on using the repository browser.