001: /*
002: * @(#)NonPublicMemberAccessor.java 1.2 04/12/06
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.ext;
010:
011: import java.lang.reflect.Constructor;
012: import java.lang.reflect.Field;
013: import java.lang.reflect.Method;
014: import java.util.Vector;
015:
016: import pnuts.lang.Configuration;
017: import pnuts.lang.Context;
018: import pnuts.lang.Runtime;
019: import pnuts.lang.PnutsException;
020:
021: /**
022: * when -a option is given to the pnuts command, this class is used so that
023: * non-public members can be accessed.
024: */
025: public class NonPublicMemberAccessor extends PublicMemberAccessor {
026:
027: public NonPublicMemberAccessor() {
028: }
029:
030: public NonPublicMemberAccessor(Configuration conf) {
031: super (conf);
032: }
033:
034: public Method[] getMethods(Class cls) {
035: Vector vec = new Vector();
036: Class c = cls;
037: while (c != null) {
038: Method[] declared = c.getDeclaredMethods();
039: for (int i = 0; i < declared.length; i++) {
040: Method m = declared[i];
041: m.setAccessible(true);
042: vec.addElement(m);
043: }
044: c = c.getSuperclass();
045: }
046: Method[] ret = new Method[vec.size()];
047: vec.copyInto(ret);
048: return ret;
049: }
050:
051: public Constructor[] getConstructors(Class cls) {
052: Vector vec = new Vector();
053: Class c = cls;
054: while (c != null) {
055: Constructor[] declared = c.getDeclaredConstructors();
056: for (int i = 0; i < declared.length; i++) {
057: Constructor cons = declared[i];
058: cons.setAccessible(true);
059: vec.addElement(cons);
060: }
061: c = c.getSuperclass();
062: }
063: Constructor[] ret = new Constructor[vec.size()];
064: vec.copyInto(ret);
065: return ret;
066: }
067:
068: protected Field getField(final Class cls, final String name)
069: throws NoSuchFieldException {
070: Class c = cls;
071: while (c != null) {
072: Field fields[] = c.getDeclaredFields();
073: for (int i = 0; i < fields.length; i++) {
074: Field f = fields[i];
075: if (f.getName().equals(name)) {
076: f.setAccessible(true);
077: return f;
078: }
079: }
080: c = c.getSuperclass();
081: }
082: throw new NoSuchFieldException(name);
083: }
084:
085: /**
086: * Gets a field value of the target object.
087: *
088: * @param context
089: * the context in which the field is read
090: * @param target
091: * the target object
092: * @param name
093: * the field name
094: * @return the field value
095: */
096: public Object getField(Context context, Object target, String name) {
097: return getObjectField(context, target, name);
098: }
099:
100: /**
101: * Sets a field value of the specified object.
102: *
103: * @param context
104: * the context in which the field is written.
105: * @param target
106: * the target object
107: * @param name
108: * the field name
109: * @param value
110: * the field value
111: */
112: public void putField(Context context, Object target, String name,
113: Object value) {
114: putObjectField(context, target, name, value);
115: }
116:
117: protected Object getObjectField(Context context, Object target,
118: String name) {
119: try {
120: return getField(target.getClass(), name).get(target);
121: } catch (NoSuchFieldException e1) {
122: if (target instanceof Class) {
123: return getStaticField(context, (Class) target, name);
124: }
125: throw new PnutsException("field.notFound", new Object[] {
126: name, target.getClass() }, context);
127: } catch (PnutsException pe) {
128: throw pe;
129: } catch (Exception e) {
130: throw new PnutsException(e, context);
131: }
132: }
133:
134: protected void putObjectField(Context context, Object target,
135: String name, Object value) {
136: try {
137: Field field = getField(target.getClass(), name);
138: Class type = field.getType();
139: if (Runtime.isArray(value) && type.isArray()) {
140: if (!type.isInstance(value)) {
141: value = Runtime.transform(type, value, null);
142: }
143: }
144: field.set(target, value);
145: } catch (NoSuchFieldException e1) {
146: throw new PnutsException("field.notFound", new Object[] {
147: name, target.getClass() }, context);
148: } catch (PnutsException pe) {
149: throw pe;
150: } catch (Exception e) {
151: throw new PnutsException(e, context);
152: }
153: }
154:
155: public Object callMethod(Context context, Class c, String name,
156: Object args[], Class types[], Object target) {
157: return invokeMethod(context, c, name, args, types, target);
158: }
159: }
|