source: branches/streams/abcl/src/org/armedbear/lisp/JavaBeans.java

Last change on this file was 14326, checked in by Mark Evenson, 12 years ago

Refactor java.beans.* out of Java.java.

Enables patches for building on Android to exclude source by
file-level compilation units.

File size: 4.7 KB
Line 
1/*
2 * Java.java
3 *
4 * Copyright (C) 2002-2006 Peter Graves, Andras Simon
5 * $Id$
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.beans.BeanInfo;
39import java.beans.IntrospectionException;
40import java.beans.Introspector;
41import java.beans.PropertyDescriptor;
42
43public final class JavaBeans {
44
45    private static final Primitive JGET_PROPERTY_VALUE = new pf__jget_property_value();
46    @DocString(name="%jget-propety-value", args="java-object property-name",
47    doc="Gets a JavaBeans property on JAVA-OBJECT.\n" +
48        "SYSTEM-INTERNAL: Use jproperty-value instead.")
49    private static final class pf__jget_property_value extends Primitive
50    {
51        pf__jget_property_value() 
52        {
53      super("%jget-property-value", PACKAGE_JAVA, false,
54                  "java-object property-name");
55        }
56     
57        @Override
58        public LispObject execute(LispObject javaObject, LispObject propertyName) {
59      try {
60        Object obj = javaObject.javaInstance();
61        PropertyDescriptor pd = getPropertyDescriptor(obj, propertyName);
62        Object value = pd.getReadMethod().invoke(obj);
63        if(value instanceof LispObject) {
64            return (LispObject) value;
65        } else if(value != null) {
66            return JavaObject.getInstance(value, true);
67        } else {
68            return NIL;
69        }
70      } catch (Exception e) {
71                return error(new JavaException(e));
72      }
73        }
74    };
75   
76    private static final Primitive JSET_PROPERTY_VALUE = new pf__jset_property_value();
77    @DocString(name="%jset-propety-value", args="java-object property-name value",
78    doc="Sets a JavaBean property on JAVA-OBJECT.\n" +
79        "SYSTEM-INTERNAL: Use (setf jproperty-value) instead.")
80    private static final class pf__jset_property_value extends Primitive
81    {
82        pf__jset_property_value()
83        {
84      super("%jset-property-value", PACKAGE_JAVA, false,
85                  "java-object property-name value");
86        }
87     
88        @Override
89        public LispObject execute(LispObject javaObject, LispObject propertyName, LispObject value) {
90      Object obj = null;
91      try {
92    obj = javaObject.javaInstance();
93    PropertyDescriptor pd = getPropertyDescriptor(obj, propertyName);
94    Object jValue;
95    //TODO maybe we should do this in javaInstance(Class)
96    if(value instanceof JavaObject) {
97        jValue = value.javaInstance();
98    } else {
99        if(Boolean.TYPE.equals(pd.getPropertyType()) ||
100           Boolean.class.equals(pd.getPropertyType())) {
101      jValue = value != NIL;
102        } else {
103      jValue = value != NIL ? value.javaInstance() : null;
104        }
105    }
106    pd.getWriteMethod().invoke(obj, jValue);
107    return value;
108      } catch (Exception e) {
109            return error(new JavaException(e));
110      }
111        }
112    };
113
114    static PropertyDescriptor getPropertyDescriptor(Object obj, LispObject propertyName) throws IntrospectionException {
115        String prop = ((AbstractString) propertyName).getStringValue();
116        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
117        for(PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
118          if(pd.getName().equals(prop)) {
119            return pd;
120          }
121        }
122        error(new LispError("Property " + prop + " not found in " + obj));
123
124        return null; // not reached
125    }
126}   
Note: See TracBrowser for help on using the repository browser.