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 className, String[] parameterizedTypes, String ownerType,
112 MJIEnv env, int objRef, int superObjRef) {
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", superObjRef);
124 // actualTypeArguments
125 int[] types = new int[parameterizedTypes.length];
126 for(int j = 0; j < parameterizedTypes.length; j++) {
127 types[j] = getTypeVariableImplObject(env, objRef, 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(className, typeVars, ownerType, env, ci.getClassObjectRef(),
177 sci.getClassObjectRef());
186 public int getGenericInterfaces_____3Ljava_lang_reflect_Type_2 (MJIEnv env, int robj) {
187 // TODO: Need to add ParameterizedTypeImpl
188 ClassInfo ci = env.getReferredClassInfo(robj);
189 int aref = MJIEnv.NULL;
190 ThreadInfo ti = env.getThreadInfo();
192 // contrary to the API doc, this only returns the interfaces directly
193 // implemented by this class, not it's bases
194 // <2do> this is not exactly correct, since the interfaces should be ordered
195 Set<ClassInfo> interfaces = ci.getInterfaceClassInfos();
196 aref = env.newObjectArray("Ljava/lang/Class;", interfaces.size());
199 for (ClassInfo ifc: interfaces){
200 env.setReferenceArrayElement(aref, i++, ifc.getClassObjectRef());
207 public int getTypeParameters_____3Ljava_lang_reflect_TypeVariable_2 (MJIEnv env, int robj){
208 // Get the ClassInfo for this class
209 ClassInfo tci = env.getReferredClassInfo(robj);
210 String[] typeVars = tci.getGenericTypeVariableNames();
212 // Return with null if this is not a generic class
213 if (typeVars.length == 0) {
214 int aRef = env.newObjectArray("Ljava/lang/reflect/TypeVariable;", 0);
217 // Now create a TypeVariableImpl object for every type variable name; get the ClassInfo for TypeVariableImpl
218 ClassLoaderInfo cli = env.getSystemClassLoaderInfo();
219 ClassInfo ci = cli.getResolvedClassInfo("sun.reflect.generics.reflectiveObjects.TypeVariableImpl");
220 int[] var = new int[typeVars.length];
222 for(int i = 0; i < typeVars.length; i++) {
223 int typeVarRef = env.newObject(ci);
224 ElementInfo ei = env.getModifiableElementInfo(typeVarRef);
225 ei.setReferenceField("genericDeclaration", tci.getClassObjectRef());
226 ei.setReferenceField("name", env.newString(typeVars[i]));
229 int aRef = env.newObjectArray("Ljava/lang/reflect/TypeVariable;", typeVars.length);
231 // Set references for every array element
232 for (int i = 0; i < typeVars.length; i++) {
233 env.setReferenceArrayElement(aRef, i, var[i]);
240 public int getAnnotation__Ljava_lang_Class_2__Ljava_lang_annotation_Annotation_2 (MJIEnv env, int robj,
242 ClassInfo ci = env.getReferredClassInfo( robj);
243 ClassInfo aci = env.getReferredClassInfo(annoClsRef);
245 AnnotationInfo ai = ci.getAnnotation(aci.getName());
247 ClassInfo aciProxy = aci.getAnnotationProxy();
250 return env.newAnnotationProxy(aciProxy, ai);
251 } catch (ClinitRequired x){
252 env.handleClinitRequest(x.getRequiredClassInfo());
261 public int getPrimitiveClass__Ljava_lang_String_2__Ljava_lang_Class_2 (MJIEnv env,
262 int rcls, int stringRef) {
263 // we don't really have to check for a valid class name here, since
264 // this is a package default method that just gets called from
265 // the clinit of box classes
266 // note this does NOT return the box class (e.g. java.lang.Integer), which
267 // is a normal, functional class, but a primitive class (e.g. 'int') that
268 // is rather a strange beast (not even Object derived)
270 ClassLoaderInfo scli = env.getSystemClassLoaderInfo(); // this is the one responsible for builtin classes
271 String primClsName = env.getStringObject(stringRef); // always initialized
273 ClassInfo ci = scli.getResolvedClassInfo(primClsName);
274 return ci.getClassObjectRef();
278 public boolean desiredAssertionStatus____Z (MJIEnv env, int robj) {
279 ClassInfo ci = env.getReferredClassInfo(robj);
280 return ci.desiredAssertionStatus();
283 public static int getClassObject (MJIEnv env, ClassInfo ci){
284 ThreadInfo ti = env.getThreadInfo();
285 Instruction insn = ti.getPC();
287 if (ci.initializeClass(ti)){
288 env.repeatInvocation();
292 StaticElementInfo ei = ci.getStaticElementInfo();
293 int ref = ei.getClassObjectRef();
299 public int forName__Ljava_lang_String_2__Ljava_lang_Class_2 (MJIEnv env,
302 if (clsNameRef == MJIEnv.NULL){
303 env.throwException("java.lang.NullPointerException", "no class name provided");
307 String clsName = env.getStringObject(clsNameRef);
309 if (clsName.isEmpty()){
310 env.throwException("java.lang.ClassNotFoundException", "empty class name");
314 ThreadInfo ti = env.getThreadInfo();
315 MethodInfo mi = ti.getTopFrame().getPrevious().getMethodInfo();
316 // class of the method that includes the invocation of Class.forName()
317 ClassInfo cls = mi.getClassInfo();
320 // for array type, the component terminal must be resolved
321 if(clsName.charAt(0)=='[') {
322 name = Types.getComponentTerminal(clsName);
327 // make the classloader of the class including the invocation of
328 // Class.forName() resolve the class with the given name
330 cls.resolveReferencedClass(name);
331 } catch(LoadOnJPFRequired lre) {
332 env.repeatInvocation();
336 // The class obtained here is the same as the resolved one, except
337 // if it represents an array type
338 ClassInfo ci = cls.getClassLoaderInfo().getResolvedClassInfo(clsName);
340 return getClassObject(env, ci);
344 * this is an example of a native method issuing direct calls - otherwise known
346 * We don't have to deal with class init here anymore, since this is called
347 * via the class object of the class to instantiate
350 public int newInstance____Ljava_lang_Object_2 (MJIEnv env, int robj) {
351 ThreadInfo ti = env.getThreadInfo();
352 DirectCallStackFrame frame = ti.getReturnedDirectCall();
354 ClassInfo ci = env.getReferredClassInfo(robj); // what are we
355 MethodInfo miCtor = ci.getMethod("<init>()V", true); // note there always is one since something needs to call Object()
357 if (frame == null){ // first time around
358 if(ci.isAbstract()){ // not allowed to instantiate
359 env.throwException("java.lang.InstantiationException");
363 // <2do> - still need to handle protected
364 if (miCtor.isPrivate()) {
365 env.throwException("java.lang.IllegalAccessException", "cannot access non-public member of class " + ci.getName());
369 int objRef = env.newObjectOfUncheckedClass(ci); // create the thing
371 frame = miCtor.createDirectCallStackFrame(ti, 1);
372 // note that we don't set this as a reflection call since it is supposed to propagate exceptions
373 frame.setReferenceArgument(0, objRef, null);
374 frame.setLocalReferenceVariable(0, objRef); // (1) store ref for retrieval during re-exec
377 // check if we have to push clinits
378 ci.initializeClass(ti);
380 env.repeatInvocation();
383 } else { // re-execution
384 int objRef = frame.getLocalVariable(0); // that's the object ref we set in (1)
390 public int getSuperclass____Ljava_lang_Class_2 (MJIEnv env, int robj) {
391 ClassInfo ci = env.getReferredClassInfo( robj);
392 ClassInfo sci = ci.getSuperClass();
394 return sci.getClassObjectRef();
400 int getMethod (MJIEnv env, int clsRef, ClassInfo ciMethod, String mname, int argTypesRef,
401 boolean isRecursiveLookup, boolean publicOnly) {
403 ClassInfo ci = env.getReferredClassInfo( clsRef);
405 StringBuilder sb = new StringBuilder(mname);
407 int nParams = argTypesRef != MJIEnv.NULL ? env.getArrayLength(argTypesRef) : 0;
408 for (int i=0; i<nParams; i++) {
409 int cRef = env.getReferenceArrayElement(argTypesRef, i);
410 ClassInfo cit = env.getReferredClassInfo( cRef);
411 String tname = cit.getName();
412 String tcode = tname;
413 tcode = Types.getTypeSignature(tcode, false);
417 String fullMthName = sb.toString();
419 MethodInfo mi = ci.getReflectionMethod(fullMthName, isRecursiveLookup);
420 if (mi == null || (publicOnly && !mi.isPublic())) {
421 env.throwException("java.lang.NoSuchMethodException", ci.getName() + '.' + fullMthName);
425 return createMethodObject(env, ciMethod, mi);
429 int createMethodObject (MJIEnv env, ClassInfo objectCi, MethodInfo mi) {
430 // NOTE - we rely on Constructor and Method peers being initialized
432 return JPF_java_lang_reflect_Constructor.createConstructorObject(env, objectCi, mi);
434 return JPF_java_lang_reflect_Method.createMethodObject(env, objectCi, mi);
439 public int getDeclaredMethod__Ljava_lang_String_2_3Ljava_lang_Class_2__Ljava_lang_reflect_Method_2 (MJIEnv env, int clsRef,
440 int nameRef, int argTypesRef) {
441 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
443 env.repeatInvocation();
447 String mname = env.getStringObject(nameRef);
448 return getMethod(env, clsRef, mci, mname, argTypesRef, false, false);
452 public int getDeclaredConstructor___3Ljava_lang_Class_2__Ljava_lang_reflect_Constructor_2 (MJIEnv env,
455 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
457 env.repeatInvocation();
461 int ctorRef = getMethod(env,clsRef, mci, "<init>",argTypesRef,false, false);
466 public int getMethod__Ljava_lang_String_2_3Ljava_lang_Class_2__Ljava_lang_reflect_Method_2 (MJIEnv env, int clsRef,
467 int nameRef, int argTypesRef) {
468 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
470 env.repeatInvocation();
474 String mname = env.getStringObject(nameRef);
475 return getMethod( env, clsRef, mci, mname, argTypesRef, true, true);
478 private void addDeclaredMethodsRec (boolean includeSuperClasses, HashMap<String,MethodInfo>methods, ClassInfo ci){
480 if (includeSuperClasses){ // do NOT include Object methods for interfaces
481 ClassInfo sci = ci.getSuperClass();
483 addDeclaredMethodsRec( includeSuperClasses, methods,sci);
487 ClassLoaderInfo cl = ci.getClassLoaderInfo();
488 for (String ifcName : ci.getDirectInterfaceNames()){
489 ClassInfo ici = cl.getResolvedClassInfo(ifcName); // has to be already defined, so no exception
490 addDeclaredMethodsRec( includeSuperClasses, methods,ici);
493 for (MethodInfo mi : ci.getDeclaredMethodInfos()) {
494 // filter out non-public, <clinit> and <init>
495 if (mi.isPublic() && (mi.getName().charAt(0) != '<')) {
496 String mname = mi.getUniqueName();
498 if (!(ci.isInterface() && methods.containsKey(mname))){
499 methods.put(mname, mi);
506 public int getMethods_____3Ljava_lang_reflect_Method_2 (MJIEnv env, int objref) {
507 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
509 env.repeatInvocation();
513 ClassInfo ci = env.getReferredClassInfo(objref);
515 // collect all the public, non-ctor instance methods
516 if (!ci.isPrimitive()) {
517 HashMap<String,MethodInfo> methods = new HashMap<String,MethodInfo>();
518 addDeclaredMethodsRec( !ci.isInterface(), methods,ci);
520 int n = methods.size();
521 int aref = env.newObjectArray("Ljava/lang/reflect/Method;", n);
524 for (MethodInfo mi : methods.values()){
525 int mref = createMethodObject(env, mci, mi);
526 env.setReferenceArrayElement(aref,i++,mref);
532 return env.newObjectArray("Ljava/lang/reflect/Method;", 0);
537 public int getDeclaredMethods_____3Ljava_lang_reflect_Method_2 (MJIEnv env, int objref) {
538 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
540 env.repeatInvocation();
544 ClassInfo ci = env.getReferredClassInfo(objref);
545 MethodInfo[] methodInfos = ci.getDeclaredMethodInfos();
547 // we have to filter out the ctors and the static init
548 int nMth = methodInfos.length;
549 for (int i=0; i<methodInfos.length; i++){
550 if (methodInfos[i].getName().charAt(0) == '<'){
551 methodInfos[i] = null;
556 int aref = env.newObjectArray("Ljava/lang/reflect/Method;", nMth);
558 for (int i=0, j=0; i<methodInfos.length; i++) {
559 if (methodInfos[i] != null){
560 int mref = createMethodObject(env, mci, methodInfos[i]);
561 env.setReferenceArrayElement(aref,j++,mref);
568 int getConstructors (MJIEnv env, int objref, boolean publicOnly){
569 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
571 env.repeatInvocation();
575 ClassInfo ci = env.getReferredClassInfo(objref);
576 ArrayList<MethodInfo> ctors = new ArrayList<MethodInfo>();
578 // we have to filter out the ctors and the static init
579 for (MethodInfo mi : ci.getDeclaredMethodInfos()){
580 if (mi.getName().equals("<init>")){
581 if (!publicOnly || mi.isPublic()) {
587 int nCtors = ctors.size();
588 int aref = env.newObjectArray("Ljava/lang/reflect/Constructor;", nCtors);
590 for (int i=0; i<nCtors; i++){
591 env.setReferenceArrayElement(aref, i, createMethodObject(env, mci, ctors.get(i)));
598 public int getConstructors_____3Ljava_lang_reflect_Constructor_2 (MJIEnv env, int objref){
599 return getConstructors(env, objref, true);
603 public int getDeclaredConstructors_____3Ljava_lang_reflect_Constructor_2 (MJIEnv env, int objref){
604 return getConstructors(env, objref, false);
608 public int getConstructor___3Ljava_lang_Class_2__Ljava_lang_reflect_Constructor_2 (MJIEnv env, int clsRef,
610 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
612 env.repeatInvocation();
616 // <2do> should only return a public ctor
617 return getMethod(env,clsRef, mci, "<init>",argTypesRef,false,true);
620 // this is only used for system classes such as java.lang.reflect.Method
621 ClassInfo getInitializedClassInfo (MJIEnv env, String clsName){
622 ThreadInfo ti = env.getThreadInfo();
623 Instruction insn = ti.getPC();
624 ClassInfo ci = ClassLoaderInfo.getSystemResolvedClassInfo( clsName);
626 if (ci.initializeClass(ti)){
634 public void initialize0____V (MJIEnv env, int clsObjRef){
635 ClassInfo ci = env.getReferredClassInfo( clsObjRef);
636 ci.initializeClass(ThreadInfo.currentThread);
639 Set<ClassInfo> getInitializedInterfaces (MJIEnv env, ClassInfo ci){
640 ThreadInfo ti = env.getThreadInfo();
641 Instruction insn = ti.getPC();
643 Set<ClassInfo> ifcs = ci.getAllInterfaceClassInfos();
644 for (ClassInfo ciIfc : ifcs){
645 if (ciIfc.initializeClass(ti)){
653 static int createFieldObject (MJIEnv env, FieldInfo fi, ClassInfo fci){
654 int regIdx = JPF_java_lang_reflect_Field.registerFieldInfo(fi);
656 int eidx = env.newObject(fci);
657 ElementInfo ei = env.getModifiableElementInfo(eidx);
658 ei.setIntField("regIdx", regIdx);
664 public int getDeclaredFields_____3Ljava_lang_reflect_Field_2 (MJIEnv env, int objRef) {
665 ClassInfo fci = getInitializedClassInfo(env, FIELD_CLASSNAME);
667 env.repeatInvocation();
671 ClassInfo ci = env.getReferredClassInfo(objRef);
672 int nInstance = ci.getNumberOfDeclaredInstanceFields();
673 int nStatic = ci.getNumberOfStaticFields();
674 int aref = env.newObjectArray("Ljava/lang/reflect/Field;", nInstance + nStatic);
677 for (i=0; i<nStatic; i++) {
678 FieldInfo fi = ci.getStaticField(i);
679 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
682 for (i=0; i<nInstance; i++) {
683 FieldInfo fi = ci.getDeclaredInstanceField(i);
684 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
691 public int getFields_____3Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef){
692 ClassInfo fci = getInitializedClassInfo(env, FIELD_CLASSNAME);
694 env.repeatInvocation();
698 ClassInfo ci = env.getReferredClassInfo(clsRef);
699 // interfaces might not be initialized yet, so we have to check first
700 Set<ClassInfo> ifcs = getInitializedInterfaces( env, ci);
702 env.repeatInvocation();
706 ArrayList<FieldInfo> fiList = new ArrayList<FieldInfo>();
707 for (; ci != null; ci = ci.getSuperClass()){
708 // the host VM returns them in order of declaration, but the spec says there is no guaranteed order so we keep it simple
709 for (FieldInfo fi : ci.getDeclaredInstanceFields()){
714 for (FieldInfo fi : ci.getDeclaredStaticFields()){
721 for (ClassInfo ciIfc : ifcs){
722 for (FieldInfo fi : ciIfc.getDeclaredStaticFields()){
723 fiList.add(fi); // there are no non-public fields in interfaces
727 int aref = env.newObjectArray("Ljava/lang/reflect/Field;", fiList.size());
729 for (FieldInfo fi : fiList){
730 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
736 int getField (MJIEnv env, int clsRef, int nameRef, boolean isRecursiveLookup) {
737 ClassInfo ci = env.getReferredClassInfo( clsRef);
738 String fname = env.getStringObject(nameRef);
741 if (isRecursiveLookup) {
742 fi = ci.getInstanceField(fname);
744 fi = ci.getStaticField(fname);
747 fi = ci.getDeclaredInstanceField(fname);
749 fi = ci.getDeclaredStaticField(fname);
754 env.throwException("java.lang.NoSuchFieldException", fname);
758 // don't do a Field clinit before we know there is such a field
759 ClassInfo fci = getInitializedClassInfo( env, FIELD_CLASSNAME);
761 env.repeatInvocation();
765 return createFieldObject( env, fi, fci);
770 public int getDeclaredField__Ljava_lang_String_2__Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef, int nameRef) {
771 return getField(env,clsRef,nameRef, false);
775 public int getField__Ljava_lang_String_2__Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef, int nameRef) {
776 return getField(env,clsRef,nameRef, true);
780 public int getModifiers____I (MJIEnv env, int clsRef){
781 ClassInfo ci = env.getReferredClassInfo(clsRef);
782 return ci.getModifiers();
786 public int getEnumConstants_____3Ljava_lang_Object_2 (MJIEnv env, int clsRef){
787 ClassInfo ci = env.getReferredClassInfo(clsRef);
789 if (env.requiresClinitExecution(ci)){
790 env.repeatInvocation();
794 if (ci.getSuperClass().getName().equals("java.lang.Enum")) {
795 ArrayList<FieldInfo> list = new ArrayList<FieldInfo>();
796 String cName = ci.getName();
798 for (FieldInfo fi : ci.getDeclaredStaticFields()) {
799 if (fi.isFinal() && cName.equals(fi.getType())){
804 int aRef = env.newObjectArray(cName, list.size());
805 StaticElementInfo sei = ci.getStaticElementInfo();
807 for (FieldInfo fi : list){
808 env.setReferenceArrayElement( aRef, i++, sei.getReferenceField(fi));
817 public int getInterfaces_____3Ljava_lang_Class_2 (MJIEnv env, int clsRef){
818 ClassInfo ci = env.getReferredClassInfo(clsRef);
819 int aref = MJIEnv.NULL;
820 ThreadInfo ti = env.getThreadInfo();
822 // contrary to the API doc, this only returns the interfaces directly
823 // implemented by this class, not it's bases
824 // <2do> this is not exactly correct, since the interfaces should be ordered
825 Set<ClassInfo> interfaces = ci.getInterfaceClassInfos();
826 aref = env.newObjectArray("Ljava/lang/Class;", interfaces.size());
829 for (ClassInfo ifc: interfaces){
830 env.setReferenceArrayElement(aref, i++, ifc.getClassObjectRef());
838 * <2do> needs to load from the classfile location, NOT the MJIEnv (native) class
840 * @author Sebastian Gfeller (sebastian.gfeller@gmail.com)
841 * @author Tihomir Gvero (tihomir.gvero@gmail.com)
844 public int getByteArrayFromResourceStream__Ljava_lang_String_2___3B(MJIEnv env, int clsRef, int nameRef) {
845 String name = env.getStringObject(nameRef);
847 // <2do> this is not loading from the classfile location! fix it
848 InputStream is = env.getClass().getResourceAsStream(name);
852 // We assume that the entire input stream can be read at the moment,
853 // although this could break.
854 byte[] content = null;
856 content = new byte[is.available()];
858 } catch (IOException e) {
859 throw new RuntimeException(e);
861 // Now if everything worked, the content should be in the byte buffer.
862 // We put this buffer into the JPF VM.
863 return env.newByteArray(content);
867 public int getEnclosingClass____Ljava_lang_Class_2 (MJIEnv env, int clsRef) {
868 ClassInfo ciEncl = env.getReferredClassInfo( clsRef).getEnclosingClassInfo();
874 if (ciEncl.initializeClass(env.getThreadInfo())) {
875 env.repeatInvocation();
879 return ciEncl.getClassObjectRef();
883 public int getDeclaredClasses_____3Ljava_lang_Class_2 (MJIEnv env, int clsRef){
884 ClassInfo ci = env.getReferredClassInfo(clsRef);
885 String[] innerClassNames = ci.getInnerClasses();
886 int aref = MJIEnv.NULL;
887 ThreadInfo ti = env.getThreadInfo();
889 MethodInfo mi = ti.getTopFrame().getPrevious().getMethodInfo();
890 // class of the method that includes the invocation of Class.getDeclaredClasses
891 ClassInfo cls = mi.getClassInfo();
893 // first resolve all the inner classes
894 int length = innerClassNames.length;
895 ClassInfo[] resolvedInnerClass = new ClassInfo[length];
896 for(int i=0; i<length; i++) {
898 resolvedInnerClass[i] = cls.resolveReferencedClass(innerClassNames[i]);
899 } catch(LoadOnJPFRequired lre) {
900 env.repeatInvocation();
905 aref = env.newObjectArray("Ljava/lang/Class;", innerClassNames.length);
906 for (int i=0; i<length; i++){
907 ClassInfo ici = resolvedInnerClass[i];
908 if (!ici.isRegistered()) {
909 ici.registerClass(ti);
911 env.setReferenceArrayElement(aref, i, ici.getClassObjectRef());
917 private String getCanonicalName (ClassInfo ci){
919 String canonicalName = getCanonicalName(ci.getComponentClassInfo());
920 if (canonicalName != null){
921 return canonicalName + "[]";
926 if (isLocalOrAnonymousClass(ci)) {
929 if (ci.getEnclosingClassInfo() == null){
932 String enclosingName = getCanonicalName(ci.getEnclosingClassInfo());
933 if (enclosingName == null){ return null; }
934 return enclosingName + "." + ci.getSimpleName();
939 public int getCanonicalName____Ljava_lang_String_2 (MJIEnv env, int clsRef){
940 ClassInfo ci = env.getReferredClassInfo(clsRef);
941 return env.newString(getCanonicalName(ci));
945 public boolean isAnnotation____Z (MJIEnv env, int clsObjRef){
946 ClassInfo ci = env.getReferredClassInfo(clsObjRef);
947 return (ci.getModifiers() & 0x2000) != 0;
951 public boolean isAnnotationPresent__Ljava_lang_Class_2__Z (MJIEnv env, int clsObjRef, int annoClsObjRef){
952 ClassInfo ci = env.getReferredClassInfo(clsObjRef);
953 ClassInfo ciAnno = env.getReferredClassInfo(annoClsObjRef);
955 return ci.getAnnotation( ciAnno.getName()) != null;
959 public int getDeclaredAnnotations_____3Ljava_lang_annotation_Annotation_2 (MJIEnv env, int robj){
960 ClassInfo ci = env.getReferredClassInfo(robj);
963 return env.newAnnotationProxies(ci.getDeclaredAnnotations());
964 } catch (ClinitRequired x){
965 env.handleClinitRequest(x.getRequiredClassInfo());
971 public int getEnclosingConstructor____Ljava_lang_reflect_Constructor_2 (MJIEnv env, int robj){
972 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
974 env.repeatInvocation();
977 ClassInfo ci = env.getReferredClassInfo(robj);
978 MethodInfo enclosingMethod = ci.getEnclosingMethodInfo();
980 if ((enclosingMethod != null) && enclosingMethod.isCtor()){
981 return createMethodObject(env, mci, enclosingMethod);
987 public int getEnclosingMethod____Ljava_lang_reflect_Method_2 (MJIEnv env, int robj){
988 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
990 env.repeatInvocation();
993 ClassInfo ci = env.getReferredClassInfo(robj);
994 MethodInfo enclosingMethod = ci.getEnclosingMethodInfo();
996 if ((enclosingMethod != null) && !enclosingMethod.isCtor()){
997 return createMethodObject(env, mci, enclosingMethod);
1003 public boolean isAnonymousClass____Z (MJIEnv env, int robj){
1004 ClassInfo ci = env.getReferredClassInfo(robj);
1005 String cname = null;
1006 if (ci.getName().contains("$")){
1007 cname = ci.getName().substring(ci.getName().lastIndexOf('$') + 1);
1009 return (cname == null) ? false : cname.matches("\\d+?");
1013 public boolean isEnum____Z (MJIEnv env, int robj){
1014 ClassInfo ci = env.getReferredClassInfo(robj);
1018 // Similar to getEnclosingClass() except it returns null for the case of
1021 public int getDeclaringClass____Ljava_lang_Class_2 (MJIEnv env, int clsRef){
1022 ClassInfo ci = env.getReferredClassInfo(clsRef);
1023 if (isLocalOrAnonymousClass(ci)){
1026 return getEnclosingClass____Ljava_lang_Class_2(env, clsRef);
1031 public boolean isLocalClass____Z (MJIEnv env, int robj){
1032 ClassInfo ci = env.getReferredClassInfo(robj);
1033 return isLocalOrAnonymousClass(ci) && !isAnonymousClass____Z(env, robj);
1036 private boolean isLocalOrAnonymousClass (ClassInfo ci){
1037 return (ci.getEnclosingMethodInfo() != null);
1041 public boolean isMemberClass____Z (MJIEnv env, int robj){
1042 ClassInfo ci = env.getReferredClassInfo(robj);
1043 return (ci.getEnclosingClassInfo() != null) && !isLocalOrAnonymousClass(ci);
1047 * Append the package name prefix of the class represented by robj, if the name is not
1048 * absolute. OW, remove leading "/".
1051 public int getResolvedName__Ljava_lang_String_2__Ljava_lang_String_2 (MJIEnv env, int robj, int resRef){
1052 String rname = env.getStringObject(resRef);
1053 ClassInfo ci = env.getReferredClassInfo(robj);
1054 if (rname == null) {
1057 if (!rname.startsWith("/")) {
1059 while (c.isArray()) {
1060 c = c.getComponentClassInfo();
1062 String baseName = c.getName();
1063 int index = baseName.lastIndexOf('.');
1065 rname = baseName.substring(0, index).replace('.', '/')
1069 rname = rname.substring(1);
1072 return env.newString(rname);