001: /*
002: * PublicMemberAccessor.java
003: *
004: * Copyright (c) 1997-2007 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.security.AccessController;
015: import java.security.PrivilegedAction;
016: import java.security.PrivilegedActionException;
017: import java.security.PrivilegedExceptionAction;
018: import java.util.Map;
019:
020: import pnuts.lang.Configuration;
021: import pnuts.lang.Context;
022: import pnuts.lang.PnutsException;
023: import pnuts.lang.Property;
024: import pnuts.lang.Runtime;
025: import org.pnuts.util.*;
026:
027: public class PublicMemberAccessor extends
028: pnuts.lang.PublicMemberAccessor {
029:
030: private transient Cache fieldCache = new MemoryCache();
031:
032: public PublicMemberAccessor() {
033: }
034:
035: public PublicMemberAccessor(Configuration conf) {
036: super (conf);
037: }
038:
039: /**
040: * Gets a field value of the target object.
041: *
042: * @param context
043: * the context in which the field is read
044: * @param target
045: * the target object
046: * @param name
047: * the field name
048: * @return the field value
049: */
050: public Object getField(Context context, Object target, String name) {
051: if (target instanceof Context) {
052: return ((Context) target).get(name);
053: } else if (target instanceof Property) {
054: return ((Property) target).get(name, context);
055: } else if (target instanceof Map) {
056: return ((Map) target).get(name);
057: } else {
058: return super .getField(context, target, name);
059: }
060: }
061:
062: /**
063: * Sets a field value of the specified object.
064: *
065: * @param context the context in which the field is written.
066: * @param target the target object
067: * @param name the field name
068: * @param expr the field value
069: */
070: public void putField(Context context, Object target, String name,
071: Object expr) {
072: if (target instanceof Context) {
073: ((Context) target).set(name, expr);
074: } else if (target instanceof Property) {
075: ((Property) target).set(name, expr, context);
076: } else if (target instanceof Map) {
077: ((Map) target).put(name, expr);
078: } else {
079: super .putField(context, target, name, expr);
080: }
081: }
082:
083: public Object getStaticField(Context context, Class clazz,
084: String name) {
085: try {
086: return getField(clazz, name).get(null);
087: } catch (PnutsException p) {
088: throw p;
089: } catch (NoSuchFieldException f) {
090: throw new PnutsException("field.notFound", new Object[] {
091: name, clazz }, context);
092: } catch (Throwable t) {
093: throw new PnutsException(t, context);
094: }
095: }
096:
097: public void putStaticField(Context context, Class clazz,
098: String name, Object value) {
099: try {
100: Field field = getField(clazz, name);
101: Class type = field.getType();
102: if (Runtime.isArray(value) && type.isArray()) {
103: if (!type.isInstance(value)) {
104: value = Runtime.transform(type, value, context);
105: }
106: }
107: field.set(null, value);
108: } catch (PnutsException e0) {
109: throw e0;
110: } catch (NoSuchFieldException f) {
111: throw new PnutsException("field.notFound", new Object[] {
112: name, clazz }, context);
113: } catch (Throwable e) {
114: throw new PnutsException(e, context);
115: }
116: }
117:
118: protected Field getField(final Class cls, final String name)
119: throws NoSuchFieldException {
120: try {
121: return (Field) AccessController
122: .doPrivileged(new PrivilegedExceptionAction() {
123: public Object run() throws Exception {
124: return cls.getField(name);
125: }
126: });
127: } catch (PrivilegedActionException e) {
128: throw (NoSuchFieldException) e.getException();
129: }
130: }
131:
132: public Method[] getMethods(final Class cls) {
133: return (Method[]) AccessController
134: .doPrivileged(new PrivilegedAction() {
135: public Object run() {
136: return cls.getMethods();
137: }
138: });
139: }
140:
141: public Constructor[] getConstructors(final Class cls) {
142: return (Constructor[]) AccessController
143: .doPrivileged(new PrivilegedAction() {
144: public Object run() {
145: return cls.getConstructors();
146: }
147: });
148: }
149: }
|