Changeset 11450


Ignore:
Timestamp:
12/15/08 03:31:04 (12 years ago)
Author:
astalla
Message:

JavaBean? property support (jproperty-value)
Minor code cleanup
Started jinterface-impl registration support on the Lisp side

Location:
branches/scripting/j/src/org/armedbear/lisp
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/scripting/j/src/org/armedbear/lisp/JProxy.java

    r11389 r11450  
    125125    //NEW IMPLEMENTATION by Alessio Stalla
    126126 
     127    /**
     128     * A weak map associating each proxy instance with a "Lisp-this" object.
     129     */
    127130    private static final Map<Object, LispObject> proxyMap = new WeakHashMap<Object, LispObject>();
    128131 
  • branches/scripting/j/src/org/armedbear/lisp/Java.java

    r11297 r11450  
    2222package org.armedbear.lisp;
    2323
     24import java.beans.BeanInfo;
     25import java.beans.IntrospectionException;
     26import java.beans.Introspector;
     27import java.beans.PropertyDescriptor;
    2428import java.lang.reflect.Array;
    2529import java.lang.reflect.Constructor;
     
    2832import java.lang.reflect.Method;
    2933import java.lang.reflect.Modifier;
     34import java.util.HashMap;
    3035import java.util.Map;
    31 import java.util.HashMap;
    3236
    3337public final class Java extends Lisp
     
    692696        }
    693697    };
    694 
     698   
     699    private static final Primitive JGET_PROPERTY_VALUE =
     700      new Primitive("%jget-property-value", PACKAGE_JAVA, true,
     701                    "java-object property-name") {
     702     
     703      public LispObject execute(LispObject javaObject, LispObject propertyName) throws ConditionThrowable {
     704      try {
     705        Object obj = javaObject.javaInstance();
     706        PropertyDescriptor pd = getPropertyDescriptor(obj, propertyName);
     707        return new JavaObject(pd.getReadMethod().invoke(obj));
     708      } catch (Exception e) {
     709        ConditionThrowable t = new ConditionThrowable("Exception in accessing property");
     710        t.initCause(e);
     711        throw t;
     712      }
     713        }
     714    };
     715   
     716    private static final Primitive JSET_PROPERTY_VALUE =
     717      new Primitive("%jset-property-value", PACKAGE_JAVA, true,
     718                    "java-object property-name value") {
     719     
     720      public LispObject execute(LispObject javaObject, LispObject propertyName, LispObject value) throws ConditionThrowable {
     721      try {
     722              Object obj = javaObject.javaInstance();
     723        PropertyDescriptor pd = getPropertyDescriptor(obj, propertyName);
     724        pd.getWriteMethod().invoke(obj, value.javaInstance());
     725        return value;
     726      } catch (Exception e) {
     727        ConditionThrowable t = new ConditionThrowable("Exception in accessing property");
     728        t.initCause(e);
     729        throw t;
     730      }
     731        }
     732    };
     733   
     734    private static PropertyDescriptor getPropertyDescriptor(Object obj, LispObject propertyName) throws ConditionThrowable, IntrospectionException {
     735        String prop = ((AbstractString) propertyName).getStringValue();
     736        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
     737        for(PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
     738          if(pd.getName().equals(prop)) {
     739            return pd;
     740          }
     741        }
     742    throw new ConditionThrowable("Property " + prop + " not found in " + obj);
     743    }
     744   
    695745    private static Class classForName(String className) throws ConditionThrowable
    696746    {
  • branches/scripting/j/src/org/armedbear/lisp/autoloads.lisp

    r11379 r11450  
    192192(export 'jmake-proxy "JAVA")
    193193(autoload 'jmake-proxy "java")
     194(export 'jproperty-value "JAVA")
     195(autoload 'jproperty-value "java")
    194196(export 'jobject-class "JAVA")
    195197(autoload 'jobject-class "java")
  • branches/scripting/j/src/org/armedbear/lisp/java.lisp

    r11389 r11450  
    279279      (error "Unknown load-from for ~A" class-name)))))
    280280
     281(defun jproperty-value (obj prop)
     282  (%jget-property-value obj prop))
     283
     284(defun (setf jproperty-value) (value obj prop)
     285  (%jset-property-value obj prop value))
     286
    281287(provide "JAVA-EXTENSIONS")
  • branches/scripting/j/src/org/armedbear/lisp/scripting/AbclScriptEngine.java

    r11393 r11450  
    327327  public ScriptEngineFactory getFactory() {
    328328    return new AbclScriptEngineFactory();
    329   }
    330 
    331   public static String decoratedVariableName(String jvar) {
    332     return jvar.toUpperCase();
    333329  }
    334330
  • branches/scripting/j/src/org/armedbear/lisp/scripting/AbclScriptEngineFactory.java

    r11360 r11450  
    7979    sb.append(method);
    8080    sb.append("\" ");
    81     sb.append(AbclScriptEngine.decoratedVariableName(obj));
     81    sb.append(obj);
    8282    for(String arg : args) {
    8383      sb.append(" ");
    84       sb.append(AbclScriptEngine.decoratedVariableName(arg));
     84      sb.append(arg);
    8585    }
    8686    sb.append(")");
  • branches/scripting/j/src/org/armedbear/lisp/scripting/lisp/abcl-script.lisp

    r11393 r11450  
    8888      (lambda ()
    8989        ,@(read-from-string (concatenate 'string "(" code-string ")")))))))
     90
     91
     92;;Java interface implementation
     93
     94(defvar *interface-implementation-map* (make-hash-table :test #'equal))
     95
     96(defun find-java-interface-implementation (interface)
     97  (gethash interface *interface-implementation-map*))
     98
     99(defun register-java-interface-implementation (interface impl)
     100  (setf (gethash interface *interface-implementation-map*) impl))
     101
     102(defun remove-java-interface-implementation (interface)
     103  (remhash interface *interface-implementation-map*))
     104
     105(defun define-java-interface-implementation (interface implementation &optional lisp-this)
     106  (register-java-interface-implementation
     107   interface
     108   (jmake-proxy interface implementation lisp-this)))
  • branches/scripting/j/src/org/armedbear/lisp/scripting/lisp/packages.lisp

    r11393 r11450  
    2424     #:define-java-interface-implementation
    2525     #:find-java-interface-implementation
    26      #:implement-java-interface))
     26     #:register-java-interface-implementation
     27     #:remove-java-interface-implementation))
    2728 
    2829(defpackage :abcl-script-user
Note: See TracChangeset for help on using the changeset viewer.