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
112 public int getGenericSuperclass____Ljava_lang_reflect_Type_2 (MJIEnv env, int robj){
113 // TODO: Need to add ParameterizedTypeImpl
114 ClassInfo ci = env.getReferredClassInfo( robj);
115 ClassInfo sci = ci.getSuperClass();
117 return sci.getClassObjectRef();
124 public int getGenericInterfaces_____3Ljava_lang_reflect_Type_2 (MJIEnv env, int robj) {
125 // TODO: Need to add ParameterizedTypeImpl
126 ClassInfo ci = env.getReferredClassInfo(robj);
127 int aref = MJIEnv.NULL;
128 ThreadInfo ti = env.getThreadInfo();
130 // contrary to the API doc, this only returns the interfaces directly
131 // implemented by this class, not it's bases
132 // <2do> this is not exactly correct, since the interfaces should be ordered
133 Set<ClassInfo> interfaces = ci.getInterfaceClassInfos();
134 aref = env.newObjectArray("Ljava/lang/Class;", interfaces.size());
137 for (ClassInfo ifc: interfaces){
138 env.setReferenceArrayElement(aref, i++, ifc.getClassObjectRef());
145 public int getTypeParameters_____3Ljava_lang_reflect_TypeVariable_2 (MJIEnv env, int robj){
146 // Get the ClassInfo for this class
147 ClassInfo tci = env.getReferredClassInfo(robj);
148 String[] typeVars = tci.getGenericTypeVariableNames();
150 // Return with null if this is not a generic class
151 if (typeVars.length == 0) {
152 int aRef = env.newObjectArray("Ljava/lang/reflect/TypeVariable;", 0);
155 // Now create a TypeVariableImpl object for every type variable name; get the ClassInfo for TypeVariableImpl
156 ClassLoaderInfo cli = env.getSystemClassLoaderInfo();
157 ClassInfo ci = cli.getResolvedClassInfo("sun.reflect.generics.reflectiveObjects.TypeVariableImpl");
158 int[] var = new int[typeVars.length];
160 for(int i = 0; i < typeVars.length; i++) {
161 int typeVarRef = env.newObject(ci);
162 ElementInfo ei = env.getModifiableElementInfo(typeVarRef);
163 ei.setReferenceField("genericDeclaration", tci.getClassObjectRef());
164 ei.setReferenceField("name", env.newString(typeVars[i]));
167 int aRef = env.newObjectArray("Ljava/lang/reflect/TypeVariable;", typeVars.length);
169 // Set references for every array element
170 for (int i = 0; i < typeVars.length; i++) {
171 env.setReferenceArrayElement(aRef, i, var[i]);
178 public int getAnnotation__Ljava_lang_Class_2__Ljava_lang_annotation_Annotation_2 (MJIEnv env, int robj,
180 ClassInfo ci = env.getReferredClassInfo( robj);
181 ClassInfo aci = env.getReferredClassInfo(annoClsRef);
183 AnnotationInfo ai = ci.getAnnotation(aci.getName());
185 ClassInfo aciProxy = aci.getAnnotationProxy();
188 return env.newAnnotationProxy(aciProxy, ai);
189 } catch (ClinitRequired x){
190 env.handleClinitRequest(x.getRequiredClassInfo());
199 public int getPrimitiveClass__Ljava_lang_String_2__Ljava_lang_Class_2 (MJIEnv env,
200 int rcls, int stringRef) {
201 // we don't really have to check for a valid class name here, since
202 // this is a package default method that just gets called from
203 // the clinit of box classes
204 // note this does NOT return the box class (e.g. java.lang.Integer), which
205 // is a normal, functional class, but a primitive class (e.g. 'int') that
206 // is rather a strange beast (not even Object derived)
208 ClassLoaderInfo scli = env.getSystemClassLoaderInfo(); // this is the one responsible for builtin classes
209 String primClsName = env.getStringObject(stringRef); // always initialized
211 ClassInfo ci = scli.getResolvedClassInfo(primClsName);
212 return ci.getClassObjectRef();
216 public boolean desiredAssertionStatus____Z (MJIEnv env, int robj) {
217 ClassInfo ci = env.getReferredClassInfo(robj);
218 return ci.desiredAssertionStatus();
221 public static int getClassObject (MJIEnv env, ClassInfo ci){
222 ThreadInfo ti = env.getThreadInfo();
223 Instruction insn = ti.getPC();
225 if (ci.initializeClass(ti)){
226 env.repeatInvocation();
230 StaticElementInfo ei = ci.getStaticElementInfo();
231 int ref = ei.getClassObjectRef();
237 public int forName__Ljava_lang_String_2__Ljava_lang_Class_2 (MJIEnv env,
240 if (clsNameRef == MJIEnv.NULL){
241 env.throwException("java.lang.NullPointerException", "no class name provided");
245 String clsName = env.getStringObject(clsNameRef);
247 if (clsName.isEmpty()){
248 env.throwException("java.lang.ClassNotFoundException", "empty class name");
252 ThreadInfo ti = env.getThreadInfo();
253 MethodInfo mi = ti.getTopFrame().getPrevious().getMethodInfo();
254 // class of the method that includes the invocation of Class.forName()
255 ClassInfo cls = mi.getClassInfo();
258 // for array type, the component terminal must be resolved
259 if(clsName.charAt(0)=='[') {
260 name = Types.getComponentTerminal(clsName);
265 // make the classloader of the class including the invocation of
266 // Class.forName() resolve the class with the given name
268 cls.resolveReferencedClass(name);
269 } catch(LoadOnJPFRequired lre) {
270 env.repeatInvocation();
274 // The class obtained here is the same as the resolved one, except
275 // if it represents an array type
276 ClassInfo ci = cls.getClassLoaderInfo().getResolvedClassInfo(clsName);
278 return getClassObject(env, ci);
282 * this is an example of a native method issuing direct calls - otherwise known
284 * We don't have to deal with class init here anymore, since this is called
285 * via the class object of the class to instantiate
288 public int newInstance____Ljava_lang_Object_2 (MJIEnv env, int robj) {
289 ThreadInfo ti = env.getThreadInfo();
290 DirectCallStackFrame frame = ti.getReturnedDirectCall();
292 ClassInfo ci = env.getReferredClassInfo(robj); // what are we
293 MethodInfo miCtor = ci.getMethod("<init>()V", true); // note there always is one since something needs to call Object()
295 if (frame == null){ // first time around
296 if(ci.isAbstract()){ // not allowed to instantiate
297 env.throwException("java.lang.InstantiationException");
301 // <2do> - still need to handle protected
302 if (miCtor.isPrivate()) {
303 env.throwException("java.lang.IllegalAccessException", "cannot access non-public member of class " + ci.getName());
307 int objRef = env.newObjectOfUncheckedClass(ci); // create the thing
309 frame = miCtor.createDirectCallStackFrame(ti, 1);
310 // note that we don't set this as a reflection call since it is supposed to propagate exceptions
311 frame.setReferenceArgument(0, objRef, null);
312 frame.setLocalReferenceVariable(0, objRef); // (1) store ref for retrieval during re-exec
315 // check if we have to push clinits
316 ci.initializeClass(ti);
318 env.repeatInvocation();
321 } else { // re-execution
322 int objRef = frame.getLocalVariable(0); // that's the object ref we set in (1)
328 public int getSuperclass____Ljava_lang_Class_2 (MJIEnv env, int robj) {
329 ClassInfo ci = env.getReferredClassInfo( robj);
330 ClassInfo sci = ci.getSuperClass();
332 return sci.getClassObjectRef();
338 int getMethod (MJIEnv env, int clsRef, ClassInfo ciMethod, String mname, int argTypesRef,
339 boolean isRecursiveLookup, boolean publicOnly) {
341 ClassInfo ci = env.getReferredClassInfo( clsRef);
343 StringBuilder sb = new StringBuilder(mname);
345 int nParams = argTypesRef != MJIEnv.NULL ? env.getArrayLength(argTypesRef) : 0;
346 for (int i=0; i<nParams; i++) {
347 int cRef = env.getReferenceArrayElement(argTypesRef, i);
348 ClassInfo cit = env.getReferredClassInfo( cRef);
349 String tname = cit.getName();
350 String tcode = tname;
351 tcode = Types.getTypeSignature(tcode, false);
355 String fullMthName = sb.toString();
357 MethodInfo mi = ci.getReflectionMethod(fullMthName, isRecursiveLookup);
358 if (mi == null || (publicOnly && !mi.isPublic())) {
359 env.throwException("java.lang.NoSuchMethodException", ci.getName() + '.' + fullMthName);
363 return createMethodObject(env, ciMethod, mi);
367 int createMethodObject (MJIEnv env, ClassInfo objectCi, MethodInfo mi) {
368 // NOTE - we rely on Constructor and Method peers being initialized
370 return JPF_java_lang_reflect_Constructor.createConstructorObject(env, objectCi, mi);
372 return JPF_java_lang_reflect_Method.createMethodObject(env, objectCi, mi);
377 public int getDeclaredMethod__Ljava_lang_String_2_3Ljava_lang_Class_2__Ljava_lang_reflect_Method_2 (MJIEnv env, int clsRef,
378 int nameRef, int argTypesRef) {
379 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
381 env.repeatInvocation();
385 String mname = env.getStringObject(nameRef);
386 return getMethod(env, clsRef, mci, mname, argTypesRef, false, false);
390 public int getDeclaredConstructor___3Ljava_lang_Class_2__Ljava_lang_reflect_Constructor_2 (MJIEnv env,
393 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
395 env.repeatInvocation();
399 int ctorRef = getMethod(env,clsRef, mci, "<init>",argTypesRef,false, false);
404 public int getMethod__Ljava_lang_String_2_3Ljava_lang_Class_2__Ljava_lang_reflect_Method_2 (MJIEnv env, int clsRef,
405 int nameRef, int argTypesRef) {
406 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
408 env.repeatInvocation();
412 String mname = env.getStringObject(nameRef);
413 return getMethod( env, clsRef, mci, mname, argTypesRef, true, true);
416 private void addDeclaredMethodsRec (boolean includeSuperClasses, HashMap<String,MethodInfo>methods, ClassInfo ci){
418 if (includeSuperClasses){ // do NOT include Object methods for interfaces
419 ClassInfo sci = ci.getSuperClass();
421 addDeclaredMethodsRec( includeSuperClasses, methods,sci);
425 ClassLoaderInfo cl = ci.getClassLoaderInfo();
426 for (String ifcName : ci.getDirectInterfaceNames()){
427 ClassInfo ici = cl.getResolvedClassInfo(ifcName); // has to be already defined, so no exception
428 addDeclaredMethodsRec( includeSuperClasses, methods,ici);
431 for (MethodInfo mi : ci.getDeclaredMethodInfos()) {
432 // filter out non-public, <clinit> and <init>
433 if (mi.isPublic() && (mi.getName().charAt(0) != '<')) {
434 String mname = mi.getUniqueName();
436 if (!(ci.isInterface() && methods.containsKey(mname))){
437 methods.put(mname, mi);
444 public int getMethods_____3Ljava_lang_reflect_Method_2 (MJIEnv env, int objref) {
445 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
447 env.repeatInvocation();
451 ClassInfo ci = env.getReferredClassInfo(objref);
453 // collect all the public, non-ctor instance methods
454 if (!ci.isPrimitive()) {
455 HashMap<String,MethodInfo> methods = new HashMap<String,MethodInfo>();
456 addDeclaredMethodsRec( !ci.isInterface(), methods,ci);
458 int n = methods.size();
459 int aref = env.newObjectArray("Ljava/lang/reflect/Method;", n);
462 for (MethodInfo mi : methods.values()){
463 int mref = createMethodObject(env, mci, mi);
464 env.setReferenceArrayElement(aref,i++,mref);
470 return env.newObjectArray("Ljava/lang/reflect/Method;", 0);
475 public int getDeclaredMethods_____3Ljava_lang_reflect_Method_2 (MJIEnv env, int objref) {
476 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
478 env.repeatInvocation();
482 ClassInfo ci = env.getReferredClassInfo(objref);
483 MethodInfo[] methodInfos = ci.getDeclaredMethodInfos();
485 // we have to filter out the ctors and the static init
486 int nMth = methodInfos.length;
487 for (int i=0; i<methodInfos.length; i++){
488 if (methodInfos[i].getName().charAt(0) == '<'){
489 methodInfos[i] = null;
494 int aref = env.newObjectArray("Ljava/lang/reflect/Method;", nMth);
496 for (int i=0, j=0; i<methodInfos.length; i++) {
497 if (methodInfos[i] != null){
498 int mref = createMethodObject(env, mci, methodInfos[i]);
499 env.setReferenceArrayElement(aref,j++,mref);
506 int getConstructors (MJIEnv env, int objref, boolean publicOnly){
507 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
509 env.repeatInvocation();
513 ClassInfo ci = env.getReferredClassInfo(objref);
514 ArrayList<MethodInfo> ctors = new ArrayList<MethodInfo>();
516 // we have to filter out the ctors and the static init
517 for (MethodInfo mi : ci.getDeclaredMethodInfos()){
518 if (mi.getName().equals("<init>")){
519 if (!publicOnly || mi.isPublic()) {
525 int nCtors = ctors.size();
526 int aref = env.newObjectArray("Ljava/lang/reflect/Constructor;", nCtors);
528 for (int i=0; i<nCtors; i++){
529 env.setReferenceArrayElement(aref, i, createMethodObject(env, mci, ctors.get(i)));
536 public int getConstructors_____3Ljava_lang_reflect_Constructor_2 (MJIEnv env, int objref){
537 return getConstructors(env, objref, true);
541 public int getDeclaredConstructors_____3Ljava_lang_reflect_Constructor_2 (MJIEnv env, int objref){
542 return getConstructors(env, objref, false);
546 public int getConstructor___3Ljava_lang_Class_2__Ljava_lang_reflect_Constructor_2 (MJIEnv env, int clsRef,
548 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
550 env.repeatInvocation();
554 // <2do> should only return a public ctor
555 return getMethod(env,clsRef, mci, "<init>",argTypesRef,false,true);
558 // this is only used for system classes such as java.lang.reflect.Method
559 ClassInfo getInitializedClassInfo (MJIEnv env, String clsName){
560 ThreadInfo ti = env.getThreadInfo();
561 Instruction insn = ti.getPC();
562 ClassInfo ci = ClassLoaderInfo.getSystemResolvedClassInfo( clsName);
564 if (ci.initializeClass(ti)){
572 public void initialize0____V (MJIEnv env, int clsObjRef){
573 ClassInfo ci = env.getReferredClassInfo( clsObjRef);
574 ci.initializeClass(ThreadInfo.currentThread);
577 Set<ClassInfo> getInitializedInterfaces (MJIEnv env, ClassInfo ci){
578 ThreadInfo ti = env.getThreadInfo();
579 Instruction insn = ti.getPC();
581 Set<ClassInfo> ifcs = ci.getAllInterfaceClassInfos();
582 for (ClassInfo ciIfc : ifcs){
583 if (ciIfc.initializeClass(ti)){
591 static int createFieldObject (MJIEnv env, FieldInfo fi, ClassInfo fci){
592 int regIdx = JPF_java_lang_reflect_Field.registerFieldInfo(fi);
594 int eidx = env.newObject(fci);
595 ElementInfo ei = env.getModifiableElementInfo(eidx);
596 ei.setIntField("regIdx", regIdx);
602 public int getDeclaredFields_____3Ljava_lang_reflect_Field_2 (MJIEnv env, int objRef) {
603 ClassInfo fci = getInitializedClassInfo(env, FIELD_CLASSNAME);
605 env.repeatInvocation();
609 ClassInfo ci = env.getReferredClassInfo(objRef);
610 int nInstance = ci.getNumberOfDeclaredInstanceFields();
611 int nStatic = ci.getNumberOfStaticFields();
612 int aref = env.newObjectArray("Ljava/lang/reflect/Field;", nInstance + nStatic);
615 for (i=0; i<nStatic; i++) {
616 FieldInfo fi = ci.getStaticField(i);
617 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
620 for (i=0; i<nInstance; i++) {
621 FieldInfo fi = ci.getDeclaredInstanceField(i);
622 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
629 public int getFields_____3Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef){
630 ClassInfo fci = getInitializedClassInfo(env, FIELD_CLASSNAME);
632 env.repeatInvocation();
636 ClassInfo ci = env.getReferredClassInfo(clsRef);
637 // interfaces might not be initialized yet, so we have to check first
638 Set<ClassInfo> ifcs = getInitializedInterfaces( env, ci);
640 env.repeatInvocation();
644 ArrayList<FieldInfo> fiList = new ArrayList<FieldInfo>();
645 for (; ci != null; ci = ci.getSuperClass()){
646 // the host VM returns them in order of declaration, but the spec says there is no guaranteed order so we keep it simple
647 for (FieldInfo fi : ci.getDeclaredInstanceFields()){
652 for (FieldInfo fi : ci.getDeclaredStaticFields()){
659 for (ClassInfo ciIfc : ifcs){
660 for (FieldInfo fi : ciIfc.getDeclaredStaticFields()){
661 fiList.add(fi); // there are no non-public fields in interfaces
665 int aref = env.newObjectArray("Ljava/lang/reflect/Field;", fiList.size());
667 for (FieldInfo fi : fiList){
668 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
674 int getField (MJIEnv env, int clsRef, int nameRef, boolean isRecursiveLookup) {
675 ClassInfo ci = env.getReferredClassInfo( clsRef);
676 String fname = env.getStringObject(nameRef);
679 if (isRecursiveLookup) {
680 fi = ci.getInstanceField(fname);
682 fi = ci.getStaticField(fname);
685 fi = ci.getDeclaredInstanceField(fname);
687 fi = ci.getDeclaredStaticField(fname);
692 env.throwException("java.lang.NoSuchFieldException", fname);
696 // don't do a Field clinit before we know there is such a field
697 ClassInfo fci = getInitializedClassInfo( env, FIELD_CLASSNAME);
699 env.repeatInvocation();
703 return createFieldObject( env, fi, fci);
708 public int getDeclaredField__Ljava_lang_String_2__Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef, int nameRef) {
709 return getField(env,clsRef,nameRef, false);
713 public int getField__Ljava_lang_String_2__Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef, int nameRef) {
714 return getField(env,clsRef,nameRef, true);
718 public int getModifiers____I (MJIEnv env, int clsRef){
719 ClassInfo ci = env.getReferredClassInfo(clsRef);
720 return ci.getModifiers();
724 public int getEnumConstants_____3Ljava_lang_Object_2 (MJIEnv env, int clsRef){
725 ClassInfo ci = env.getReferredClassInfo(clsRef);
727 if (env.requiresClinitExecution(ci)){
728 env.repeatInvocation();
732 if (ci.getSuperClass().getName().equals("java.lang.Enum")) {
733 ArrayList<FieldInfo> list = new ArrayList<FieldInfo>();
734 String cName = ci.getName();
736 for (FieldInfo fi : ci.getDeclaredStaticFields()) {
737 if (fi.isFinal() && cName.equals(fi.getType())){
742 int aRef = env.newObjectArray(cName, list.size());
743 StaticElementInfo sei = ci.getStaticElementInfo();
745 for (FieldInfo fi : list){
746 env.setReferenceArrayElement( aRef, i++, sei.getReferenceField(fi));
755 public int getInterfaces_____3Ljava_lang_Class_2 (MJIEnv env, int clsRef){
756 ClassInfo ci = env.getReferredClassInfo(clsRef);
757 int aref = MJIEnv.NULL;
758 ThreadInfo ti = env.getThreadInfo();
760 // contrary to the API doc, this only returns the interfaces directly
761 // implemented by this class, not it's bases
762 // <2do> this is not exactly correct, since the interfaces should be ordered
763 Set<ClassInfo> interfaces = ci.getInterfaceClassInfos();
764 aref = env.newObjectArray("Ljava/lang/Class;", interfaces.size());
767 for (ClassInfo ifc: interfaces){
768 env.setReferenceArrayElement(aref, i++, ifc.getClassObjectRef());
776 * <2do> needs to load from the classfile location, NOT the MJIEnv (native) class
778 * @author Sebastian Gfeller (sebastian.gfeller@gmail.com)
779 * @author Tihomir Gvero (tihomir.gvero@gmail.com)
782 public int getByteArrayFromResourceStream__Ljava_lang_String_2___3B(MJIEnv env, int clsRef, int nameRef) {
783 String name = env.getStringObject(nameRef);
785 // <2do> this is not loading from the classfile location! fix it
786 InputStream is = env.getClass().getResourceAsStream(name);
790 // We assume that the entire input stream can be read at the moment,
791 // although this could break.
792 byte[] content = null;
794 content = new byte[is.available()];
796 } catch (IOException e) {
797 throw new RuntimeException(e);
799 // Now if everything worked, the content should be in the byte buffer.
800 // We put this buffer into the JPF VM.
801 return env.newByteArray(content);
805 public int getEnclosingClass____Ljava_lang_Class_2 (MJIEnv env, int clsRef) {
806 ClassInfo ciEncl = env.getReferredClassInfo( clsRef).getEnclosingClassInfo();
812 if (ciEncl.initializeClass(env.getThreadInfo())) {
813 env.repeatInvocation();
817 return ciEncl.getClassObjectRef();
821 public int getDeclaredClasses_____3Ljava_lang_Class_2 (MJIEnv env, int clsRef){
822 ClassInfo ci = env.getReferredClassInfo(clsRef);
823 String[] innerClassNames = ci.getInnerClasses();
824 int aref = MJIEnv.NULL;
825 ThreadInfo ti = env.getThreadInfo();
827 MethodInfo mi = ti.getTopFrame().getPrevious().getMethodInfo();
828 // class of the method that includes the invocation of Class.getDeclaredClasses
829 ClassInfo cls = mi.getClassInfo();
831 // first resolve all the inner classes
832 int length = innerClassNames.length;
833 ClassInfo[] resolvedInnerClass = new ClassInfo[length];
834 for(int i=0; i<length; i++) {
836 resolvedInnerClass[i] = cls.resolveReferencedClass(innerClassNames[i]);
837 } catch(LoadOnJPFRequired lre) {
838 env.repeatInvocation();
843 aref = env.newObjectArray("Ljava/lang/Class;", innerClassNames.length);
844 for (int i=0; i<length; i++){
845 ClassInfo ici = resolvedInnerClass[i];
846 if (!ici.isRegistered()) {
847 ici.registerClass(ti);
849 env.setReferenceArrayElement(aref, i, ici.getClassObjectRef());
855 private String getCanonicalName (ClassInfo ci){
857 String canonicalName = getCanonicalName(ci.getComponentClassInfo());
858 if (canonicalName != null){
859 return canonicalName + "[]";
864 if (isLocalOrAnonymousClass(ci)) {
867 if (ci.getEnclosingClassInfo() == null){
870 String enclosingName = getCanonicalName(ci.getEnclosingClassInfo());
871 if (enclosingName == null){ return null; }
872 return enclosingName + "." + ci.getSimpleName();
877 public int getCanonicalName____Ljava_lang_String_2 (MJIEnv env, int clsRef){
878 ClassInfo ci = env.getReferredClassInfo(clsRef);
879 return env.newString(getCanonicalName(ci));
883 public boolean isAnnotation____Z (MJIEnv env, int clsObjRef){
884 ClassInfo ci = env.getReferredClassInfo(clsObjRef);
885 return (ci.getModifiers() & 0x2000) != 0;
889 public boolean isAnnotationPresent__Ljava_lang_Class_2__Z (MJIEnv env, int clsObjRef, int annoClsObjRef){
890 ClassInfo ci = env.getReferredClassInfo(clsObjRef);
891 ClassInfo ciAnno = env.getReferredClassInfo(annoClsObjRef);
893 return ci.getAnnotation( ciAnno.getName()) != null;
897 public int getDeclaredAnnotations_____3Ljava_lang_annotation_Annotation_2 (MJIEnv env, int robj){
898 ClassInfo ci = env.getReferredClassInfo(robj);
901 return env.newAnnotationProxies(ci.getDeclaredAnnotations());
902 } catch (ClinitRequired x){
903 env.handleClinitRequest(x.getRequiredClassInfo());
909 public int getEnclosingConstructor____Ljava_lang_reflect_Constructor_2 (MJIEnv env, int robj){
910 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
912 env.repeatInvocation();
915 ClassInfo ci = env.getReferredClassInfo(robj);
916 MethodInfo enclosingMethod = ci.getEnclosingMethodInfo();
918 if ((enclosingMethod != null) && enclosingMethod.isCtor()){
919 return createMethodObject(env, mci, enclosingMethod);
925 public int getEnclosingMethod____Ljava_lang_reflect_Method_2 (MJIEnv env, int robj){
926 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
928 env.repeatInvocation();
931 ClassInfo ci = env.getReferredClassInfo(robj);
932 MethodInfo enclosingMethod = ci.getEnclosingMethodInfo();
934 if ((enclosingMethod != null) && !enclosingMethod.isCtor()){
935 return createMethodObject(env, mci, enclosingMethod);
941 public boolean isAnonymousClass____Z (MJIEnv env, int robj){
942 ClassInfo ci = env.getReferredClassInfo(robj);
944 if (ci.getName().contains("$")){
945 cname = ci.getName().substring(ci.getName().lastIndexOf('$') + 1);
947 return (cname == null) ? false : cname.matches("\\d+?");
951 public boolean isEnum____Z (MJIEnv env, int robj){
952 ClassInfo ci = env.getReferredClassInfo(robj);
956 // Similar to getEnclosingClass() except it returns null for the case of
959 public int getDeclaringClass____Ljava_lang_Class_2 (MJIEnv env, int clsRef){
960 ClassInfo ci = env.getReferredClassInfo(clsRef);
961 if (isLocalOrAnonymousClass(ci)){
964 return getEnclosingClass____Ljava_lang_Class_2(env, clsRef);
969 public boolean isLocalClass____Z (MJIEnv env, int robj){
970 ClassInfo ci = env.getReferredClassInfo(robj);
971 return isLocalOrAnonymousClass(ci) && !isAnonymousClass____Z(env, robj);
974 private boolean isLocalOrAnonymousClass (ClassInfo ci){
975 return (ci.getEnclosingMethodInfo() != null);
979 public boolean isMemberClass____Z (MJIEnv env, int robj){
980 ClassInfo ci = env.getReferredClassInfo(robj);
981 return (ci.getEnclosingClassInfo() != null) && !isLocalOrAnonymousClass(ci);
985 * Append the package name prefix of the class represented by robj, if the name is not
986 * absolute. OW, remove leading "/".
989 public int getResolvedName__Ljava_lang_String_2__Ljava_lang_String_2 (MJIEnv env, int robj, int resRef){
990 String rname = env.getStringObject(resRef);
991 ClassInfo ci = env.getReferredClassInfo(robj);
995 if (!rname.startsWith("/")) {
997 while (c.isArray()) {
998 c = c.getComponentClassInfo();
1000 String baseName = c.getName();
1001 int index = baseName.lastIndexOf('.');
1003 rname = baseName.substring(0, index).replace('.', '/')
1007 rname = rname.substring(1);
1010 return env.newString(rname);