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));
164 // TODO: Fix for Groovy's model-checking
166 public int getGenericSuperclass____Ljava_lang_reflect_Type_2 (MJIEnv env, int robj){
167 ClassInfo ci = env.getReferredClassInfo( robj);
168 ClassInfo sci = ci.getSuperClass();
170 String[] typeVars = sci.getGenericTypeVariableNames();
171 // Not a generic class
172 if (typeVars.length == 0) {
173 return sci.getClassObjectRef();
175 String className = sci.getName();
176 String ownerType = Types.getOwnerClassName(className);
177 int gRef = getParameterizedTypeImplObj(typeVars, ownerType, env, ci.getClassObjectRef(), sci.getClassObjectRef());
186 public int getGenericInterfaces_____3Ljava_lang_reflect_Type_2 (MJIEnv env, int robj) {
187 ClassInfo ci = env.getReferredClassInfo(robj);
188 int aref = MJIEnv.NULL;
189 ThreadInfo ti = env.getThreadInfo();
190 // contrary to the API doc, this only returns the interfaces directly
191 // implemented by this class, not it's bases
192 // <2do> this is not exactly correct, since the interfaces should be ordered
193 Set<ClassInfo> interfaces = ci.getInterfaceClassInfos();
195 aref = env.newObjectArray("Ljava/lang/Class;", interfaces.size());
198 for (ClassInfo ifc: interfaces){
199 if (Types.isGenericSignature(ifc.getGenericSignature())) {
200 String className = ifc.getName();
201 String ownerType = Types.getOwnerClassName(className);
202 String[] typeVars = ifc.getGenericTypeVariableNames();
203 int paramTypeObj = getParameterizedTypeImplObj(typeVars, ownerType, env, ci.getClassObjectRef(),
204 ifc.getClassObjectRef());
205 env.setReferenceArrayElement(aref, i++, paramTypeObj);
207 env.setReferenceArrayElement(aref, i++, ifc.getClassObjectRef());
215 public int getTypeParameters_____3Ljava_lang_reflect_TypeVariable_2 (MJIEnv env, int robj){
216 // Get the ClassInfo for this class
217 ClassInfo tci = env.getReferredClassInfo(robj);
218 String[] typeVars = tci.getGenericTypeVariableNames();
220 // Return with null if this is not a generic class
221 if (typeVars.length == 0) {
222 int aRef = env.newObjectArray("Ljava/lang/reflect/TypeVariable;", 0);
225 // Now create a TypeVariableImpl object for every type variable name; get the ClassInfo for TypeVariableImpl
226 ClassLoaderInfo cli = env.getSystemClassLoaderInfo();
227 ClassInfo ci = cli.getResolvedClassInfo("sun.reflect.generics.reflectiveObjects.TypeVariableImpl");
228 int[] var = new int[typeVars.length];
230 for(int i = 0; i < typeVars.length; i++) {
231 int typeVarRef = env.newObject(ci);
232 ElementInfo ei = env.getModifiableElementInfo(typeVarRef);
233 ei.setReferenceField("genericDeclaration", tci.getClassObjectRef());
234 ei.setReferenceField("name", env.newString(typeVars[i]));
237 int aRef = env.newObjectArray("Ljava/lang/reflect/TypeVariable;", typeVars.length);
239 // Set references for every array element
240 for (int i = 0; i < typeVars.length; i++) {
241 env.setReferenceArrayElement(aRef, i, var[i]);
248 public int getProtectionDomain____Ljava_security_ProtectionDomain_2 (MJIEnv env, int robj) {
249 // Now create a ProtectionDomain object
250 ClassLoaderInfo cli = env.getSystemClassLoaderInfo();
251 ClassInfo pdci = cli.getResolvedClassInfo("java.security.ProtectionDomain");
253 int proDomRef = MJIEnv.NULL;
254 /* TODO: Defer the following to future implementations
255 int proDomRef = env.newObject(pdci);
256 ElementInfo ei = env.getModifiableElementInfo(proDomRef);
257 ei.setReferenceField("codesource", MJIEnv.NULL);
258 ei.setReferenceField("classloader", MJIEnv.NULL);
259 ei.setBooleanField("hasAllPerm", true);
260 ei.setBooleanField("staticPermissions", true);
262 // Load the Permission (AllPermission class)
263 ClassInfo pci = cli.getResolvedClassInfo("java.security.AllPermission");
264 int permRef = env.newObject(pci);
265 ElementInfo pei = env.getModifiableElementInfo(permRef);
266 pei.setReferenceField("name", env.newString("<all permissions>"));
267 ei.setReferenceField("permissions", permRef);*/
271 // TODO: Fix for Groovy's model-checking
274 public int getAnnotation__Ljava_lang_Class_2__Ljava_lang_annotation_Annotation_2 (MJIEnv env, int robj,
276 ClassInfo ci = env.getReferredClassInfo( robj);
277 ClassInfo aci = env.getReferredClassInfo(annoClsRef);
279 AnnotationInfo ai = ci.getAnnotation(aci.getName());
281 ClassInfo aciProxy = aci.getAnnotationProxy();
284 return env.newAnnotationProxy(aciProxy, ai);
285 } catch (ClinitRequired x){
286 env.handleClinitRequest(x.getRequiredClassInfo());
295 public int getPrimitiveClass__Ljava_lang_String_2__Ljava_lang_Class_2 (MJIEnv env,
296 int rcls, int stringRef) {
297 // we don't really have to check for a valid class name here, since
298 // this is a package default method that just gets called from
299 // the clinit of box classes
300 // note this does NOT return the box class (e.g. java.lang.Integer), which
301 // is a normal, functional class, but a primitive class (e.g. 'int') that
302 // is rather a strange beast (not even Object derived)
304 ClassLoaderInfo scli = env.getSystemClassLoaderInfo(); // this is the one responsible for builtin classes
305 String primClsName = env.getStringObject(stringRef); // always initialized
307 ClassInfo ci = scli.getResolvedClassInfo(primClsName);
308 return ci.getClassObjectRef();
312 public boolean desiredAssertionStatus____Z (MJIEnv env, int robj) {
313 ClassInfo ci = env.getReferredClassInfo(robj);
314 return ci.desiredAssertionStatus();
317 public static int getClassObject (MJIEnv env, ClassInfo ci){
318 ThreadInfo ti = env.getThreadInfo();
319 Instruction insn = ti.getPC();
321 if (ci.initializeClass(ti)){
322 env.repeatInvocation();
326 StaticElementInfo ei = ci.getStaticElementInfo();
327 int ref = ei.getClassObjectRef();
333 public int forName__Ljava_lang_String_2__Ljava_lang_Class_2 (MJIEnv env,
336 if (clsNameRef == MJIEnv.NULL){
337 env.throwException("java.lang.NullPointerException", "no class name provided");
341 String clsName = env.getStringObject(clsNameRef);
343 if (clsName.isEmpty()){
344 env.throwException("java.lang.ClassNotFoundException", "empty class name");
348 ThreadInfo ti = env.getThreadInfo();
349 MethodInfo mi = ti.getTopFrame().getPrevious().getMethodInfo();
350 // class of the method that includes the invocation of Class.forName()
351 ClassInfo cls = mi.getClassInfo();
354 // for array type, the component terminal must be resolved
355 if(clsName.charAt(0)=='[') {
356 name = Types.getComponentTerminal(clsName);
361 // make the classloader of the class including the invocation of
362 // Class.forName() resolve the class with the given name
364 cls.resolveReferencedClass(name);
365 } catch(LoadOnJPFRequired lre) {
366 env.repeatInvocation();
370 // The class obtained here is the same as the resolved one, except
371 // if it represents an array type
372 ClassInfo ci = cls.getClassLoaderInfo().getResolvedClassInfo(clsName);
374 return getClassObject(env, ci);
378 * this is an example of a native method issuing direct calls - otherwise known
380 * We don't have to deal with class init here anymore, since this is called
381 * via the class object of the class to instantiate
384 public int newInstance____Ljava_lang_Object_2 (MJIEnv env, int robj) {
385 ThreadInfo ti = env.getThreadInfo();
386 DirectCallStackFrame frame = ti.getReturnedDirectCall();
388 ClassInfo ci = env.getReferredClassInfo(robj); // what are we
389 MethodInfo miCtor = ci.getMethod("<init>()V", true); // note there always is one since something needs to call Object()
391 if (frame == null){ // first time around
392 if(ci.isAbstract()){ // not allowed to instantiate
393 env.throwException("java.lang.InstantiationException");
397 // <2do> - still need to handle protected
398 if (miCtor.isPrivate()) {
399 env.throwException("java.lang.IllegalAccessException", "cannot access non-public member of class " + ci.getName());
403 int objRef = env.newObjectOfUncheckedClass(ci); // create the thing
405 frame = miCtor.createDirectCallStackFrame(ti, 1);
406 // note that we don't set this as a reflection call since it is supposed to propagate exceptions
407 frame.setReferenceArgument(0, objRef, null);
408 frame.setLocalReferenceVariable(0, objRef); // (1) store ref for retrieval during re-exec
411 // check if we have to push clinits
412 ci.initializeClass(ti);
414 env.repeatInvocation();
417 } else { // re-execution
418 int objRef = frame.getLocalVariable(0); // that's the object ref we set in (1)
424 public int getSuperclass____Ljava_lang_Class_2 (MJIEnv env, int robj) {
425 ClassInfo ci = env.getReferredClassInfo( robj);
426 ClassInfo sci = ci.getSuperClass();
428 return sci.getClassObjectRef();
434 int getMethod (MJIEnv env, int clsRef, ClassInfo ciMethod, String mname, int argTypesRef,
435 boolean isRecursiveLookup, boolean publicOnly) {
437 ClassInfo ci = env.getReferredClassInfo( clsRef);
439 StringBuilder sb = new StringBuilder(mname);
441 int nParams = argTypesRef != MJIEnv.NULL ? env.getArrayLength(argTypesRef) : 0;
442 for (int i=0; i<nParams; i++) {
443 int cRef = env.getReferenceArrayElement(argTypesRef, i);
444 ClassInfo cit = env.getReferredClassInfo( cRef);
445 String tname = cit.getName();
446 String tcode = tname;
447 tcode = Types.getTypeSignature(tcode, false);
451 String fullMthName = sb.toString();
453 MethodInfo mi = ci.getReflectionMethod(fullMthName, isRecursiveLookup);
454 if (mi == null || (publicOnly && !mi.isPublic())) {
455 env.throwException("java.lang.NoSuchMethodException", ci.getName() + '.' + fullMthName);
459 return createMethodObject(env, ciMethod, mi);
463 int createMethodObject (MJIEnv env, ClassInfo objectCi, MethodInfo mi) {
464 // NOTE - we rely on Constructor and Method peers being initialized
466 return JPF_java_lang_reflect_Constructor.createConstructorObject(env, objectCi, mi);
468 return JPF_java_lang_reflect_Method.createMethodObject(env, objectCi, mi);
473 public int getDeclaredMethod__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, false, false);
486 public int getDeclaredConstructor___3Ljava_lang_Class_2__Ljava_lang_reflect_Constructor_2 (MJIEnv env,
489 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
491 env.repeatInvocation();
495 int ctorRef = getMethod(env,clsRef, mci, "<init>",argTypesRef,false, false);
500 public int getMethod__Ljava_lang_String_2_3Ljava_lang_Class_2__Ljava_lang_reflect_Method_2 (MJIEnv env, int clsRef,
501 int nameRef, int argTypesRef) {
502 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
504 env.repeatInvocation();
508 String mname = env.getStringObject(nameRef);
509 return getMethod( env, clsRef, mci, mname, argTypesRef, true, true);
512 private void addDeclaredMethodsRec (boolean includeSuperClasses, HashMap<String,MethodInfo>methods, ClassInfo ci){
514 if (includeSuperClasses){ // do NOT include Object methods for interfaces
515 ClassInfo sci = ci.getSuperClass();
517 addDeclaredMethodsRec( includeSuperClasses, methods,sci);
521 ClassLoaderInfo cl = ci.getClassLoaderInfo();
522 for (String ifcName : ci.getDirectInterfaceNames()){
523 ClassInfo ici = cl.getResolvedClassInfo(ifcName); // has to be already defined, so no exception
524 addDeclaredMethodsRec( includeSuperClasses, methods,ici);
527 for (MethodInfo mi : ci.getDeclaredMethodInfos()) {
528 // filter out non-public, <clinit> and <init>
529 if (mi.isPublic() && (mi.getName().charAt(0) != '<')) {
530 String mname = mi.getUniqueName();
532 if (!(ci.isInterface() && methods.containsKey(mname))){
533 methods.put(mname, mi);
540 public int getMethods_____3Ljava_lang_reflect_Method_2 (MJIEnv env, int objref) {
541 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
543 env.repeatInvocation();
547 ClassInfo ci = env.getReferredClassInfo(objref);
549 // collect all the public, non-ctor instance methods
550 if (!ci.isPrimitive()) {
551 HashMap<String,MethodInfo> methods = new HashMap<String,MethodInfo>();
552 addDeclaredMethodsRec( !ci.isInterface(), methods,ci);
554 int n = methods.size();
555 int aref = env.newObjectArray("Ljava/lang/reflect/Method;", n);
558 for (MethodInfo mi : methods.values()){
559 int mref = createMethodObject(env, mci, mi);
560 env.setReferenceArrayElement(aref,i++,mref);
566 return env.newObjectArray("Ljava/lang/reflect/Method;", 0);
571 public int getDeclaredMethods_____3Ljava_lang_reflect_Method_2 (MJIEnv env, int objref) {
572 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
574 env.repeatInvocation();
578 ClassInfo ci = env.getReferredClassInfo(objref);
579 MethodInfo[] methodInfos = ci.getDeclaredMethodInfos();
581 // we have to filter out the ctors and the static init
582 int nMth = methodInfos.length;
583 for (int i=0; i<methodInfos.length; i++){
584 if (methodInfos[i].getName().charAt(0) == '<'){
585 methodInfos[i] = null;
590 int aref = env.newObjectArray("Ljava/lang/reflect/Method;", nMth);
592 for (int i=0, j=0; i<methodInfos.length; i++) {
593 if (methodInfos[i] != null){
594 int mref = createMethodObject(env, mci, methodInfos[i]);
595 env.setReferenceArrayElement(aref,j++,mref);
602 int getConstructors (MJIEnv env, int objref, boolean publicOnly){
603 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
605 env.repeatInvocation();
609 ClassInfo ci = env.getReferredClassInfo(objref);
610 ArrayList<MethodInfo> ctors = new ArrayList<MethodInfo>();
612 // we have to filter out the ctors and the static init
613 for (MethodInfo mi : ci.getDeclaredMethodInfos()){
614 if (mi.getName().equals("<init>")){
615 if (!publicOnly || mi.isPublic()) {
621 int nCtors = ctors.size();
622 int aref = env.newObjectArray("Ljava/lang/reflect/Constructor;", nCtors);
624 for (int i=0; i<nCtors; i++){
625 env.setReferenceArrayElement(aref, i, createMethodObject(env, mci, ctors.get(i)));
632 public int getConstructors_____3Ljava_lang_reflect_Constructor_2 (MJIEnv env, int objref){
633 return getConstructors(env, objref, true);
637 public int getDeclaredConstructors_____3Ljava_lang_reflect_Constructor_2 (MJIEnv env, int objref){
638 return getConstructors(env, objref, false);
642 public int getConstructor___3Ljava_lang_Class_2__Ljava_lang_reflect_Constructor_2 (MJIEnv env, int clsRef,
644 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
646 env.repeatInvocation();
650 // <2do> should only return a public ctor
651 return getMethod(env,clsRef, mci, "<init>",argTypesRef,false,true);
654 // this is only used for system classes such as java.lang.reflect.Method
655 ClassInfo getInitializedClassInfo (MJIEnv env, String clsName){
656 ThreadInfo ti = env.getThreadInfo();
657 Instruction insn = ti.getPC();
658 ClassInfo ci = ClassLoaderInfo.getSystemResolvedClassInfo( clsName);
660 if (ci.initializeClass(ti)){
668 public void initialize0____V (MJIEnv env, int clsObjRef){
669 ClassInfo ci = env.getReferredClassInfo( clsObjRef);
670 ci.initializeClass(ThreadInfo.currentThread);
673 Set<ClassInfo> getInitializedInterfaces (MJIEnv env, ClassInfo ci){
674 ThreadInfo ti = env.getThreadInfo();
675 Instruction insn = ti.getPC();
677 Set<ClassInfo> ifcs = ci.getAllInterfaceClassInfos();
678 for (ClassInfo ciIfc : ifcs){
679 if (ciIfc.initializeClass(ti)){
687 static int createFieldObject (MJIEnv env, FieldInfo fi, ClassInfo fci){
688 int regIdx = JPF_java_lang_reflect_Field.registerFieldInfo(fi);
690 int eidx = env.newObject(fci);
691 ElementInfo ei = env.getModifiableElementInfo(eidx);
692 ei.setIntField("regIdx", regIdx);
698 public int getDeclaredFields_____3Ljava_lang_reflect_Field_2 (MJIEnv env, int objRef) {
699 ClassInfo fci = getInitializedClassInfo(env, FIELD_CLASSNAME);
701 env.repeatInvocation();
705 ClassInfo ci = env.getReferredClassInfo(objRef);
706 int nInstance = ci.getNumberOfDeclaredInstanceFields();
707 int nStatic = ci.getNumberOfStaticFields();
708 int aref = env.newObjectArray("Ljava/lang/reflect/Field;", nInstance + nStatic);
711 for (i=0; i<nStatic; i++) {
712 FieldInfo fi = ci.getStaticField(i);
713 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
716 for (i=0; i<nInstance; i++) {
717 FieldInfo fi = ci.getDeclaredInstanceField(i);
718 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
725 public int getFields_____3Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef){
726 ClassInfo fci = getInitializedClassInfo(env, FIELD_CLASSNAME);
728 env.repeatInvocation();
732 ClassInfo ci = env.getReferredClassInfo(clsRef);
733 // interfaces might not be initialized yet, so we have to check first
734 Set<ClassInfo> ifcs = getInitializedInterfaces( env, ci);
736 env.repeatInvocation();
740 ArrayList<FieldInfo> fiList = new ArrayList<FieldInfo>();
741 for (; ci != null; ci = ci.getSuperClass()){
742 // the host VM returns them in order of declaration, but the spec says there is no guaranteed order so we keep it simple
743 for (FieldInfo fi : ci.getDeclaredInstanceFields()){
748 for (FieldInfo fi : ci.getDeclaredStaticFields()){
755 for (ClassInfo ciIfc : ifcs){
756 for (FieldInfo fi : ciIfc.getDeclaredStaticFields()){
757 fiList.add(fi); // there are no non-public fields in interfaces
761 int aref = env.newObjectArray("Ljava/lang/reflect/Field;", fiList.size());
763 for (FieldInfo fi : fiList){
764 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
770 int getField (MJIEnv env, int clsRef, int nameRef, boolean isRecursiveLookup) {
771 ClassInfo ci = env.getReferredClassInfo( clsRef);
772 String fname = env.getStringObject(nameRef);
775 if (isRecursiveLookup) {
776 fi = ci.getInstanceField(fname);
778 fi = ci.getStaticField(fname);
781 fi = ci.getDeclaredInstanceField(fname);
783 fi = ci.getDeclaredStaticField(fname);
788 env.throwException("java.lang.NoSuchFieldException", fname);
792 // don't do a Field clinit before we know there is such a field
793 ClassInfo fci = getInitializedClassInfo( env, FIELD_CLASSNAME);
795 env.repeatInvocation();
799 return createFieldObject( env, fi, fci);
804 public int getDeclaredField__Ljava_lang_String_2__Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef, int nameRef) {
805 return getField(env,clsRef,nameRef, false);
809 public int getField__Ljava_lang_String_2__Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef, int nameRef) {
810 return getField(env,clsRef,nameRef, true);
814 public int getModifiers____I (MJIEnv env, int clsRef){
815 ClassInfo ci = env.getReferredClassInfo(clsRef);
816 return ci.getModifiers();
820 public int getEnumConstants_____3Ljava_lang_Object_2 (MJIEnv env, int clsRef){
821 ClassInfo ci = env.getReferredClassInfo(clsRef);
823 if (env.requiresClinitExecution(ci)){
824 env.repeatInvocation();
828 if (ci.getSuperClass().getName().equals("java.lang.Enum")) {
829 ArrayList<FieldInfo> list = new ArrayList<FieldInfo>();
830 String cName = ci.getName();
832 for (FieldInfo fi : ci.getDeclaredStaticFields()) {
833 if (fi.isFinal() && cName.equals(fi.getType())){
838 int aRef = env.newObjectArray(cName, list.size());
839 StaticElementInfo sei = ci.getStaticElementInfo();
841 for (FieldInfo fi : list){
842 env.setReferenceArrayElement( aRef, i++, sei.getReferenceField(fi));
851 public int getInterfaces_____3Ljava_lang_Class_2 (MJIEnv env, int clsRef){
852 ClassInfo ci = env.getReferredClassInfo(clsRef);
853 int aref = MJIEnv.NULL;
854 ThreadInfo ti = env.getThreadInfo();
856 // contrary to the API doc, this only returns the interfaces directly
857 // implemented by this class, not it's bases
858 // <2do> this is not exactly correct, since the interfaces should be ordered
859 Set<ClassInfo> interfaces = ci.getInterfaceClassInfos();
860 aref = env.newObjectArray("Ljava/lang/Class;", interfaces.size());
863 for (ClassInfo ifc: interfaces){
864 env.setReferenceArrayElement(aref, i++, ifc.getClassObjectRef());
872 * <2do> needs to load from the classfile location, NOT the MJIEnv (native) class
874 * @author Sebastian Gfeller (sebastian.gfeller@gmail.com)
875 * @author Tihomir Gvero (tihomir.gvero@gmail.com)
878 public int getByteArrayFromResourceStream__Ljava_lang_String_2___3B(MJIEnv env, int clsRef, int nameRef) {
879 String name = env.getStringObject(nameRef);
881 // <2do> this is not loading from the classfile location! fix it
882 InputStream is = env.getClass().getResourceAsStream(name);
886 // We assume that the entire input stream can be read at the moment,
887 // although this could break.
888 byte[] content = null;
890 content = new byte[is.available()];
892 } catch (IOException e) {
893 throw new RuntimeException(e);
895 // Now if everything worked, the content should be in the byte buffer.
896 // We put this buffer into the JPF VM.
897 return env.newByteArray(content);
901 public int getEnclosingClass____Ljava_lang_Class_2 (MJIEnv env, int clsRef) {
902 ClassInfo ciEncl = env.getReferredClassInfo( clsRef).getEnclosingClassInfo();
908 if (ciEncl.initializeClass(env.getThreadInfo())) {
909 env.repeatInvocation();
913 return ciEncl.getClassObjectRef();
917 public int getDeclaredClasses_____3Ljava_lang_Class_2 (MJIEnv env, int clsRef){
918 ClassInfo ci = env.getReferredClassInfo(clsRef);
919 String[] innerClassNames = ci.getInnerClasses();
920 int aref = MJIEnv.NULL;
921 ThreadInfo ti = env.getThreadInfo();
923 MethodInfo mi = ti.getTopFrame().getPrevious().getMethodInfo();
924 // class of the method that includes the invocation of Class.getDeclaredClasses
925 ClassInfo cls = mi.getClassInfo();
927 // first resolve all the inner classes
928 int length = innerClassNames.length;
929 ClassInfo[] resolvedInnerClass = new ClassInfo[length];
930 for(int i=0; i<length; i++) {
932 resolvedInnerClass[i] = cls.resolveReferencedClass(innerClassNames[i]);
933 } catch(LoadOnJPFRequired lre) {
934 env.repeatInvocation();
939 aref = env.newObjectArray("Ljava/lang/Class;", innerClassNames.length);
940 for (int i=0; i<length; i++){
941 ClassInfo ici = resolvedInnerClass[i];
942 if (!ici.isRegistered()) {
943 ici.registerClass(ti);
945 env.setReferenceArrayElement(aref, i, ici.getClassObjectRef());
951 private String getCanonicalName (ClassInfo ci){
953 String canonicalName = getCanonicalName(ci.getComponentClassInfo());
954 if (canonicalName != null){
955 return canonicalName + "[]";
960 if (isLocalOrAnonymousClass(ci)) {
963 if (ci.getEnclosingClassInfo() == null){
966 String enclosingName = getCanonicalName(ci.getEnclosingClassInfo());
967 if (enclosingName == null){ return null; }
968 return enclosingName + "." + ci.getSimpleName();
973 public int getCanonicalName____Ljava_lang_String_2 (MJIEnv env, int clsRef){
974 ClassInfo ci = env.getReferredClassInfo(clsRef);
975 return env.newString(getCanonicalName(ci));
979 public boolean isAnnotation____Z (MJIEnv env, int clsObjRef){
980 ClassInfo ci = env.getReferredClassInfo(clsObjRef);
981 return (ci.getModifiers() & 0x2000) != 0;
985 public boolean isAnnotationPresent__Ljava_lang_Class_2__Z (MJIEnv env, int clsObjRef, int annoClsObjRef){
986 ClassInfo ci = env.getReferredClassInfo(clsObjRef);
987 ClassInfo ciAnno = env.getReferredClassInfo(annoClsObjRef);
989 return ci.getAnnotation( ciAnno.getName()) != null;
993 public int getDeclaredAnnotations_____3Ljava_lang_annotation_Annotation_2 (MJIEnv env, int robj){
994 ClassInfo ci = env.getReferredClassInfo(robj);
997 return env.newAnnotationProxies(ci.getDeclaredAnnotations());
998 } catch (ClinitRequired x){
999 env.handleClinitRequest(x.getRequiredClassInfo());
1005 public int getEnclosingConstructor____Ljava_lang_reflect_Constructor_2 (MJIEnv env, int robj){
1006 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
1008 env.repeatInvocation();
1011 ClassInfo ci = env.getReferredClassInfo(robj);
1012 MethodInfo enclosingMethod = ci.getEnclosingMethodInfo();
1014 if ((enclosingMethod != null) && enclosingMethod.isCtor()){
1015 return createMethodObject(env, mci, enclosingMethod);
1021 public int getEnclosingMethod____Ljava_lang_reflect_Method_2 (MJIEnv env, int robj){
1022 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
1024 env.repeatInvocation();
1027 ClassInfo ci = env.getReferredClassInfo(robj);
1028 MethodInfo enclosingMethod = ci.getEnclosingMethodInfo();
1030 if ((enclosingMethod != null) && !enclosingMethod.isCtor()){
1031 return createMethodObject(env, mci, enclosingMethod);
1037 public boolean isAnonymousClass____Z (MJIEnv env, int robj){
1038 ClassInfo ci = env.getReferredClassInfo(robj);
1039 String cname = null;
1040 if (ci.getName().contains("$")){
1041 cname = ci.getName().substring(ci.getName().lastIndexOf('$') + 1);
1043 return (cname == null) ? false : cname.matches("\\d+?");
1047 public boolean isEnum____Z (MJIEnv env, int robj){
1048 ClassInfo ci = env.getReferredClassInfo(robj);
1052 // Similar to getEnclosingClass() except it returns null for the case of
1055 public int getDeclaringClass____Ljava_lang_Class_2 (MJIEnv env, int clsRef){
1056 ClassInfo ci = env.getReferredClassInfo(clsRef);
1057 if (isLocalOrAnonymousClass(ci)){
1060 return getEnclosingClass____Ljava_lang_Class_2(env, clsRef);
1065 public boolean isLocalClass____Z (MJIEnv env, int robj){
1066 ClassInfo ci = env.getReferredClassInfo(robj);
1067 return isLocalOrAnonymousClass(ci) && !isAnonymousClass____Z(env, robj);
1070 private boolean isLocalOrAnonymousClass (ClassInfo ci){
1071 return (ci.getEnclosingMethodInfo() != null);
1075 public boolean isMemberClass____Z (MJIEnv env, int robj){
1076 ClassInfo ci = env.getReferredClassInfo(robj);
1077 return (ci.getEnclosingClassInfo() != null) && !isLocalOrAnonymousClass(ci);
1081 * Append the package name prefix of the class represented by robj, if the name is not
1082 * absolute. OW, remove leading "/".
1085 public int getResolvedName__Ljava_lang_String_2__Ljava_lang_String_2 (MJIEnv env, int robj, int resRef){
1086 String rname = env.getStringObject(resRef);
1087 ClassInfo ci = env.getReferredClassInfo(robj);
1088 if (rname == null) {
1091 if (!rname.startsWith("/")) {
1093 while (c.isArray()) {
1094 c = c.getComponentClassInfo();
1096 String baseName = c.getName();
1097 int index = baseName.lastIndexOf('.');
1099 rname = baseName.substring(0, index).replace('.', '/')
1103 rname = rname.substring(1);
1106 return env.newString(rname);