Changeset 4523


Ignore:
Timestamp:
10/24/03 00:05:13 (19 years ago)
Author:
piso
Message:

JFIELD patch from András Simon <asimon@…>.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/j/src/org/armedbear/lisp/Java.java

    r4514 r4523  
    33 *
    44 * Copyright (C) 2002-2003 Peter Graves
    5  * $Id: Java.java,v 1.12 2003-10-23 15:01:41 piso Exp $
     5 * $Id: Java.java,v 1.13 2003-10-24 00:05:13 piso Exp $
    66 *
    77 * This program is free software; you can redistribute it and/or
     
    2525import java.lang.reflect.InvocationTargetException;
    2626import java.lang.reflect.Method;
     27import java.lang.reflect.Field;
    2728import java.lang.reflect.Modifier;
    2829
     
    3031{
    3132    // ### jclass
    32     private static final Primitive1 JCLASS =
    33         new Primitive1("jclass", PACKAGE_JAVA) {
     33    private static final Primitive1 JCLASS = new Primitive1("jclass", PACKAGE_JAVA)
     34    {
    3435        public LispObject execute(LispObject arg) throws ConditionThrowable
    3536        {
     
    3940            catch (ClassNotFoundException e) {
    4041                throw new ConditionThrowable(new LispError("class not found: " + arg));
     42            }
     43            catch (Throwable t) {
     44                throw new ConditionThrowable(new LispError(getMessage(t)));
     45            }
     46        }
     47    };
     48
     49    // ### jfield
     50    // jfield class-name field-name &optional instance
     51    private static final Primitive JFIELD = new Primitive("jfield", PACKAGE_JAVA)
     52    {
     53        public LispObject execute(LispObject[] args) throws ConditionThrowable
     54        {
     55            if (args.length < 2 || args.length > 3)
     56                throw new ConditionThrowable(new WrongNumberOfArgumentsException(this));
     57            String className = LispString.getValue(args[0]);
     58      String fieldName = LispString.getValue(args[1]);
     59      Object instance = null;
     60            try {
     61                final Class c = Class.forName(className);
     62    final Field f = c.getField(fieldName);
     63    if (args.length == 3) {
     64      if (args[2] instanceof LispString)
     65                    instance = LispString.getValue(args[2]);
     66      else
     67                    instance = JavaObject.getObject(args[2]);
     68                }
     69    return makeLispObject(f.get(instance));
     70            }
     71            catch (ClassNotFoundException e) {
     72                throw new ConditionThrowable(new LispError("class not found: " + className));
     73            }
     74            catch (NoSuchFieldException e) {
     75                throw new ConditionThrowable(new LispError("no such field"));
     76            }
     77            catch (SecurityException e) {
     78                throw new ConditionThrowable(new LispError("inaccessible field"));
     79            }
     80            catch (IllegalAccessException e) {
     81                throw new ConditionThrowable(new LispError("illegal access"));
     82            }
     83            catch (IllegalArgumentException e) {
     84                throw new ConditionThrowable(new LispError("illegal argument"));
    4185            }
    4286            catch (Throwable t) {
     
    4993    // jconstructor class-name &rest parameter-class-names
    5094    private static final Primitive JCONSTRUCTOR =
    51         new Primitive("jconstructor", PACKAGE_JAVA) {
     95        new Primitive("jconstructor", PACKAGE_JAVA)
     96    {
    5297        public LispObject execute(LispObject[] args) throws ConditionThrowable
    5398        {
     
    78123    // ### jmethod
    79124    // jmethod class-ref name &rest parameter-class-names
    80     private static final Primitive JMETHOD =
    81         new Primitive("jmethod", PACKAGE_JAVA) {
     125    private static final Primitive JMETHOD = new Primitive("jmethod", PACKAGE_JAVA)
     126    {
    82127        public LispObject execute(LispObject[] args) throws ConditionThrowable
    83128        {
     
    129174    // ### jstatic
    130175    // jstatic method class &rest args
    131     private static final Primitive JSTATIC =
    132         new Primitive("jstatic", PACKAGE_JAVA) {
     176    private static final Primitive JSTATIC = new Primitive("jstatic", PACKAGE_JAVA)
     177    {
    133178        public LispObject execute(LispObject[] args) throws ConditionThrowable
    134179        {
     
    208253                    else if (arg instanceof LispFloat)
    209254                        initargs[i-1] = new Double(((LispFloat)arg).getValue());
     255                    else if (arg instanceof JavaObject)
     256                        initargs[i-1] = ((JavaObject)arg).getObject();
    210257                }
    211258                return new JavaObject(constructor.newInstance(initargs));
     
    326373    static {
    327374        export("JCLASS", PACKAGE_JAVA);
     375        export("JFIELD", PACKAGE_JAVA);
    328376        export("JCONSTRUCTOR", PACKAGE_JAVA);
    329377        export("JMETHOD", PACKAGE_JAVA);
Note: See TracChangeset for help on using the changeset viewer.