KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > reflect > ClassMemberLocation


1 package gnu.kawa.reflect;
2 import gnu.mapping.*;
3 import gnu.bytecode.*;
4 import gnu.mapping.Location; // As opposed to gnu.bytecode.Location.
5
import gnu.expr.*;
6
7 /** A Location whose value is that of a named field/method of an object.
8  * The object is used as the owning Location's value.
9  * (For now, only fields are supported.)
10  */

11
12 public class ClassMemberLocation extends Location
13 {
14   Object JavaDoc instance;
15   ClassType type;
16   /** Member (method or field) name. */
17   String JavaDoc mname;
18   java.lang.reflect.Field JavaDoc rfield;
19
20   public final Object JavaDoc getInstance () { return instance; }
21   public final void setInstance (Object JavaDoc obj) { instance = obj; }
22
23   public ClassMemberLocation(Object JavaDoc instance, ClassType type, String JavaDoc mname)
24   {
25     this.instance = instance;
26     this.type = type;
27     this.mname = mname;
28   }
29
30   public ClassMemberLocation(Object JavaDoc instance, Class JavaDoc clas, String JavaDoc mname)
31   {
32     this.instance = instance;
33     this.type = (ClassType) Type.make(clas);
34     this.mname = mname;
35   }
36
37   public String JavaDoc getMemberName()
38   {
39     return mname;
40   }
41
42   public ClassType getDeclaringClass()
43   {
44     return type;
45   }
46
47   public ClassMemberLocation(Object JavaDoc instance, java.lang.reflect.Field JavaDoc field)
48   {
49     this.instance = instance;
50     this.rfield = field;
51     this.mname = field.getName();
52   }
53
54   void setup ()
55   {
56     if (rfield == null)
57       {
58     Class JavaDoc clas;
59     try
60       {
61         clas = type.getReflectClass();
62       }
63     catch (RuntimeException JavaDoc ex)
64       {
65             RuntimeException JavaDoc uex
66               = new UnboundLocationException(null, "Unbound location - "
67                                              + ex.toString());
68             /* #ifdef use:java.lang.Throwable.getCause */
69             uex.initCause(ex);
70             /* #endif */
71             throw uex;
72       }
73         try
74           {
75             rfield = clas.getField(mname);
76           }
77         catch (java.lang.NoSuchFieldException JavaDoc ex)
78           {
79             RuntimeException JavaDoc uex
80               = new UnboundLocationException(null, "Unbound location "
81                                              + " - no field " + mname
82                                              + " in " + type.getName());
83             /* #ifdef use:java.lang.Throwable.getCause */
84             uex.initCause(ex);
85             /* #endif */
86         throw uex;
87           }
88       }
89   }
90
91   public java.lang.reflect.Field JavaDoc getRField ()
92   {
93     java.lang.reflect.Field JavaDoc rfld = this.rfield;
94     if (rfld == null)
95       {
96     Class JavaDoc clas
97 = null;
98     try
99       {
100         clas = type.getReflectClass();
101             rfld = clas.getField(mname);
102         this.rfield = rfld;
103       }
104     catch (Exception JavaDoc ex)
105       {
106         return null;
107       }
108       }
109     return rfld;
110   }
111
112   public Object JavaDoc get (Object JavaDoc defaultValue)
113   {
114     java.lang.reflect.Field JavaDoc rfld = getRField();
115     if (rfld == null)
116       return defaultValue;
117
118     try
119       {
120         return rfld.get(instance);
121       }
122     catch (IllegalAccessException JavaDoc ex)
123       {
124     throw WrappedException.wrapIfNeeded(ex);
125       }
126   }
127
128   public boolean isConstant ()
129   {
130     java.lang.reflect.Field JavaDoc rfld = getRField();
131     return rfld != null && (rfield.getModifiers() & Access.FINAL) != 0;
132   }
133
134   public boolean isBound ()
135   {
136     java.lang.reflect.Field JavaDoc rfld = getRField();
137     return rfld != null;
138   }
139
140   public void set (Object JavaDoc value)
141   {
142     setup();
143     try
144       {
145         rfield.set(instance, value);
146     return;
147       }
148     catch (IllegalAccessException JavaDoc ex)
149       {
150     throw WrappedException.wrapIfNeeded(ex);
151       }
152     // This is a bit of a kludge FIXME.
153
//setLocation(loc, new TrivialLocation(getEnvironment(loc)));
154
//setValue(loc, value);
155
}
156
157   public static void define (Object JavaDoc instance, java.lang.reflect.Field JavaDoc rfield,
158                  String JavaDoc uri, Language language, Environment env)
159     throws IllegalAccessException JavaDoc
160   {
161     Object JavaDoc fvalue = rfield.get(instance);
162     Type ftype = Type.make(rfield.getType());
163     boolean isAlias = ftype.isSubtype(Compilation.typeLocation);
164     boolean isProcedure = ftype.isSubtype(Compilation.typeProcedure);
165     Object JavaDoc fdname = ((fvalue instanceof Named && ! isAlias)
166              ? ((Named) fvalue).getSymbol()
167              : Compilation.demangleName(rfield.getName(), true));
168     Symbol sym;
169     if (fdname instanceof Symbol)
170       sym = (Symbol) fdname;
171     else
172       {
173     sym = Symbol.make(uri == null ? "" : uri,
174               fdname.toString().intern());
175       }
176     boolean isFinal = (rfield.getModifiers() & Access.FINAL) != 0;
177     Location loc;
178     Object JavaDoc property = null;
179     if (isAlias && isFinal)
180       {
181     loc = (Location) fvalue;
182       }
183     else
184       {
185     if (isFinal)
186       property = language.getEnvPropertyFor(rfield, fvalue);
187     loc = new ClassMemberLocation(instance, rfield);
188       }
189     env.addLocation(sym, property, loc);
190   }
191
192   /** Import all the public fields of an object. */
193   public static void defineAll(Object JavaDoc instance, Environment env)
194     throws IllegalAccessException JavaDoc
195   {
196     Class JavaDoc clas = instance.getClass();
197     Language language = Language.getDefaultLanguage();
198     java.lang.reflect.Field JavaDoc[] fields = clas.getFields();
199     for (int i = fields.length; --i >= 0; )
200       {
201     java.lang.reflect.Field JavaDoc field = fields[i];
202     String JavaDoc fname = field.getName();
203     if (fname.startsWith(Declaration.PRIVATE_PREFIX)
204         || fname.endsWith("$instance"))
205           continue;
206     define(instance, field, null, language, env);
207       }
208   }
209 }
210
Popular Tags