001: /*
002: * @(#)JavaBeansConfiguration.java 1.3 05/05/09
003: *
004: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.lang;
010:
011: import java.lang.reflect.Constructor;
012: import java.lang.reflect.InvocationTargetException;
013: import java.lang.reflect.Method;
014: import java.security.AccessController;
015: import java.security.PrivilegedAction;
016: import org.pnuts.lang.*;
017:
018: /**
019: * This is a configuration for JavaBeans. Only methods in method descriptors can
020: * be called. Field access expression reads/writes property of the Beans.
021: */
022: abstract class JavaBeansConfiguration extends Configuration {
023:
024: private Class stopClass;
025:
026: /**
027: * Constructor
028: */
029: public JavaBeansConfiguration() {
030: }
031:
032: /**
033: * Constructor
034: */
035: public JavaBeansConfiguration(Class stopClass) {
036: this .stopClass = stopClass;
037: }
038:
039: /*
040: * Gets the Introspector's "stopClass"
041: *
042: * @see java.beans.Introspector
043: */
044: protected Class getStopClass() {
045: return stopClass;
046: }
047:
048: /**
049: * Collects the Bean methods for the specified class.
050: */
051: public Method[] getMethods(Class cls) {
052: return ObjectDescFactory.getDefault().create(cls, stopClass)
053: .getMethods();
054: }
055:
056: /**
057: * Get all public constructors of the specified class.
058: *
059: * @param cls the class
060: * @return an array of Constructor objects
061: */
062: public Constructor[] getConstructors(final Class cls) {
063: return (Constructor[]) AccessController
064: .doPrivileged(new PrivilegedAction() {
065: public Object run() {
066: return cls.getConstructors();
067: }
068: });
069: }
070:
071: /**
072: * Gets a Bean property of the specified bean.
073: *
074: * @param context
075: * the context in which the property is read
076: * @param target
077: * the target bean
078: * @param name
079: * the Bean property name
080: */
081: public Object getField(Context context, Object target, String name) {
082: return getBeanProperty(context, target, name);
083: }
084:
085: /**
086: * Gets a Bean property of the specified bean.
087: *
088: * @param context
089: * the context in which the property is read
090: * @param target
091: * the target bean
092: * @param name
093: * the Bean property name
094: */
095: protected Object getBeanProperty(Context context, Object target,
096: String name) {
097: try {
098: return context.runtime.getBeanProperty(target, name,
099: stopClass);
100: } catch (InvocationTargetException e1) {
101: throw new PnutsException(e1.getTargetException(), context);
102: } catch (IllegalAccessException e2) {
103: throw new PnutsException(e2, context);
104: }
105: }
106:
107: /**
108: * Sets a Bean property of the specified bean.
109: *
110: * @param context
111: * the context in which the property is read
112: * @param target
113: * the target bean
114: * @param name
115: * the Bean property name
116: * @param value
117: * the new property value
118: */
119: public void putField(Context context, Object target, String name,
120: Object value) {
121: setBeanProperty(context, target, name, value);
122: }
123:
124: /**
125: * Sets a Bean property of the specified bean.
126: *
127: * @param context
128: * the context in which the property is read
129: * @param target
130: * the target bean
131: * @param name
132: * the Bean property name
133: * @param value
134: * the new property value
135: */
136: protected void setBeanProperty(Context context, Object target,
137: String name, Object value) {
138: try {
139: context.runtime.setBeanProperty(target, name, value,
140: stopClass);
141: } catch (InvocationTargetException e1) {
142: throw new PnutsException(e1.getTargetException(), context);
143: } catch (IllegalAccessException e2) {
144: throw new PnutsException(e2, context);
145: }
146: }
147:
148: /**
149: * Calls a method
150: *
151: * @param context
152: * the contexct
153: * @param c
154: * the class of the method
155: * @param name
156: * the name of the method
157: * @param args
158: * arguments
159: * @param types
160: * type information of each arguments
161: * @param target
162: * the target object of the method call
163: * @return the result of the method call
164: */
165: public Object callMethod(Context context, Class c, String name,
166: Object args[], Class types[], Object target) {
167: try {
168: return context.runtime._callMethod(context, c, name, args,
169: types, target);
170: } catch (PnutsException pe) {
171: PnutsException.TraceInfo trace = new PnutsException.TraceInfo(
172: target, name, args, context.getScriptSource(),
173: context.beginLine, context.beginColumn);
174: pe.backtrace(trace);
175: throw pe;
176: }
177: }
178:
179: /**
180: * Calls a constructor
181: *
182: * @param context
183: * the context
184: * @param c
185: * class of the constructor
186: * @param args
187: * the arguments
188: * @param types
189: * type information of each arguments
190: * @return the result
191: */
192: public Object callConstructor(Context context, Class c,
193: Object[] args, Class[] types) {
194: try {
195: return context.runtime._callConstructor(context, c, args,
196: types);
197: } catch (PnutsException pe) {
198: PnutsException.TraceInfo trace = new PnutsException.TraceInfo(
199: c, args, context.getScriptSource(),
200: context.beginLine, context.beginColumn);
201: pe.backtrace(trace);
202: throw pe;
203: }
204: }
205: }
|