KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > pointer > nativemethods > JavaLangClassNative


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Feng Qian
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /**
21  * Simulates the native method side effects in class java.lang.Class
22  *
23  * @author Feng Qian
24  * @author <XXX>
25  */

26
27 package soot.jimple.toolkits.pointer.nativemethods;
28
29 import soot.*;
30 import soot.jimple.toolkits.pointer.representations.*;
31 import soot.jimple.toolkits.pointer.util.*;
32
33 public class JavaLangClassNative extends NativeMethodClass {
34     public JavaLangClassNative( NativeHelper helper ) { super(helper); }
35
36   /**
37    * Implements the abstract method simulateMethod.
38    * It distributes the request to the corresponding methods
39    * by signatures.
40    */

41   public void simulateMethod(SootMethod method,
42                  ReferenceVariable thisVar,
43                  ReferenceVariable returnVar,
44                  ReferenceVariable params[]){
45
46     String JavaDoc subSignature = method.getSubSignature();
47
48     if (subSignature.equals("java.lang.Class forName0(java.lang.String,boolean,java.lang.ClassLoader)")) {
49       java_lang_Class_forName0(method, thisVar, returnVar, params);
50       return;
51
52     } else if (subSignature.equals("java.lang.Object newInstance0()")) {
53       java_lang_Class_newInstance0(method, thisVar, returnVar, params);
54       return;
55
56     } else if (subSignature.equals("java.lang.String getName()")) {
57       java_lang_Class_getName(method, thisVar, returnVar, params);
58       return;
59
60     } else if (subSignature.equals("java.lang.ClassLoader getClassLoader0()")){
61       java_lang_Class_getClassLoader0(method, thisVar, returnVar, params);
62       return;
63
64     } else if (subSignature.equals("java.lang.Class getSuperclass()")) {
65       java_lang_Class_getSuperclass(method, thisVar, returnVar, params);
66       return;
67
68     } else if (subSignature.equals("java.lang.Class[] getInterfaces()")){
69       java_lang_Class_getInterfaces(method, thisVar, returnVar, params);
70       return;
71
72     } else if (subSignature.equals("java.lang.Class getComponentType()")){
73       java_lang_Class_getComponentType(method, thisVar, returnVar, params);
74       return;
75
76     } else if (subSignature.equals("java.lang.Object[] getSigners()")){
77       java_lang_Class_getSigners(method, thisVar, returnVar, params);
78       return;
79
80     } else if (subSignature.equals("void setSigners(java.lang.Object[])")) {
81       java_lang_Class_setSigners(method, thisVar, returnVar, params);
82       return;
83
84     } else if (subSignature.equals("java.lang.Class getDeclaringClass()")){
85       java_lang_Class_getDeclaringClass(method, thisVar, returnVar, params);
86       return;
87
88     } else if (subSignature.equals("void setProtectionDomain0(java.security.ProtectionDomain)")){
89       java_lang_Class_setProtectionDomain0(method, thisVar, returnVar, params);
90       return;
91
92     } else if (subSignature.equals("java.security.ProtectionDomain getProtectionDomain0()")) {
93       java_lang_Class_getProtectionDomain0(method, thisVar, returnVar, params);
94       return;
95
96     } else if (subSignature.equals("java.lang.Class getPrimitiveClass(java.lang.String)")) {
97       java_lang_Class_getPrimitiveClass(method, thisVar, returnVar, params);
98       return;
99
100     } else if (subSignature.equals("java.lang.reflect.Field[] getFields0(int)")){
101       java_lang_Class_getFields0(method, thisVar, returnVar, params);
102       return;
103
104     } else if (subSignature.equals("java.lang.reflect.Method[] getMethods0(int)")){
105       java_lang_Class_getMethods0(method, thisVar, returnVar, params);
106       return;
107
108     } else if (subSignature.equals("java.lang.reflect.Constructor[] getConstructors0(int)")) {
109       java_lang_Class_getConstructors0(method, thisVar, returnVar, params);
110       return;
111
112     } else if (subSignature.equals("java.lang.reflect.Field getField0(java.lang.String,int)")) {
113       java_lang_Class_getField0(method, thisVar, returnVar, params);
114       return;
115
116     } else if (subSignature.equals("java.lang.reflect.Method getMethod0(java.lang.String,java.lang.Class[],int)")) {
117       java_lang_Class_getMethod0(method, thisVar, returnVar, params);
118       return;
119
120     } else if (subSignature.equals("java.lang.reflect.Constructor getConstructor0(java.lang.Class[],int)")) {
121       java_lang_Class_getConstructor0(method, thisVar, returnVar, params);
122       return;
123    
124     } else if (subSignature.equals("java.lang.Class[] getDeclaredClasses0()")){
125       java_lang_Class_getDeclaredClasses0(method, thisVar, returnVar, params);
126       return;
127
128     } else {
129       defaultMethod(method, thisVar, returnVar, params);
130       return;
131
132     }
133   }
134
135   /****************************** java.lang.Class **********************/
136   /* A quick note for simulating java.lang.Class :
137    *
138    * In theory, the same class may have two or more representations
139    * at the runtime. But statically, we can assume that all variables
140    * of java.lang.Class type are aliased together. By looking at
141    * static class hierarchy, there is only one ReferenceVariable
142    * variable for a class in the hierarchy.
143    */

144
145   /**
146    * NOTE: the semantic of forName0 follows forName method.
147    *
148    * Returns the Class object associated with the class or interface
149    * with the given string name, using the given class loader. Given
150    * the fully qualified name for a class or interface (in the same
151    * format returned by getName) this method attempts to locate,
152    * load, and link the class or interface. The specified class
153    * loader is used to load the class or interface. If the parameter
154    * loader is null, the class is loaded through the bootstrap class
155    * loader. The class is initialized only if the initialize
156    * parameter is true and if it has not been initialized earlier.
157    *
158    * If name denotes a primitive type or void, an attempt will be made
159    * to locate a user-defined class in the unnamed package whose
160    * name is name. Therefore, this method cannot be used to obtain
161    * any of the Class objects representing primitive types or void.
162    *
163    * If name denotes an array class, the component type of the array
164    * class is loaded but not initialized.
165    *
166    * For example, in an instance method the expression:
167    * Class.forName("Foo")
168    * is equivalent to:
169    * Class.forName("Foo", true, this.getClass().getClassLoader())
170    *
171    * private static native java.lang.Class forName0(java.lang.String,
172    * boolean,
173    * java.lang.ClassLoader)
174    * throws java.lang.ClassNotFoundException;
175    */

176   public void java_lang_Class_forName0(SootMethod method,
177                           ReferenceVariable thisVar,
178                           ReferenceVariable returnVar,
179                           ReferenceVariable params[]){
180     helper.assignObjectTo(returnVar, Environment.v().getClassObject());
181   }
182
183   /**
184    * NOTE: creates an object.
185    *
186    * private native java.lang.Object newInstance0()
187    * throws java.lang.InstantiationException,
188    * java.lang.IllegalAccessException
189    */

190   public void java_lang_Class_newInstance0(SootMethod method,
191                           ReferenceVariable thisVar,
192                           ReferenceVariable returnVar,
193                           ReferenceVariable params[]){
194     ReferenceVariable instanceVar = helper.newInstanceOf(thisVar);
195     helper.assign(returnVar, instanceVar);
196   }
197
198   /**
199    * Returns the class name.
200    *
201    * public native java.lang.String getName();
202    */

203   public void java_lang_Class_getName(SootMethod method,
204                          ReferenceVariable thisVar,
205                          ReferenceVariable returnVar,
206                          ReferenceVariable params[]) {
207     helper.assignObjectTo(returnVar, Environment.v().getStringObject());
208   }
209
210   /**
211    * returns the class loader object for this class.
212    *
213    * it is almost impossible to distinguish the dynamic class loader
214    * for classes. a conservative way is to use one static representation
215    * for all class loader, which means all class loader variable aliased
216    * together.
217    *
218    * private native java.lang.ClassLoader getClassLoader0();
219    */

220   public
221     void java_lang_Class_getClassLoader0(SootMethod method,
222                      ReferenceVariable thisVar,
223                      ReferenceVariable returnVar,
224                      ReferenceVariable params[]) {
225     helper.assignObjectTo(returnVar, Environment.v().getClassLoaderObject());
226   }
227
228   /**
229    * returns the super class of this class
230    *
231    * public native java.lang.Class getSuperclass();
232    */

233   public
234     void java_lang_Class_getSuperclass(SootMethod method,
235                        ReferenceVariable thisVar,
236                        ReferenceVariable returnVar,
237                        ReferenceVariable params[]) {
238     helper.assignObjectTo(returnVar, Environment.v().getClassObject());
239   }
240
241   /**
242    * Determines the interfaces implemented by the class or interface
243    * represented by this object.
244    *
245    * public native java.lang.Class getInterfaces()[];
246    */

247   public
248     void java_lang_Class_getInterfaces(SootMethod method,
249                        ReferenceVariable thisVar,
250                        ReferenceVariable returnVar,
251                        ReferenceVariable params[]) {
252     /* currently, we do not distinguish array object and scalar object.*/
253     helper.assignObjectTo(returnVar, Environment.v().getClassObject());
254   }
255
256   /**
257    * Returns the Class representing the component type of an array. If
258    * this class does not represent an array class this method returns
259    * null.
260    *
261    * public native java.lang.Class getComponentType();
262    */

263   public
264     void java_lang_Class_getComponentType(SootMethod method,
265                       ReferenceVariable thisVar,
266                       ReferenceVariable returnVar,
267                       ReferenceVariable params[]) {
268     helper.assignObjectTo(returnVar, Environment.v().getClassObject());
269   }
270
271   /**
272    * Sets the signers of a class. This should be called after defining a
273    * class. Parameters:
274    * c - the Class object
275    * signers - the signers for the class
276    *
277    * native void setSigners(java.lang.Object[]);
278    */

279   public
280     void java_lang_Class_setSigners(SootMethod method,
281                     ReferenceVariable thisVar,
282                     ReferenceVariable returnVar,
283                     ReferenceVariable params[]) {
284     ReferenceVariable tempFld =
285       helper.tempField("<java.lang.Class signers>");
286     helper.assign(tempFld, params[0]);
287   }
288
289   /**
290    * Gets the signers of this class.
291    * We need an artificial field variable to connect setSigners
292    * and getSigners.
293    *
294    * public native java.lang.Object getSigners()[];
295    */

296   public
297     void java_lang_Class_getSigners(SootMethod method,
298                     ReferenceVariable thisVar,
299                     ReferenceVariable returnVar,
300                     ReferenceVariable params[]) {
301     ReferenceVariable tempFld =
302       helper.tempField("<java.lang.Class signers>");
303     helper.assign(returnVar, tempFld);
304   }
305
306   /**
307    * If the class or interface represented by this Class object is a
308    * member of another class, returns the Class object representing the
309    * class in which it was declared. This method returns null if this
310    * class or interface is not a member of any other class. If this
311    * Class object represents an array class, a primitive type, or
312    * void,then this method returns null.
313    *
314    * Returns:
315    * the declaring class for this class
316    *
317    * public native java.lang.Class getDeclaringClass();
318    */

319   public
320     void java_lang_Class_getDeclaringClass(SootMethod method,
321                        ReferenceVariable thisVar,
322                        ReferenceVariable returnVar,
323                        ReferenceVariable params[]) {
324     helper.assignObjectTo(returnVar, Environment.v().getClassObject());
325   }
326
327   /**
328    * Sets or returns the ProtectionDomain of this class,
329    * called by getProtectiondomain.
330    *
331    * We need an artificial field variable to handle this.
332    *
333    * native void setProtectionDomain0(java.security.ProtectionDomain);
334    */

335   public
336     void java_lang_Class_setProtectionDomain0(SootMethod method,
337                           ReferenceVariable thisVar,
338                           ReferenceVariable returnVar,
339                           ReferenceVariable params[]) {
340     ReferenceVariable protdmn =
341       helper.tempField("<java.lang.Class ProtDmn>");
342     helper.assign(protdmn, params[0]);
343   }
344
345   /**
346    * private native java.security.ProtectionDomain getProtectionDomain0();
347    */

348   public
349     void java_lang_Class_getProtectionDomain0(SootMethod method,
350                           ReferenceVariable thisVar,
351                           ReferenceVariable returnVar,
352                           ReferenceVariable params[]) {
353     ReferenceVariable protdmn =
354       helper.tempField("<java.lang.Class ProtDmn>");
355     helper.assign(returnVar, protdmn);
356   }
357
358   /**
359    * Undocumented. It is supposed to return a class object for primitive
360    * type named by @param0.
361    *
362    * static native java.lang.Class getPrimitiveClass(java.lang.String);
363    */

364   public
365     void java_lang_Class_getPrimitiveClass(SootMethod method,
366                        ReferenceVariable thisVar,
367                        ReferenceVariable returnVar,
368                        ReferenceVariable params[]) {
369     helper.assignObjectTo(returnVar, Environment.v().getClassObject());
370   }
371
372   /**
373    * Returns an array containing Field objects reflecting all the
374    * accessible public fields of the class or interface represented by
375    * this Class object.
376    *
377    * private native java.lang.reflect.Field getFields0(int)[];
378    */

379   public
380     void java_lang_Class_getFields0(SootMethod method,
381                     ReferenceVariable thisVar,
382                     ReferenceVariable returnVar,
383                     ReferenceVariable params[]) {
384     helper.assignObjectTo(returnVar, Environment.v().getLeastArrayObject());
385   }
386
387   /**
388    * Returns an array containing Method objects reflecting all the
389    * public member methods of the class or interface represented by
390    * this Class object, including those declared by the class or
391    * interface and and those inherited from superclasses and
392    * superinterfaces.
393    *
394    * private native java.lang.reflect.Method getMethods0(int)[];
395    */

396   public
397     void java_lang_Class_getMethods0(SootMethod method,
398                      ReferenceVariable thisVar,
399                      ReferenceVariable returnVar,
400                      ReferenceVariable params[]) {
401     helper.assignObjectTo(returnVar, Environment.v().getLeastArrayObject());
402   }
403
404   /**
405    * Returns a Constructor object that reflects the specified public
406    * constructor of the class represented by this Class object. The
407    * parameterTypes parameter is an array of Class objects that
408    * identify the constructor's formal parameter types, in declared
409    * order.
410    *
411    * private native java.lang.reflect.Constructor getConstructors0(int)[];
412    */

413   public
414     void java_lang_Class_getConstructors0(SootMethod method,
415                       ReferenceVariable thisVar,
416                       ReferenceVariable returnVar,
417                       ReferenceVariable params[]) {
418     helper.assignObjectTo(returnVar, Environment.v().getLeastArrayObject());
419   }
420
421   /**
422    * Returns a Field object that reflects the specified public member
423    * field of the class or interface represented by this Class object.
424    *
425    * Called by getField(String)
426    *
427    * NOTE: getField0(String name), since the name can be dynamically
428    * constructed, it may be not able to know exact field name
429    * in static analysis. Uses a C.F to represent the class field.
430    *
431    * private native java.lang.reflect.Field getField0(java.lang.String,
432    * int);
433    */

434   public
435     void java_lang_Class_getField0(SootMethod method,
436                    ReferenceVariable thisVar,
437                    ReferenceVariable returnVar,
438                    ReferenceVariable params[]){
439     helper.assignObjectTo(returnVar, Environment.v().getFieldObject());
440   }
441
442   /**
443    * Returns a Method object that reflects the specified public member
444    * method of the class or interface represented by this Class
445    * object.
446    *
447    * Called by getMethod()
448    *
449    * private native java.lang.reflect.Method getMethod0(java.lang.String,
450    * java.lang.Class[],
451    * int);
452    */

453   public
454     void java_lang_Class_getMethod0(SootMethod method,
455                     ReferenceVariable thisVar,
456                     ReferenceVariable returnVar,
457                     ReferenceVariable params[]){
458     helper.assignObjectTo(returnVar, Environment.v().getMethodObject());
459   }
460
461   /**
462    * Returns a constructor of a class
463    *
464    * private native java.lang.reflect.Constructor
465    * getConstructor0(java.lang.Class[], int);
466    */

467   public
468     void java_lang_Class_getConstructor0(SootMethod method,
469                      ReferenceVariable thisVar,
470                      ReferenceVariable returnVar,
471                      ReferenceVariable params[]){
472     helper.assignObjectTo(returnVar, Environment.v().getConstructorObject());
473   }
474
475   /**
476    * Returns an array of Class objects reflecting all the classes and
477    * interfaces declared as members of the class represented by this
478    * Class object.
479    *
480    * private native java.lang.Class getDeclaredClasses0()[];
481    */

482   public
483     void java_lang_Class_getDeclaredClasses0(SootMethod method,
484                          ReferenceVariable thisVar,
485                          ReferenceVariable returnVar,
486                          ReferenceVariable params[]) {
487     helper.assignObjectTo(returnVar, Environment.v().getLeastArrayObject());
488   }
489
490   /**
491    * Following methods have NO side effects.
492    *
493    * private static native void registerNatives();
494    * public native boolean isInstance(java.lang.Object);
495    * public native boolean isAssignableFrom(java.lang.Class);
496    * public native boolean isInterface();
497    * public native boolean isArray();
498    * public native boolean isPrimitive();
499    * public native int getModifiers();
500    */

501
502 }
503
Popular Tags