2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.vm;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.ArrayList;
23 import java.util.HashMap;
26 import gov.nasa.jpf.Config;
27 import gov.nasa.jpf.annotation.MJI;
30 * MJI NativePeer class for java.lang.Class library abstraction
32 public class JPF_java_lang_Class extends NativePeer {
34 static final String FIELD_CLASSNAME = "java.lang.reflect.Field";
35 static final String METHOD_CLASSNAME = "java.lang.reflect.Method";
36 static final String CONSTRUCTOR_CLASSNAME = "java.lang.reflect.Constructor";
38 public static boolean init (Config conf){
39 // we create Method and Constructor objects, so we better make sure these
40 // classes are initialized (they already might be)
41 JPF_java_lang_reflect_Method.init(conf);
42 JPF_java_lang_reflect_Constructor.init(conf);
47 public boolean isArray____Z (MJIEnv env, int robj) {
48 ClassInfo ci = env.getReferredClassInfo( robj);
53 public int getComponentType____Ljava_lang_Class_2 (MJIEnv env, int robj) {
54 if (isArray____Z(env, robj)) {
55 ThreadInfo ti = env.getThreadInfo();
56 Instruction insn = ti.getPC();
57 ClassInfo ci = env.getReferredClassInfo( robj).getComponentClassInfo();
59 if (ci.initializeClass(ti)){
60 env.repeatInvocation();
64 return ci.getClassObjectRef();
71 public boolean isInstance__Ljava_lang_Object_2__Z (MJIEnv env, int robj,
73 ElementInfo sei = env.getStaticElementInfo(robj);
74 ClassInfo ci = sei.getClassInfo();
75 ClassInfo ciOther = env.getClassInfo(r1);
76 return (ciOther.isInstanceOf(ci));
80 public boolean isInterface____Z (MJIEnv env, int robj){
81 ClassInfo ci = env.getReferredClassInfo( robj);
82 return ci.isInterface();
86 public boolean isAssignableFrom__Ljava_lang_Class_2__Z (MJIEnv env, int rcls,
88 ElementInfo sei1 = env.getStaticElementInfo(rcls);
89 ClassInfo ci1 = sei1.getClassInfo();
91 ElementInfo sei2 = env.getStaticElementInfo(r1);
92 ClassInfo ci2 = sei2.getClassInfo();
94 return ci2.isInstanceOf( ci1);
98 public int getAnnotations_____3Ljava_lang_annotation_Annotation_2 (MJIEnv env, int robj){
99 ClassInfo ci = env.getReferredClassInfo( robj);
100 AnnotationInfo[] ai = ci.getAnnotations();
103 return env.newAnnotationProxies(ai);
104 } catch (ClinitRequired x){
105 env.handleClinitRequest(x.getRequiredClassInfo());
110 // TODO: Fix for Groovy's model-checking
111 private static int getParameterizedTypeImplObj(String[] parameterizedTypes, String ownerType,
112 MJIEnv env, int actObjRef, int rawObjRef) {
114 ThreadInfo ti = env.getThreadInfo();
115 ClassLoaderInfo cli = env.getSystemClassLoaderInfo();
116 ClassInfo ci = cli.getResolvedClassInfo("sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl");
117 // Create a new object of type ParameterizedTypeImpl
118 int paramTypeRef = env.newObject(ci);
119 ElementInfo ei = env.getModifiableElementInfo(paramTypeRef);
120 // Get field information and fill out the fields
122 ei.setReferenceField("rawType", rawObjRef);
124 // actualTypeArguments
125 int[] types = new int[parameterizedTypes.length];
126 for(int j = 0; j < parameterizedTypes.length; j++) {
127 types[j] = getTypeVariableImplObject(env, actObjRef, parameterizedTypes[j]);
129 int aRef = env.newObjectArray("Ljava/lang/reflect/Type;", parameterizedTypes.length);
130 // Set references for every array element
131 for (int j = 0; j < parameterizedTypes.length; j++) {
132 env.setReferenceArrayElement(aRef, j, types[j]);
134 ei.setReferenceField("actualTypeArguments", aRef);
137 if (ownerType != null) {
138 ClassInfo oci = ClassLoaderInfo.getCurrentResolvedClassInfo(ownerType);
139 if (!oci.isRegistered()) {
140 oci.registerClass(ti);
142 ei.setReferenceField("ownerType", oci.getClassObjectRef());
144 ei.setReferenceField("ownerType", MJIEnv.NULL);
150 private static int getTypeVariableImplObject(MJIEnv env, int objRef, String parameterizedType) {
152 ClassLoaderInfo cli = env.getSystemClassLoaderInfo();
153 ClassInfo ci = cli.getResolvedClassInfo("sun.reflect.generics.reflectiveObjects.TypeVariableImpl");
155 int typeVarRef = env.newObject(ci);
156 ElementInfo ei = env.getModifiableElementInfo(typeVarRef);
157 // genericDeclaration contains this java.lang.reflect.Method object
158 ei.setReferenceField("genericDeclaration", objRef);
159 ei.setReferenceField("name", env.newString(parameterizedType));
165 public int getGenericSuperclass____Ljava_lang_reflect_Type_2 (MJIEnv env, int robj){
166 ClassInfo ci = env.getReferredClassInfo( robj);
167 ClassInfo sci = ci.getSuperClass();
169 String[] typeVars = sci.getGenericTypeVariableNames();
170 // Not a generic class
171 if (typeVars.length == 0) {
172 return sci.getClassObjectRef();
174 String className = sci.getName();
175 String ownerType = Types.getOwnerClassName(className);
176 int gRef = getParameterizedTypeImplObj(typeVars, ownerType, env, ci.getClassObjectRef(), sci.getClassObjectRef());
185 public int getGenericInterfaces_____3Ljava_lang_reflect_Type_2 (MJIEnv env, int robj) {
186 ClassInfo ci = env.getReferredClassInfo(robj);
187 int aref = MJIEnv.NULL;
188 ThreadInfo ti = env.getThreadInfo();
189 // contrary to the API doc, this only returns the interfaces directly
190 // implemented by this class, not it's bases
191 // <2do> this is not exactly correct, since the interfaces should be ordered
192 Set<ClassInfo> interfaces = ci.getInterfaceClassInfos();
194 aref = env.newObjectArray("Ljava/lang/Class;", interfaces.size());
197 for (ClassInfo ifc: interfaces){
198 if (Types.isGenericSignature(ifc.getGenericSignature())) {
199 String className = ifc.getName();
200 String ownerType = Types.getOwnerClassName(className);
201 String[] typeVars = ifc.getGenericTypeVariableNames();
202 int paramTypeObj = getParameterizedTypeImplObj(typeVars, ownerType, env, ci.getClassObjectRef(),
203 ifc.getClassObjectRef());
204 env.setReferenceArrayElement(aref, i++, paramTypeObj);
206 env.setReferenceArrayElement(aref, i++, ifc.getClassObjectRef());
214 public int getTypeParameters_____3Ljava_lang_reflect_TypeVariable_2 (MJIEnv env, int robj){
215 // Get the ClassInfo for this class
216 ClassInfo tci = env.getReferredClassInfo(robj);
217 String[] typeVars = tci.getGenericTypeVariableNames();
219 // Return with null if this is not a generic class
220 if (typeVars.length == 0) {
221 int aRef = env.newObjectArray("Ljava/lang/reflect/TypeVariable;", 0);
224 // Now create a TypeVariableImpl object for every type variable name; get the ClassInfo for TypeVariableImpl
225 ClassLoaderInfo cli = env.getSystemClassLoaderInfo();
226 ClassInfo ci = cli.getResolvedClassInfo("sun.reflect.generics.reflectiveObjects.TypeVariableImpl");
227 int[] var = new int[typeVars.length];
229 for(int i = 0; i < typeVars.length; i++) {
230 int typeVarRef = env.newObject(ci);
231 ElementInfo ei = env.getModifiableElementInfo(typeVarRef);
232 ei.setReferenceField("genericDeclaration", tci.getClassObjectRef());
233 ei.setReferenceField("name", env.newString(typeVars[i]));
236 int aRef = env.newObjectArray("Ljava/lang/reflect/TypeVariable;", typeVars.length);
238 // Set references for every array element
239 for (int i = 0; i < typeVars.length; i++) {
240 env.setReferenceArrayElement(aRef, i, var[i]);
247 public int getAnnotation__Ljava_lang_Class_2__Ljava_lang_annotation_Annotation_2 (MJIEnv env, int robj,
249 ClassInfo ci = env.getReferredClassInfo( robj);
250 ClassInfo aci = env.getReferredClassInfo(annoClsRef);
252 AnnotationInfo ai = ci.getAnnotation(aci.getName());
254 ClassInfo aciProxy = aci.getAnnotationProxy();
257 return env.newAnnotationProxy(aciProxy, ai);
258 } catch (ClinitRequired x){
259 env.handleClinitRequest(x.getRequiredClassInfo());
268 public int getPrimitiveClass__Ljava_lang_String_2__Ljava_lang_Class_2 (MJIEnv env,
269 int rcls, int stringRef) {
270 // we don't really have to check for a valid class name here, since
271 // this is a package default method that just gets called from
272 // the clinit of box classes
273 // note this does NOT return the box class (e.g. java.lang.Integer), which
274 // is a normal, functional class, but a primitive class (e.g. 'int') that
275 // is rather a strange beast (not even Object derived)
277 ClassLoaderInfo scli = env.getSystemClassLoaderInfo(); // this is the one responsible for builtin classes
278 String primClsName = env.getStringObject(stringRef); // always initialized
280 ClassInfo ci = scli.getResolvedClassInfo(primClsName);
281 return ci.getClassObjectRef();
285 public boolean desiredAssertionStatus____Z (MJIEnv env, int robj) {
286 ClassInfo ci = env.getReferredClassInfo(robj);
287 return ci.desiredAssertionStatus();
290 public static int getClassObject (MJIEnv env, ClassInfo ci){
291 ThreadInfo ti = env.getThreadInfo();
292 Instruction insn = ti.getPC();
294 if (ci.initializeClass(ti)){
295 env.repeatInvocation();
299 StaticElementInfo ei = ci.getStaticElementInfo();
300 int ref = ei.getClassObjectRef();
306 public int forName__Ljava_lang_String_2__Ljava_lang_Class_2 (MJIEnv env,
309 if (clsNameRef == MJIEnv.NULL){
310 env.throwException("java.lang.NullPointerException", "no class name provided");
314 String clsName = env.getStringObject(clsNameRef);
316 if (clsName.isEmpty()){
317 env.throwException("java.lang.ClassNotFoundException", "empty class name");
321 ThreadInfo ti = env.getThreadInfo();
322 MethodInfo mi = ti.getTopFrame().getPrevious().getMethodInfo();
323 // class of the method that includes the invocation of Class.forName()
324 ClassInfo cls = mi.getClassInfo();
327 // for array type, the component terminal must be resolved
328 if(clsName.charAt(0)=='[') {
329 name = Types.getComponentTerminal(clsName);
334 // make the classloader of the class including the invocation of
335 // Class.forName() resolve the class with the given name
337 cls.resolveReferencedClass(name);
338 } catch(LoadOnJPFRequired lre) {
339 env.repeatInvocation();
343 // The class obtained here is the same as the resolved one, except
344 // if it represents an array type
345 ClassInfo ci = cls.getClassLoaderInfo().getResolvedClassInfo(clsName);
347 return getClassObject(env, ci);
351 * this is an example of a native method issuing direct calls - otherwise known
353 * We don't have to deal with class init here anymore, since this is called
354 * via the class object of the class to instantiate
357 public int newInstance____Ljava_lang_Object_2 (MJIEnv env, int robj) {
358 ThreadInfo ti = env.getThreadInfo();
359 DirectCallStackFrame frame = ti.getReturnedDirectCall();
361 ClassInfo ci = env.getReferredClassInfo(robj); // what are we
362 MethodInfo miCtor = ci.getMethod("<init>()V", true); // note there always is one since something needs to call Object()
364 if (frame == null){ // first time around
365 if(ci.isAbstract()){ // not allowed to instantiate
366 env.throwException("java.lang.InstantiationException");
370 // <2do> - still need to handle protected
371 if (miCtor.isPrivate()) {
372 env.throwException("java.lang.IllegalAccessException", "cannot access non-public member of class " + ci.getName());
376 int objRef = env.newObjectOfUncheckedClass(ci); // create the thing
378 frame = miCtor.createDirectCallStackFrame(ti, 1);
379 // note that we don't set this as a reflection call since it is supposed to propagate exceptions
380 frame.setReferenceArgument(0, objRef, null);
381 frame.setLocalReferenceVariable(0, objRef); // (1) store ref for retrieval during re-exec
384 // check if we have to push clinits
385 ci.initializeClass(ti);
387 env.repeatInvocation();
390 } else { // re-execution
391 int objRef = frame.getLocalVariable(0); // that's the object ref we set in (1)
397 public int getSuperclass____Ljava_lang_Class_2 (MJIEnv env, int robj) {
398 ClassInfo ci = env.getReferredClassInfo( robj);
399 ClassInfo sci = ci.getSuperClass();
401 return sci.getClassObjectRef();
407 int getMethod (MJIEnv env, int clsRef, ClassInfo ciMethod, String mname, int argTypesRef,
408 boolean isRecursiveLookup, boolean publicOnly) {
410 ClassInfo ci = env.getReferredClassInfo( clsRef);
412 StringBuilder sb = new StringBuilder(mname);
414 int nParams = argTypesRef != MJIEnv.NULL ? env.getArrayLength(argTypesRef) : 0;
415 for (int i=0; i<nParams; i++) {
416 int cRef = env.getReferenceArrayElement(argTypesRef, i);
417 ClassInfo cit = env.getReferredClassInfo( cRef);
418 String tname = cit.getName();
419 String tcode = tname;
420 tcode = Types.getTypeSignature(tcode, false);
424 String fullMthName = sb.toString();
426 MethodInfo mi = ci.getReflectionMethod(fullMthName, isRecursiveLookup);
427 if (mi == null || (publicOnly && !mi.isPublic())) {
428 env.throwException("java.lang.NoSuchMethodException", ci.getName() + '.' + fullMthName);
432 return createMethodObject(env, ciMethod, mi);
436 int createMethodObject (MJIEnv env, ClassInfo objectCi, MethodInfo mi) {
437 // NOTE - we rely on Constructor and Method peers being initialized
439 return JPF_java_lang_reflect_Constructor.createConstructorObject(env, objectCi, mi);
441 return JPF_java_lang_reflect_Method.createMethodObject(env, objectCi, mi);
446 public int getDeclaredMethod__Ljava_lang_String_2_3Ljava_lang_Class_2__Ljava_lang_reflect_Method_2 (MJIEnv env, int clsRef,
447 int nameRef, int argTypesRef) {
448 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
450 env.repeatInvocation();
454 String mname = env.getStringObject(nameRef);
455 return getMethod(env, clsRef, mci, mname, argTypesRef, false, false);
459 public int getDeclaredConstructor___3Ljava_lang_Class_2__Ljava_lang_reflect_Constructor_2 (MJIEnv env,
462 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
464 env.repeatInvocation();
468 int ctorRef = getMethod(env,clsRef, mci, "<init>",argTypesRef,false, false);
473 public int getMethod__Ljava_lang_String_2_3Ljava_lang_Class_2__Ljava_lang_reflect_Method_2 (MJIEnv env, int clsRef,
474 int nameRef, int argTypesRef) {
475 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
477 env.repeatInvocation();
481 String mname = env.getStringObject(nameRef);
482 return getMethod( env, clsRef, mci, mname, argTypesRef, true, true);
485 private void addDeclaredMethodsRec (boolean includeSuperClasses, HashMap<String,MethodInfo>methods, ClassInfo ci){
487 if (includeSuperClasses){ // do NOT include Object methods for interfaces
488 ClassInfo sci = ci.getSuperClass();
490 addDeclaredMethodsRec( includeSuperClasses, methods,sci);
494 ClassLoaderInfo cl = ci.getClassLoaderInfo();
495 for (String ifcName : ci.getDirectInterfaceNames()){
496 ClassInfo ici = cl.getResolvedClassInfo(ifcName); // has to be already defined, so no exception
497 addDeclaredMethodsRec( includeSuperClasses, methods,ici);
500 for (MethodInfo mi : ci.getDeclaredMethodInfos()) {
501 // filter out non-public, <clinit> and <init>
502 if (mi.isPublic() && (mi.getName().charAt(0) != '<')) {
503 String mname = mi.getUniqueName();
505 if (!(ci.isInterface() && methods.containsKey(mname))){
506 methods.put(mname, mi);
513 public int getMethods_____3Ljava_lang_reflect_Method_2 (MJIEnv env, int objref) {
514 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
516 env.repeatInvocation();
520 ClassInfo ci = env.getReferredClassInfo(objref);
522 // collect all the public, non-ctor instance methods
523 if (!ci.isPrimitive()) {
524 HashMap<String,MethodInfo> methods = new HashMap<String,MethodInfo>();
525 addDeclaredMethodsRec( !ci.isInterface(), methods,ci);
527 int n = methods.size();
528 int aref = env.newObjectArray("Ljava/lang/reflect/Method;", n);
531 for (MethodInfo mi : methods.values()){
532 int mref = createMethodObject(env, mci, mi);
533 env.setReferenceArrayElement(aref,i++,mref);
539 return env.newObjectArray("Ljava/lang/reflect/Method;", 0);
544 public int getDeclaredMethods_____3Ljava_lang_reflect_Method_2 (MJIEnv env, int objref) {
545 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
547 env.repeatInvocation();
551 ClassInfo ci = env.getReferredClassInfo(objref);
552 MethodInfo[] methodInfos = ci.getDeclaredMethodInfos();
554 // we have to filter out the ctors and the static init
555 int nMth = methodInfos.length;
556 for (int i=0; i<methodInfos.length; i++){
557 if (methodInfos[i].getName().charAt(0) == '<'){
558 methodInfos[i] = null;
563 int aref = env.newObjectArray("Ljava/lang/reflect/Method;", nMth);
565 for (int i=0, j=0; i<methodInfos.length; i++) {
566 if (methodInfos[i] != null){
567 int mref = createMethodObject(env, mci, methodInfos[i]);
568 env.setReferenceArrayElement(aref,j++,mref);
575 int getConstructors (MJIEnv env, int objref, boolean publicOnly){
576 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
578 env.repeatInvocation();
582 ClassInfo ci = env.getReferredClassInfo(objref);
583 ArrayList<MethodInfo> ctors = new ArrayList<MethodInfo>();
585 // we have to filter out the ctors and the static init
586 for (MethodInfo mi : ci.getDeclaredMethodInfos()){
587 if (mi.getName().equals("<init>")){
588 if (!publicOnly || mi.isPublic()) {
594 int nCtors = ctors.size();
595 int aref = env.newObjectArray("Ljava/lang/reflect/Constructor;", nCtors);
597 for (int i=0; i<nCtors; i++){
598 env.setReferenceArrayElement(aref, i, createMethodObject(env, mci, ctors.get(i)));
605 public int getConstructors_____3Ljava_lang_reflect_Constructor_2 (MJIEnv env, int objref){
606 return getConstructors(env, objref, true);
610 public int getDeclaredConstructors_____3Ljava_lang_reflect_Constructor_2 (MJIEnv env, int objref){
611 return getConstructors(env, objref, false);
615 public int getConstructor___3Ljava_lang_Class_2__Ljava_lang_reflect_Constructor_2 (MJIEnv env, int clsRef,
617 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
619 env.repeatInvocation();
623 // <2do> should only return a public ctor
624 return getMethod(env,clsRef, mci, "<init>",argTypesRef,false,true);
627 // this is only used for system classes such as java.lang.reflect.Method
628 ClassInfo getInitializedClassInfo (MJIEnv env, String clsName){
629 ThreadInfo ti = env.getThreadInfo();
630 Instruction insn = ti.getPC();
631 ClassInfo ci = ClassLoaderInfo.getSystemResolvedClassInfo( clsName);
633 if (ci.initializeClass(ti)){
641 public void initialize0____V (MJIEnv env, int clsObjRef){
642 ClassInfo ci = env.getReferredClassInfo( clsObjRef);
643 ci.initializeClass(ThreadInfo.currentThread);
646 Set<ClassInfo> getInitializedInterfaces (MJIEnv env, ClassInfo ci){
647 ThreadInfo ti = env.getThreadInfo();
648 Instruction insn = ti.getPC();
650 Set<ClassInfo> ifcs = ci.getAllInterfaceClassInfos();
651 for (ClassInfo ciIfc : ifcs){
652 if (ciIfc.initializeClass(ti)){
660 static int createFieldObject (MJIEnv env, FieldInfo fi, ClassInfo fci){
661 int regIdx = JPF_java_lang_reflect_Field.registerFieldInfo(fi);
663 int eidx = env.newObject(fci);
664 ElementInfo ei = env.getModifiableElementInfo(eidx);
665 ei.setIntField("regIdx", regIdx);
671 public int getDeclaredFields_____3Ljava_lang_reflect_Field_2 (MJIEnv env, int objRef) {
672 ClassInfo fci = getInitializedClassInfo(env, FIELD_CLASSNAME);
674 env.repeatInvocation();
678 ClassInfo ci = env.getReferredClassInfo(objRef);
679 int nInstance = ci.getNumberOfDeclaredInstanceFields();
680 int nStatic = ci.getNumberOfStaticFields();
681 int aref = env.newObjectArray("Ljava/lang/reflect/Field;", nInstance + nStatic);
684 for (i=0; i<nStatic; i++) {
685 FieldInfo fi = ci.getStaticField(i);
686 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
689 for (i=0; i<nInstance; i++) {
690 FieldInfo fi = ci.getDeclaredInstanceField(i);
691 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
698 public int getFields_____3Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef){
699 ClassInfo fci = getInitializedClassInfo(env, FIELD_CLASSNAME);
701 env.repeatInvocation();
705 ClassInfo ci = env.getReferredClassInfo(clsRef);
706 // interfaces might not be initialized yet, so we have to check first
707 Set<ClassInfo> ifcs = getInitializedInterfaces( env, ci);
709 env.repeatInvocation();
713 ArrayList<FieldInfo> fiList = new ArrayList<FieldInfo>();
714 for (; ci != null; ci = ci.getSuperClass()){
715 // the host VM returns them in order of declaration, but the spec says there is no guaranteed order so we keep it simple
716 for (FieldInfo fi : ci.getDeclaredInstanceFields()){
721 for (FieldInfo fi : ci.getDeclaredStaticFields()){
728 for (ClassInfo ciIfc : ifcs){
729 for (FieldInfo fi : ciIfc.getDeclaredStaticFields()){
730 fiList.add(fi); // there are no non-public fields in interfaces
734 int aref = env.newObjectArray("Ljava/lang/reflect/Field;", fiList.size());
736 for (FieldInfo fi : fiList){
737 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
743 int getField (MJIEnv env, int clsRef, int nameRef, boolean isRecursiveLookup) {
744 ClassInfo ci = env.getReferredClassInfo( clsRef);
745 String fname = env.getStringObject(nameRef);
748 if (isRecursiveLookup) {
749 fi = ci.getInstanceField(fname);
751 fi = ci.getStaticField(fname);
754 fi = ci.getDeclaredInstanceField(fname);
756 fi = ci.getDeclaredStaticField(fname);
761 env.throwException("java.lang.NoSuchFieldException", fname);
765 // don't do a Field clinit before we know there is such a field
766 ClassInfo fci = getInitializedClassInfo( env, FIELD_CLASSNAME);
768 env.repeatInvocation();
772 return createFieldObject( env, fi, fci);
777 public int getDeclaredField__Ljava_lang_String_2__Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef, int nameRef) {
778 return getField(env,clsRef,nameRef, false);
782 public int getField__Ljava_lang_String_2__Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef, int nameRef) {
783 return getField(env,clsRef,nameRef, true);
787 public int getModifiers____I (MJIEnv env, int clsRef){
788 ClassInfo ci = env.getReferredClassInfo(clsRef);
789 return ci.getModifiers();
793 public int getEnumConstants_____3Ljava_lang_Object_2 (MJIEnv env, int clsRef){
794 ClassInfo ci = env.getReferredClassInfo(clsRef);
796 if (env.requiresClinitExecution(ci)){
797 env.repeatInvocation();
801 if (ci.getSuperClass().getName().equals("java.lang.Enum")) {
802 ArrayList<FieldInfo> list = new ArrayList<FieldInfo>();
803 String cName = ci.getName();
805 for (FieldInfo fi : ci.getDeclaredStaticFields()) {
806 if (fi.isFinal() && cName.equals(fi.getType())){
811 int aRef = env.newObjectArray(cName, list.size());
812 StaticElementInfo sei = ci.getStaticElementInfo();
814 for (FieldInfo fi : list){
815 env.setReferenceArrayElement( aRef, i++, sei.getReferenceField(fi));
824 public int getInterfaces_____3Ljava_lang_Class_2 (MJIEnv env, int clsRef){
825 ClassInfo ci = env.getReferredClassInfo(clsRef);
826 int aref = MJIEnv.NULL;
827 ThreadInfo ti = env.getThreadInfo();
829 // contrary to the API doc, this only returns the interfaces directly
830 // implemented by this class, not it's bases
831 // <2do> this is not exactly correct, since the interfaces should be ordered
832 Set<ClassInfo> interfaces = ci.getInterfaceClassInfos();
833 aref = env.newObjectArray("Ljava/lang/Class;", interfaces.size());
836 for (ClassInfo ifc: interfaces){
837 env.setReferenceArrayElement(aref, i++, ifc.getClassObjectRef());
845 * <2do> needs to load from the classfile location, NOT the MJIEnv (native) class
847 * @author Sebastian Gfeller (sebastian.gfeller@gmail.com)
848 * @author Tihomir Gvero (tihomir.gvero@gmail.com)
851 public int getByteArrayFromResourceStream__Ljava_lang_String_2___3B(MJIEnv env, int clsRef, int nameRef) {
852 String name = env.getStringObject(nameRef);
854 // <2do> this is not loading from the classfile location! fix it
855 InputStream is = env.getClass().getResourceAsStream(name);
859 // We assume that the entire input stream can be read at the moment,
860 // although this could break.
861 byte[] content = null;
863 content = new byte[is.available()];
865 } catch (IOException e) {
866 throw new RuntimeException(e);
868 // Now if everything worked, the content should be in the byte buffer.
869 // We put this buffer into the JPF VM.
870 return env.newByteArray(content);
874 public int getEnclosingClass____Ljava_lang_Class_2 (MJIEnv env, int clsRef) {
875 ClassInfo ciEncl = env.getReferredClassInfo( clsRef).getEnclosingClassInfo();
881 if (ciEncl.initializeClass(env.getThreadInfo())) {
882 env.repeatInvocation();
886 return ciEncl.getClassObjectRef();
890 public int getDeclaredClasses_____3Ljava_lang_Class_2 (MJIEnv env, int clsRef){
891 ClassInfo ci = env.getReferredClassInfo(clsRef);
892 String[] innerClassNames = ci.getInnerClasses();
893 int aref = MJIEnv.NULL;
894 ThreadInfo ti = env.getThreadInfo();
896 MethodInfo mi = ti.getTopFrame().getPrevious().getMethodInfo();
897 // class of the method that includes the invocation of Class.getDeclaredClasses
898 ClassInfo cls = mi.getClassInfo();
900 // first resolve all the inner classes
901 int length = innerClassNames.length;
902 ClassInfo[] resolvedInnerClass = new ClassInfo[length];
903 for(int i=0; i<length; i++) {
905 resolvedInnerClass[i] = cls.resolveReferencedClass(innerClassNames[i]);
906 } catch(LoadOnJPFRequired lre) {
907 env.repeatInvocation();
912 aref = env.newObjectArray("Ljava/lang/Class;", innerClassNames.length);
913 for (int i=0; i<length; i++){
914 ClassInfo ici = resolvedInnerClass[i];
915 if (!ici.isRegistered()) {
916 ici.registerClass(ti);
918 env.setReferenceArrayElement(aref, i, ici.getClassObjectRef());
924 private String getCanonicalName (ClassInfo ci){
926 String canonicalName = getCanonicalName(ci.getComponentClassInfo());
927 if (canonicalName != null){
928 return canonicalName + "[]";
933 if (isLocalOrAnonymousClass(ci)) {
936 if (ci.getEnclosingClassInfo() == null){
939 String enclosingName = getCanonicalName(ci.getEnclosingClassInfo());
940 if (enclosingName == null){ return null; }
941 return enclosingName + "." + ci.getSimpleName();
946 public int getCanonicalName____Ljava_lang_String_2 (MJIEnv env, int clsRef){
947 ClassInfo ci = env.getReferredClassInfo(clsRef);
948 return env.newString(getCanonicalName(ci));
952 public boolean isAnnotation____Z (MJIEnv env, int clsObjRef){
953 ClassInfo ci = env.getReferredClassInfo(clsObjRef);
954 return (ci.getModifiers() & 0x2000) != 0;
958 public boolean isAnnotationPresent__Ljava_lang_Class_2__Z (MJIEnv env, int clsObjRef, int annoClsObjRef){
959 ClassInfo ci = env.getReferredClassInfo(clsObjRef);
960 ClassInfo ciAnno = env.getReferredClassInfo(annoClsObjRef);
962 return ci.getAnnotation( ciAnno.getName()) != null;
966 public int getDeclaredAnnotations_____3Ljava_lang_annotation_Annotation_2 (MJIEnv env, int robj){
967 ClassInfo ci = env.getReferredClassInfo(robj);
970 return env.newAnnotationProxies(ci.getDeclaredAnnotations());
971 } catch (ClinitRequired x){
972 env.handleClinitRequest(x.getRequiredClassInfo());
978 public int getEnclosingConstructor____Ljava_lang_reflect_Constructor_2 (MJIEnv env, int robj){
979 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
981 env.repeatInvocation();
984 ClassInfo ci = env.getReferredClassInfo(robj);
985 MethodInfo enclosingMethod = ci.getEnclosingMethodInfo();
987 if ((enclosingMethod != null) && enclosingMethod.isCtor()){
988 return createMethodObject(env, mci, enclosingMethod);
994 public int getEnclosingMethod____Ljava_lang_reflect_Method_2 (MJIEnv env, int robj){
995 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
997 env.repeatInvocation();
1000 ClassInfo ci = env.getReferredClassInfo(robj);
1001 MethodInfo enclosingMethod = ci.getEnclosingMethodInfo();
1003 if ((enclosingMethod != null) && !enclosingMethod.isCtor()){
1004 return createMethodObject(env, mci, enclosingMethod);
1010 public boolean isAnonymousClass____Z (MJIEnv env, int robj){
1011 ClassInfo ci = env.getReferredClassInfo(robj);
1012 String cname = null;
1013 if (ci.getName().contains("$")){
1014 cname = ci.getName().substring(ci.getName().lastIndexOf('$') + 1);
1016 return (cname == null) ? false : cname.matches("\\d+?");
1020 public boolean isEnum____Z (MJIEnv env, int robj){
1021 ClassInfo ci = env.getReferredClassInfo(robj);
1025 // Similar to getEnclosingClass() except it returns null for the case of
1028 public int getDeclaringClass____Ljava_lang_Class_2 (MJIEnv env, int clsRef){
1029 ClassInfo ci = env.getReferredClassInfo(clsRef);
1030 if (isLocalOrAnonymousClass(ci)){
1033 return getEnclosingClass____Ljava_lang_Class_2(env, clsRef);
1038 public boolean isLocalClass____Z (MJIEnv env, int robj){
1039 ClassInfo ci = env.getReferredClassInfo(robj);
1040 return isLocalOrAnonymousClass(ci) && !isAnonymousClass____Z(env, robj);
1043 private boolean isLocalOrAnonymousClass (ClassInfo ci){
1044 return (ci.getEnclosingMethodInfo() != null);
1048 public boolean isMemberClass____Z (MJIEnv env, int robj){
1049 ClassInfo ci = env.getReferredClassInfo(robj);
1050 return (ci.getEnclosingClassInfo() != null) && !isLocalOrAnonymousClass(ci);
1054 * Append the package name prefix of the class represented by robj, if the name is not
1055 * absolute. OW, remove leading "/".
1058 public int getResolvedName__Ljava_lang_String_2__Ljava_lang_String_2 (MJIEnv env, int robj, int resRef){
1059 String rname = env.getStringObject(resRef);
1060 ClassInfo ci = env.getReferredClassInfo(robj);
1061 if (rname == null) {
1064 if (!rname.startsWith("/")) {
1066 while (c.isArray()) {
1067 c = c.getComponentClassInfo();
1069 String baseName = c.getName();
1070 int index = baseName.lastIndexOf('.');
1072 rname = baseName.substring(0, index).replace('.', '/')
1076 rname = rname.substring(1);
1079 return env.newString(rname);