View Javadoc

1   // Reflection.java, created Mon Feb  5 23:23:21 2001 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.Runtime;
5   
6   import java.util.Set;
7   import java.lang.reflect.Constructor;
8   import java.lang.reflect.Field;
9   import java.lang.reflect.InvocationTargetException;
10  import java.lang.reflect.Member;
11  import java.lang.reflect.Method;
12  import joeq.Class.PrimordialClassLoader;
13  import joeq.Class.jq_Class;
14  import joeq.Class.jq_Field;
15  import joeq.Class.jq_Initializer;
16  import joeq.Class.jq_InstanceField;
17  import joeq.Class.jq_InstanceMethod;
18  import joeq.Class.jq_Member;
19  import joeq.Class.jq_Method;
20  import joeq.Class.jq_Primitive;
21  import joeq.Class.jq_Reference;
22  import joeq.Class.jq_StaticField;
23  import joeq.Class.jq_StaticMethod;
24  import joeq.Class.jq_Type;
25  import joeq.Main.jq;
26  import joeq.Memory.Address;
27  import joeq.UTF.Utf8;
28  
29  /***
30   * @author  John Whaley <jwhaley@alum.mit.edu>
31   * @version $Id: Reflection.java 2465 2006-06-07 23:03:17Z joewhaley $
32   */
33  public abstract class Reflection {
34  
35      public static ObjectTraverser obj_trav;
36      public static boolean USE_DECLARED_FIELDS_CACHE = true;
37  
38      public static final jq_Reference getTypeOf(Object o) {
39          return _delegate.getTypeOf(o);
40      }
41      public static final jq_Type getJQType(Class c) {
42          return _delegate.getJQType(c);
43      }
44      public static final Class getJDKType(jq_Type c) {
45          return _delegate.getJDKType(c);
46      }
47      public static final Class getJDKType(jq_Primitive c) {
48          return _delegate.getJDKType(c);
49      }
50      public static Class getJDKType(jq_Reference c) {
51          return _delegate.getJDKType(c);
52      }
53      public static final jq_Field getJQMember(Field f) {
54          return _delegate.getJQMember(f);
55      }
56      public static final jq_Method getJQMember(Method f) {
57          return _delegate.getJQMember(f);
58      }
59      public static final jq_Initializer getJQMember(Constructor f) {
60          return _delegate.getJQMember(f);
61      }
62      public static final Field getJDKField(Class c, String name) {
63          return _delegate.getJDKField(c, name);
64      }
65      public static final Method getJDKMethod(Class c, String name, Class[] args) {
66          return _delegate.getJDKMethod(c, name, args);
67      }
68      public static final Constructor getJDKConstructor(Class c, Class[] args) {
69          return _delegate.getJDKConstructor(c, args);
70      }
71      public static final Member getJDKMember(jq_Member m) {
72          return _delegate.getJDKMember(m);
73      }
74  
75      /***
76       * Utility function to extract the argument types from a method descriptor.
77       * 
78       * @param desc
79       * @return array of argument types
80       */
81      public static Class[] getArgTypesFromDesc(Utf8 desc) {
82          Utf8.MethodDescriptorIterator i = desc.getParamDescriptors();
83          // count them up
84          int num = 0;
85          while (i.hasNext()) { i.nextUtf8(); ++num; }
86          // get them for real
87          Class[] param_types = new Class[num];
88          i = desc.getParamDescriptors();
89          for (int j=0; j<num; ++j) {
90              Utf8 pd = i.nextUtf8();
91              jq_Type t = PrimordialClassLoader.loader.getOrCreateBSType(pd);
92              param_types[j] = getJDKType(t);
93          }
94          //Utf8 rd = i.getReturnDescriptor();
95          return param_types;
96      }
97      
98      /* Reflective invocations.  Unreachable unless running native. */
99      public static void invokestatic_V(jq_StaticMethod m) throws Throwable {
100         _delegate.invokestatic_V(m);
101     }
102     public static int invokestatic_I(jq_StaticMethod m) throws Throwable {
103         return _delegate.invokestatic_I(m);
104     }
105     public static Object invokestatic_A(jq_StaticMethod m) throws Throwable {
106         return _delegate.invokestatic_A(m);
107     }
108     public static long invokestatic_J(jq_StaticMethod m) throws Throwable {
109         return _delegate.invokestatic_J(m);
110     }
111     public static void invokestatic_V(jq_StaticMethod m, Object arg1) throws Throwable {
112         _delegate.invokestatic_V(m, arg1);
113     }
114     public static void invokeinstance_V(jq_InstanceMethod m, Object dis) throws Throwable {
115         _delegate.invokeinstance_V(m, dis);
116     }
117     public static Object invokeinstance_A(jq_InstanceMethod m, Object dis) throws Throwable {
118         return _delegate.invokeinstance_A(m, dis);
119     }
120     public static void invokeinstance_V(jq_InstanceMethod m, Object dis, Object arg1) throws Throwable {
121         _delegate.invokeinstance_V(m, dis, arg1);
122     }
123     public static Object invokeinstance_A(jq_InstanceMethod m, Object dis, Object arg1) throws Throwable {
124         return _delegate.invokeinstance_A(m, dis, arg1);
125     }
126     public static boolean invokeinstance_Z(jq_InstanceMethod m, Object dis, Object arg1) throws Throwable {
127         return _delegate.invokeinstance_Z(m, dis, arg1);
128     }
129     public static void invokeinstance_V(jq_InstanceMethod m, Object dis, Object arg1, Object arg2) throws Throwable {
130         _delegate.invokeinstance_V(m, dis, arg1, arg2);
131     }
132     public static void invokeinstance_V(jq_InstanceMethod m, Object dis, Object arg1, Object arg2, Object arg3) throws Throwable {
133         _delegate.invokeinstance_V(m, dis, arg1, arg2, arg3);
134     }
135     public static void invokeinstance_V(jq_InstanceMethod m, Object dis, Object arg1, Object arg2, Object arg3, long arg4) throws Throwable {
136         _delegate.invokeinstance_V(m, dis, arg1, arg2, arg3, arg4);
137     }
138     public static void invokeinstance_V(jq_InstanceMethod m, Object dis, Object arg1, int arg2, long arg3, int arg4) throws Throwable {
139         _delegate.invokeinstance_V(m, dis, arg1, arg2, arg3, arg4);
140     }
141     public static long invoke(jq_Method m, Object dis, Object[] args)
142         throws IllegalArgumentException, InvocationTargetException
143     {
144         return _delegate.invoke(m, dis, args);
145     }
146     public static Address invokeA(jq_Method m, Object dis, Object[] args) 
147         throws IllegalArgumentException, InvocationTargetException
148     {
149         return _delegate.invokeA(m, dis, args);
150     }
151 
152 
153     public static int getfield_I(Object o, jq_InstanceField f) {
154         return _delegate.getfield_I(o, f);
155     }
156     public static long getfield_L(Object o, jq_InstanceField f) {
157         return _delegate.getfield_L(o, f);
158     }
159     public static float getfield_F(Object o, jq_InstanceField f) {
160         return _delegate.getfield_F(o, f);
161     }
162     public static double getfield_D(Object o, jq_InstanceField f) {
163         return _delegate.getfield_D(o, f);
164     }
165     public static Object getfield_A(Object o, jq_InstanceField f) {
166         return _delegate.getfield_A(o, f);
167     }
168     public static Address getfield_P(Object o, jq_InstanceField f) {
169         return _delegate.getfield_P(o, f);
170     }
171     public static byte getfield_B(Object o, jq_InstanceField f) {
172         return _delegate.getfield_B(o, f);
173     }
174     public static char getfield_C(Object o, jq_InstanceField f) {
175         return _delegate.getfield_C(o, f);
176     }
177     public static short getfield_S(Object o, jq_InstanceField f) {
178         return _delegate.getfield_S(o, f);
179     }
180     public static boolean getfield_Z(Object o, jq_InstanceField f) {
181         return _delegate.getfield_Z(o, f);
182     }
183     public static Object getfield(Object o, jq_InstanceField f) {
184         return _delegate.getfield(o, f);
185     }
186     public static void putfield_I(Object o, jq_InstanceField f, int v) {
187         _delegate.putfield_I(o, f, v);
188     }
189     public static void putfield_L(Object o, jq_InstanceField f, long v) {
190         _delegate.putfield_L(o, f, v);
191     }
192     public static void putfield_F(Object o, jq_InstanceField f, float v) {
193         _delegate.putfield_F(o, f, v);
194     }
195     public static void putfield_D(Object o, jq_InstanceField f, double v) {
196         _delegate.putfield_D(o, f, v);
197     }
198     public static void putfield_A(Object o, jq_InstanceField f, Object v) {
199         _delegate.putfield_A(o, f, v);
200     }
201     public static void putfield_P(Object o, jq_InstanceField f, Address v) {
202         _delegate.putfield_P(o, f, v);
203     }
204     public static void putfield_B(Object o, jq_InstanceField f, byte v) {
205         _delegate.putfield_B(o, f, v);
206     }
207     public static void putfield_C(Object o, jq_InstanceField f, char v) {
208         _delegate.putfield_C(o, f, v);
209     }
210     public static void putfield_S(Object o, jq_InstanceField f, short v) {
211         _delegate.putfield_S(o, f, v);
212     }
213     public static void putfield_Z(Object o, jq_InstanceField f, boolean v) {
214         _delegate.putfield_Z(o, f, v);
215     }
216     public static int getstatic_I(jq_StaticField f) {
217         return _delegate.getstatic_I(f);
218     }
219     public static long getstatic_L(jq_StaticField f) {
220         return _delegate.getstatic_L(f);
221     }
222     public static float getstatic_F(jq_StaticField f) {
223         return _delegate.getstatic_F(f);
224     }
225     public static double getstatic_D(jq_StaticField f) {
226         return _delegate.getstatic_D(f);
227     }
228     public static Object getstatic_A(jq_StaticField f) {
229         return _delegate.getstatic_A(f);
230     }
231     public static Address getstatic_P(jq_StaticField f) {
232         return _delegate.getstatic_P(f);
233     }
234     public static boolean getstatic_Z(jq_StaticField f) {
235         return _delegate.getstatic_Z(f);
236     }
237     public static byte getstatic_B(jq_StaticField f) {
238         return _delegate.getstatic_B(f);
239     }
240     public static short getstatic_S(jq_StaticField f) {
241         return _delegate.getstatic_S(f);
242     }
243     public static char getstatic_C(jq_StaticField f) {
244         return _delegate.getstatic_C(f);
245     }
246     public static void putstatic_I(jq_StaticField f, int v) {
247         _delegate.putstatic_I(f, v);
248     }
249     public static void putstatic_L(jq_StaticField f, long v) {
250         _delegate.putstatic_L(f, v);
251     }
252     public static void putstatic_F(jq_StaticField f, float v) {
253         _delegate.putstatic_F(f, v);
254     }
255     public static void putstatic_D(jq_StaticField f, double v) {
256         _delegate.putstatic_D(f, v);
257     }
258     public static void putstatic_A(jq_StaticField f, Object v) {
259         _delegate.putstatic_A(f, v);
260     }
261     public static void putstatic_P(jq_StaticField f, Address v) {
262         _delegate.putstatic_P(f, v);
263     }
264     public static void putstatic_Z(jq_StaticField f, boolean v) {
265         _delegate.putstatic_Z(f, v);
266     }
267     public static void putstatic_B(jq_StaticField f, byte v) {
268         _delegate.putstatic_B(f, v);
269     }
270     public static void putstatic_S(jq_StaticField f, short v) {
271         _delegate.putstatic_S(f, v);
272     }
273     public static void putstatic_C(jq_StaticField f, char v) {
274         _delegate.putstatic_C(f, v);
275     }
276     public static int arraylength(Object o) {
277         return _delegate.arraylength(o);
278     }
279     public static Object arrayload_A(Object[] o, int i) {
280         return _delegate.arrayload_A(o, i);
281     }
282     public static Address arrayload_R(Address[] o, int i) {
283         return _delegate.arrayload_R(o, i);
284     }
285 
286     public static void registerNullStaticFields (Set s) {
287         _delegate.registerNullStaticFields(s);
288     }
289 
290     public static jq_Class _class; 
291     public static jq_StaticField _obj_trav; 
292 
293     static interface Delegate {
294         jq_Reference getTypeOf(Object o);
295         jq_Type getJQType(Class c);
296         Class getJDKType(jq_Type c);
297         Class getJDKType(jq_Primitive c);
298         Class getJDKType(jq_Reference c);
299         jq_Field getJQMember(Field f);
300         jq_Method getJQMember(Method f);
301         jq_Initializer getJQMember(Constructor f);
302         Field getJDKField(Class c, String name);
303         Method getJDKMethod(Class c, String name, Class[] args);
304         Constructor getJDKConstructor(Class c, Class[] args);
305         Member getJDKMember(jq_Member m);
306         void invokestatic_V(jq_StaticMethod m) throws Throwable;
307         int invokestatic_I(jq_StaticMethod m) throws Throwable;
308         Object invokestatic_A(jq_StaticMethod m) throws Throwable;
309         long invokestatic_J(jq_StaticMethod m) throws Throwable;
310         void invokestatic_V(jq_StaticMethod m, Object arg1) throws Throwable;
311         void invokeinstance_V(jq_InstanceMethod m, Object dis) throws Throwable;
312         Object invokeinstance_A(jq_InstanceMethod m, Object dis) throws Throwable;
313         void invokeinstance_V(jq_InstanceMethod m, Object dis, Object arg1) throws Throwable;
314         Object invokeinstance_A(jq_InstanceMethod m, Object dis, Object arg1) throws Throwable;
315         boolean invokeinstance_Z(jq_InstanceMethod m, Object dis, Object arg1) throws Throwable;
316         void invokeinstance_V(jq_InstanceMethod m, Object dis, Object arg1, Object arg2) throws Throwable;
317         void invokeinstance_V(jq_InstanceMethod m, Object dis, Object arg1, Object arg2, Object arg3) throws Throwable;
318         void invokeinstance_V(jq_InstanceMethod m, Object dis, Object arg1, Object arg2, Object arg3, long arg4) throws Throwable;
319         void invokeinstance_V(jq_InstanceMethod m, Object dis, Object arg1, int arg2, long arg3, int arg4) throws Throwable;
320         long invoke(jq_Method m, Object dis, Object[] args) throws IllegalArgumentException, InvocationTargetException;
321         Address invokeA(jq_Method m, Object dis, Object[] args) throws IllegalArgumentException, InvocationTargetException;
322         int getfield_I(Object o, jq_InstanceField f);
323         long getfield_L(Object o, jq_InstanceField f);
324         float getfield_F(Object o, jq_InstanceField f);
325         double getfield_D(Object o, jq_InstanceField f);
326         Object getfield_A(Object o, jq_InstanceField f);
327         Address getfield_P(Object o, jq_InstanceField f);
328         byte getfield_B(Object o, jq_InstanceField f);
329         char getfield_C(Object o, jq_InstanceField f);
330         short getfield_S(Object o, jq_InstanceField f);
331         boolean getfield_Z(Object o, jq_InstanceField f);
332         Object getfield(Object o, jq_InstanceField f);
333         void putfield_I(Object o, jq_InstanceField f, int v);
334         void putfield_L(Object o, jq_InstanceField f, long v);
335         void putfield_F(Object o, jq_InstanceField f, float v);
336         void putfield_D(Object o, jq_InstanceField f, double v);
337         void putfield_A(Object o, jq_InstanceField f, Object v);
338         void putfield_P(Object o, jq_InstanceField f, Address v);
339         void putfield_B(Object o, jq_InstanceField f, byte v);
340         void putfield_C(Object o, jq_InstanceField f, char v);
341         void putfield_S(Object o, jq_InstanceField f, short v);
342         void putfield_Z(Object o, jq_InstanceField f, boolean v);
343         int getstatic_I(jq_StaticField f);
344         long getstatic_L(jq_StaticField f);
345         float getstatic_F(jq_StaticField f);
346         double getstatic_D(jq_StaticField f);
347         Object getstatic_A(jq_StaticField f);
348         Address getstatic_P(jq_StaticField f);
349         boolean getstatic_Z(jq_StaticField f);
350         byte getstatic_B(jq_StaticField f);
351         short getstatic_S(jq_StaticField f);
352         char getstatic_C(jq_StaticField f);
353         void putstatic_I(jq_StaticField f, int v);
354         void putstatic_L(jq_StaticField f, long v);
355         void putstatic_F(jq_StaticField f, float v);
356         void putstatic_D(jq_StaticField f, double v);
357         void putstatic_A(jq_StaticField f, Object v);
358         void putstatic_P(jq_StaticField f, Address v);
359         void putstatic_Z(jq_StaticField f, boolean v);
360         void putstatic_B(jq_StaticField f, byte v);
361         void putstatic_S(jq_StaticField f, short v);
362         void putstatic_C(jq_StaticField f, char v);
363         int arraylength(Object o);
364         Object arrayload_A(Object[] o, int i);
365         Address arrayload_R(Address[] o, int i);
366         void registerNullStaticFields(Set h);
367         void initialize();
368     }
369 
370     private static Delegate _delegate;
371     static {
372         /* Set up delegates. */
373         _delegate = null;
374         boolean nullVM = jq.nullVM;
375         if (!nullVM) {
376             _delegate = attemptDelegate("joeq.Runtime.ReflectionImpl");
377         }
378         if (_delegate == null) {
379             _delegate = new joeq.Runtime.BasicReflectionImpl();
380         }
381 
382         _class = (jq_Class)PrimordialClassLoader.loader.getOrCreateBSType("Ljoeq/Runtime/Reflection;");
383         _obj_trav = _class.getOrCreateStaticField("obj_trav", "Ljoeq/Runtime/ObjectTraverser;");
384         _delegate.initialize();
385     }
386 
387     private static Delegate attemptDelegate(String s) {
388         //String type = "reflection delegate";
389         try {
390             Class c = Class.forName(s);
391             return (Delegate)c.newInstance();
392         } catch (java.lang.ClassNotFoundException x) {
393             //System.err.println("Cannot find "+type+" "+s+": "+x);
394         } catch (java.lang.InstantiationException x) {
395             //System.err.println("Cannot instantiate "+type+" "+s+": "+x);
396         } catch (java.lang.IllegalAccessException x) {
397             //System.err.println("Cannot access "+type+" "+s+": "+x);
398         }
399         return null;
400     }
401 }