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";
37 // TODO: Fix for Groovy's model-checking
38 static final String TYPEVARIABLE_CLASSNAME = "java.lang.reflect.TypeVariable";
40 public static boolean init (Config conf){
41 // we create Method and Constructor objects, so we better make sure these
42 // classes are initialized (they already might be)
43 JPF_java_lang_reflect_Method.init(conf);
44 JPF_java_lang_reflect_Constructor.init(conf);
49 public boolean isArray____Z (MJIEnv env, int robj) {
50 ClassInfo ci = env.getReferredClassInfo( robj);
55 public int getComponentType____Ljava_lang_Class_2 (MJIEnv env, int robj) {
56 if (isArray____Z(env, robj)) {
57 ThreadInfo ti = env.getThreadInfo();
58 Instruction insn = ti.getPC();
59 ClassInfo ci = env.getReferredClassInfo( robj).getComponentClassInfo();
61 if (ci.initializeClass(ti)){
62 env.repeatInvocation();
66 return ci.getClassObjectRef();
73 public boolean isInstance__Ljava_lang_Object_2__Z (MJIEnv env, int robj,
75 ElementInfo sei = env.getStaticElementInfo(robj);
76 ClassInfo ci = sei.getClassInfo();
77 ClassInfo ciOther = env.getClassInfo(r1);
78 return (ciOther.isInstanceOf(ci));
82 public boolean isInterface____Z (MJIEnv env, int robj){
83 ClassInfo ci = env.getReferredClassInfo( robj);
84 return ci.isInterface();
88 public boolean isAssignableFrom__Ljava_lang_Class_2__Z (MJIEnv env, int rcls,
90 ElementInfo sei1 = env.getStaticElementInfo(rcls);
91 ClassInfo ci1 = sei1.getClassInfo();
93 ElementInfo sei2 = env.getStaticElementInfo(r1);
94 ClassInfo ci2 = sei2.getClassInfo();
96 return ci2.isInstanceOf( ci1);
100 public int getAnnotations_____3Ljava_lang_annotation_Annotation_2 (MJIEnv env, int robj){
101 ClassInfo ci = env.getReferredClassInfo( robj);
102 AnnotationInfo[] ai = ci.getAnnotations();
105 return env.newAnnotationProxies(ai);
106 } catch (ClinitRequired x){
107 env.handleClinitRequest(x.getRequiredClassInfo());
112 // TODO: Fix for Groovy's model-checking
114 public int getTypeParameters_____3Ljava_lang_reflect_TypeVariable_2 (MJIEnv env, int robj){
115 // Get the ClassInfo for this class
116 ClassInfo tci = env.getReferredClassInfo(robj);
117 String[] typeVars = tci.getGenericTypeVariableNames();
119 // Return with null if this is not a generic class
120 if (typeVars.length == 0) {
121 int aRef = env.newObjectArray("Ljava/lang/reflect/TypeVariable;", 0);
124 // Now create a TypeVariableImpl object for every type variable name; get the ClassInfo for TypeVariableImpl
125 ClassLoaderInfo cli = env.getSystemClassLoaderInfo();
126 ClassInfo ci = cli.getResolvedClassInfo("sun.reflect.generics.reflectiveObjects.TypeVariableImpl");
127 int[] var = new int[typeVars.length];
129 for(int i = 0; i < typeVars.length; i++) {
130 int typeVarRef = env.newObject(ci);
131 ElementInfo ei = env.getModifiableElementInfo(typeVarRef);
132 ei.setReferenceField("genericDeclaration", tci.getClassObjectRef());
133 ei.setReferenceField("name", env.newString(typeVars[i]));
136 int aRef = env.newObjectArray("Ljava/lang/reflect/TypeVariable;", typeVars.length);
138 // Set references for every array element
139 for (int i = 0; i < typeVars.length; i++) {
140 env.setReferenceArrayElement(aRef, i, var[i]);
147 public int getAnnotation__Ljava_lang_Class_2__Ljava_lang_annotation_Annotation_2 (MJIEnv env, int robj,
149 ClassInfo ci = env.getReferredClassInfo( robj);
150 ClassInfo aci = env.getReferredClassInfo(annoClsRef);
152 AnnotationInfo ai = ci.getAnnotation(aci.getName());
154 ClassInfo aciProxy = aci.getAnnotationProxy();
157 return env.newAnnotationProxy(aciProxy, ai);
158 } catch (ClinitRequired x){
159 env.handleClinitRequest(x.getRequiredClassInfo());
168 public int getPrimitiveClass__Ljava_lang_String_2__Ljava_lang_Class_2 (MJIEnv env,
169 int rcls, int stringRef) {
170 // we don't really have to check for a valid class name here, since
171 // this is a package default method that just gets called from
172 // the clinit of box classes
173 // note this does NOT return the box class (e.g. java.lang.Integer), which
174 // is a normal, functional class, but a primitive class (e.g. 'int') that
175 // is rather a strange beast (not even Object derived)
177 ClassLoaderInfo scli = env.getSystemClassLoaderInfo(); // this is the one responsible for builtin classes
178 String primClsName = env.getStringObject(stringRef); // always initialized
180 ClassInfo ci = scli.getResolvedClassInfo(primClsName);
181 return ci.getClassObjectRef();
185 public boolean desiredAssertionStatus____Z (MJIEnv env, int robj) {
186 ClassInfo ci = env.getReferredClassInfo(robj);
187 return ci.desiredAssertionStatus();
190 public static int getClassObject (MJIEnv env, ClassInfo ci){
191 ThreadInfo ti = env.getThreadInfo();
192 Instruction insn = ti.getPC();
194 if (ci.initializeClass(ti)){
195 env.repeatInvocation();
199 StaticElementInfo ei = ci.getStaticElementInfo();
200 int ref = ei.getClassObjectRef();
206 public int forName__Ljava_lang_String_2__Ljava_lang_Class_2 (MJIEnv env,
209 if (clsNameRef == MJIEnv.NULL){
210 env.throwException("java.lang.NullPointerException", "no class name provided");
214 String clsName = env.getStringObject(clsNameRef);
216 if (clsName.isEmpty()){
217 env.throwException("java.lang.ClassNotFoundException", "empty class name");
221 ThreadInfo ti = env.getThreadInfo();
222 MethodInfo mi = ti.getTopFrame().getPrevious().getMethodInfo();
223 // class of the method that includes the invocation of Class.forName()
224 ClassInfo cls = mi.getClassInfo();
227 // for array type, the component terminal must be resolved
228 if(clsName.charAt(0)=='[') {
229 name = Types.getComponentTerminal(clsName);
234 // make the classloader of the class including the invocation of
235 // Class.forName() resolve the class with the given name
237 cls.resolveReferencedClass(name);
238 } catch(LoadOnJPFRequired lre) {
239 env.repeatInvocation();
243 // The class obtained here is the same as the resolved one, except
244 // if it represents an array type
245 ClassInfo ci = cls.getClassLoaderInfo().getResolvedClassInfo(clsName);
247 return getClassObject(env, ci);
251 * this is an example of a native method issuing direct calls - otherwise known
253 * We don't have to deal with class init here anymore, since this is called
254 * via the class object of the class to instantiate
257 public int newInstance____Ljava_lang_Object_2 (MJIEnv env, int robj) {
258 ThreadInfo ti = env.getThreadInfo();
259 DirectCallStackFrame frame = ti.getReturnedDirectCall();
261 ClassInfo ci = env.getReferredClassInfo(robj); // what are we
262 MethodInfo miCtor = ci.getMethod("<init>()V", true); // note there always is one since something needs to call Object()
264 if (frame == null){ // first time around
265 if(ci.isAbstract()){ // not allowed to instantiate
266 env.throwException("java.lang.InstantiationException");
270 // <2do> - still need to handle protected
271 if (miCtor.isPrivate()) {
272 env.throwException("java.lang.IllegalAccessException", "cannot access non-public member of class " + ci.getName());
276 int objRef = env.newObjectOfUncheckedClass(ci); // create the thing
278 frame = miCtor.createDirectCallStackFrame(ti, 1);
279 // note that we don't set this as a reflection call since it is supposed to propagate exceptions
280 frame.setReferenceArgument(0, objRef, null);
281 frame.setLocalReferenceVariable(0, objRef); // (1) store ref for retrieval during re-exec
284 // check if we have to push clinits
285 ci.initializeClass(ti);
287 env.repeatInvocation();
290 } else { // re-execution
291 int objRef = frame.getLocalVariable(0); // that's the object ref we set in (1)
297 public int getSuperclass____Ljava_lang_Class_2 (MJIEnv env, int robj) {
298 ClassInfo ci = env.getReferredClassInfo( robj);
299 ClassInfo sci = ci.getSuperClass();
301 return sci.getClassObjectRef();
307 int getMethod (MJIEnv env, int clsRef, ClassInfo ciMethod, String mname, int argTypesRef,
308 boolean isRecursiveLookup, boolean publicOnly) {
310 ClassInfo ci = env.getReferredClassInfo( clsRef);
312 StringBuilder sb = new StringBuilder(mname);
314 int nParams = argTypesRef != MJIEnv.NULL ? env.getArrayLength(argTypesRef) : 0;
315 for (int i=0; i<nParams; i++) {
316 int cRef = env.getReferenceArrayElement(argTypesRef, i);
317 ClassInfo cit = env.getReferredClassInfo( cRef);
318 String tname = cit.getName();
319 String tcode = tname;
320 tcode = Types.getTypeSignature(tcode, false);
324 String fullMthName = sb.toString();
326 MethodInfo mi = ci.getReflectionMethod(fullMthName, isRecursiveLookup);
327 if (mi == null || (publicOnly && !mi.isPublic())) {
328 env.throwException("java.lang.NoSuchMethodException", ci.getName() + '.' + fullMthName);
332 return createMethodObject(env, ciMethod, mi);
336 int createMethodObject (MJIEnv env, ClassInfo objectCi, MethodInfo mi) {
337 // NOTE - we rely on Constructor and Method peers being initialized
339 return JPF_java_lang_reflect_Constructor.createConstructorObject(env, objectCi, mi);
341 return JPF_java_lang_reflect_Method.createMethodObject(env, objectCi, mi);
346 public int getDeclaredMethod__Ljava_lang_String_2_3Ljava_lang_Class_2__Ljava_lang_reflect_Method_2 (MJIEnv env, int clsRef,
347 int nameRef, int argTypesRef) {
348 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
350 env.repeatInvocation();
354 String mname = env.getStringObject(nameRef);
355 return getMethod(env, clsRef, mci, mname, argTypesRef, false, false);
359 public int getDeclaredConstructor___3Ljava_lang_Class_2__Ljava_lang_reflect_Constructor_2 (MJIEnv env,
362 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
364 env.repeatInvocation();
368 int ctorRef = getMethod(env,clsRef, mci, "<init>",argTypesRef,false, false);
373 public int getMethod__Ljava_lang_String_2_3Ljava_lang_Class_2__Ljava_lang_reflect_Method_2 (MJIEnv env, int clsRef,
374 int nameRef, int argTypesRef) {
375 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
377 env.repeatInvocation();
381 String mname = env.getStringObject(nameRef);
382 return getMethod( env, clsRef, mci, mname, argTypesRef, true, true);
385 private void addDeclaredMethodsRec (boolean includeSuperClasses, HashMap<String,MethodInfo>methods, ClassInfo ci){
387 if (includeSuperClasses){ // do NOT include Object methods for interfaces
388 ClassInfo sci = ci.getSuperClass();
390 addDeclaredMethodsRec( includeSuperClasses, methods,sci);
394 ClassLoaderInfo cl = ci.getClassLoaderInfo();
395 for (String ifcName : ci.getDirectInterfaceNames()){
396 ClassInfo ici = cl.getResolvedClassInfo(ifcName); // has to be already defined, so no exception
397 addDeclaredMethodsRec( includeSuperClasses, methods,ici);
400 for (MethodInfo mi : ci.getDeclaredMethodInfos()) {
401 // filter out non-public, <clinit> and <init>
402 if (mi.isPublic() && (mi.getName().charAt(0) != '<')) {
403 String mname = mi.getUniqueName();
405 if (!(ci.isInterface() && methods.containsKey(mname))){
406 methods.put(mname, mi);
413 public int getMethods_____3Ljava_lang_reflect_Method_2 (MJIEnv env, int objref) {
414 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
416 env.repeatInvocation();
420 ClassInfo ci = env.getReferredClassInfo(objref);
422 // collect all the public, non-ctor instance methods
423 if (!ci.isPrimitive()) {
424 HashMap<String,MethodInfo> methods = new HashMap<String,MethodInfo>();
425 addDeclaredMethodsRec( !ci.isInterface(), methods,ci);
427 int n = methods.size();
428 int aref = env.newObjectArray("Ljava/lang/reflect/Method;", n);
431 for (MethodInfo mi : methods.values()){
432 int mref = createMethodObject(env, mci, mi);
433 env.setReferenceArrayElement(aref,i++,mref);
439 return env.newObjectArray("Ljava/lang/reflect/Method;", 0);
444 public int getDeclaredMethods_____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);
452 MethodInfo[] methodInfos = ci.getDeclaredMethodInfos();
454 // we have to filter out the ctors and the static init
455 int nMth = methodInfos.length;
456 for (int i=0; i<methodInfos.length; i++){
457 if (methodInfos[i].getName().charAt(0) == '<'){
458 methodInfos[i] = null;
463 int aref = env.newObjectArray("Ljava/lang/reflect/Method;", nMth);
465 for (int i=0, j=0; i<methodInfos.length; i++) {
466 if (methodInfos[i] != null){
467 int mref = createMethodObject(env, mci, methodInfos[i]);
468 env.setReferenceArrayElement(aref,j++,mref);
475 int getConstructors (MJIEnv env, int objref, boolean publicOnly){
476 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
478 env.repeatInvocation();
482 ClassInfo ci = env.getReferredClassInfo(objref);
483 ArrayList<MethodInfo> ctors = new ArrayList<MethodInfo>();
485 // we have to filter out the ctors and the static init
486 for (MethodInfo mi : ci.getDeclaredMethodInfos()){
487 if (mi.getName().equals("<init>")){
488 if (!publicOnly || mi.isPublic()) {
494 int nCtors = ctors.size();
495 int aref = env.newObjectArray("Ljava/lang/reflect/Constructor;", nCtors);
497 for (int i=0; i<nCtors; i++){
498 env.setReferenceArrayElement(aref, i, createMethodObject(env, mci, ctors.get(i)));
505 public int getConstructors_____3Ljava_lang_reflect_Constructor_2 (MJIEnv env, int objref){
506 return getConstructors(env, objref, true);
510 public int getDeclaredConstructors_____3Ljava_lang_reflect_Constructor_2 (MJIEnv env, int objref){
511 return getConstructors(env, objref, false);
515 public int getConstructor___3Ljava_lang_Class_2__Ljava_lang_reflect_Constructor_2 (MJIEnv env, int clsRef,
517 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
519 env.repeatInvocation();
523 // <2do> should only return a public ctor
524 return getMethod(env,clsRef, mci, "<init>",argTypesRef,false,true);
527 // this is only used for system classes such as java.lang.reflect.Method
528 ClassInfo getInitializedClassInfo (MJIEnv env, String clsName){
529 ThreadInfo ti = env.getThreadInfo();
530 Instruction insn = ti.getPC();
531 ClassInfo ci = ClassLoaderInfo.getSystemResolvedClassInfo( clsName);
533 if (ci.initializeClass(ti)){
541 public void initialize0____V (MJIEnv env, int clsObjRef){
542 ClassInfo ci = env.getReferredClassInfo( clsObjRef);
543 ci.initializeClass(ThreadInfo.currentThread);
546 Set<ClassInfo> getInitializedInterfaces (MJIEnv env, ClassInfo ci){
547 ThreadInfo ti = env.getThreadInfo();
548 Instruction insn = ti.getPC();
550 Set<ClassInfo> ifcs = ci.getAllInterfaceClassInfos();
551 for (ClassInfo ciIfc : ifcs){
552 if (ciIfc.initializeClass(ti)){
560 static int createFieldObject (MJIEnv env, FieldInfo fi, ClassInfo fci){
561 int regIdx = JPF_java_lang_reflect_Field.registerFieldInfo(fi);
563 int eidx = env.newObject(fci);
564 ElementInfo ei = env.getModifiableElementInfo(eidx);
565 ei.setIntField("regIdx", regIdx);
571 public int getDeclaredFields_____3Ljava_lang_reflect_Field_2 (MJIEnv env, int objRef) {
572 ClassInfo fci = getInitializedClassInfo(env, FIELD_CLASSNAME);
574 env.repeatInvocation();
578 ClassInfo ci = env.getReferredClassInfo(objRef);
579 int nInstance = ci.getNumberOfDeclaredInstanceFields();
580 int nStatic = ci.getNumberOfStaticFields();
581 int aref = env.newObjectArray("Ljava/lang/reflect/Field;", nInstance + nStatic);
584 for (i=0; i<nStatic; i++) {
585 FieldInfo fi = ci.getStaticField(i);
586 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
589 for (i=0; i<nInstance; i++) {
590 FieldInfo fi = ci.getDeclaredInstanceField(i);
591 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
598 public int getFields_____3Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef){
599 ClassInfo fci = getInitializedClassInfo(env, FIELD_CLASSNAME);
601 env.repeatInvocation();
605 ClassInfo ci = env.getReferredClassInfo(clsRef);
606 // interfaces might not be initialized yet, so we have to check first
607 Set<ClassInfo> ifcs = getInitializedInterfaces( env, ci);
609 env.repeatInvocation();
613 ArrayList<FieldInfo> fiList = new ArrayList<FieldInfo>();
614 for (; ci != null; ci = ci.getSuperClass()){
615 // the host VM returns them in order of declaration, but the spec says there is no guaranteed order so we keep it simple
616 for (FieldInfo fi : ci.getDeclaredInstanceFields()){
621 for (FieldInfo fi : ci.getDeclaredStaticFields()){
628 for (ClassInfo ciIfc : ifcs){
629 for (FieldInfo fi : ciIfc.getDeclaredStaticFields()){
630 fiList.add(fi); // there are no non-public fields in interfaces
634 int aref = env.newObjectArray("Ljava/lang/reflect/Field;", fiList.size());
636 for (FieldInfo fi : fiList){
637 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
643 int getField (MJIEnv env, int clsRef, int nameRef, boolean isRecursiveLookup) {
644 ClassInfo ci = env.getReferredClassInfo( clsRef);
645 String fname = env.getStringObject(nameRef);
648 if (isRecursiveLookup) {
649 fi = ci.getInstanceField(fname);
651 fi = ci.getStaticField(fname);
654 fi = ci.getDeclaredInstanceField(fname);
656 fi = ci.getDeclaredStaticField(fname);
661 env.throwException("java.lang.NoSuchFieldException", fname);
665 // don't do a Field clinit before we know there is such a field
666 ClassInfo fci = getInitializedClassInfo( env, FIELD_CLASSNAME);
668 env.repeatInvocation();
672 return createFieldObject( env, fi, fci);
677 public int getDeclaredField__Ljava_lang_String_2__Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef, int nameRef) {
678 return getField(env,clsRef,nameRef, false);
682 public int getField__Ljava_lang_String_2__Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef, int nameRef) {
683 return getField(env,clsRef,nameRef, true);
687 public int getModifiers____I (MJIEnv env, int clsRef){
688 ClassInfo ci = env.getReferredClassInfo(clsRef);
689 return ci.getModifiers();
693 public int getEnumConstants_____3Ljava_lang_Object_2 (MJIEnv env, int clsRef){
694 ClassInfo ci = env.getReferredClassInfo(clsRef);
696 if (env.requiresClinitExecution(ci)){
697 env.repeatInvocation();
701 if (ci.getSuperClass().getName().equals("java.lang.Enum")) {
702 ArrayList<FieldInfo> list = new ArrayList<FieldInfo>();
703 String cName = ci.getName();
705 for (FieldInfo fi : ci.getDeclaredStaticFields()) {
706 if (fi.isFinal() && cName.equals(fi.getType())){
711 int aRef = env.newObjectArray(cName, list.size());
712 StaticElementInfo sei = ci.getStaticElementInfo();
714 for (FieldInfo fi : list){
715 env.setReferenceArrayElement( aRef, i++, sei.getReferenceField(fi));
724 public int getInterfaces_____3Ljava_lang_Class_2 (MJIEnv env, int clsRef){
725 ClassInfo ci = env.getReferredClassInfo(clsRef);
726 int aref = MJIEnv.NULL;
727 ThreadInfo ti = env.getThreadInfo();
729 // contrary to the API doc, this only returns the interfaces directly
730 // implemented by this class, not it's bases
731 // <2do> this is not exactly correct, since the interfaces should be ordered
732 Set<ClassInfo> interfaces = ci.getInterfaceClassInfos();
733 aref = env.newObjectArray("Ljava/lang/Class;", interfaces.size());
736 for (ClassInfo ifc: interfaces){
737 env.setReferenceArrayElement(aref, i++, ifc.getClassObjectRef());
745 * <2do> needs to load from the classfile location, NOT the MJIEnv (native) class
747 * @author Sebastian Gfeller (sebastian.gfeller@gmail.com)
748 * @author Tihomir Gvero (tihomir.gvero@gmail.com)
751 public int getByteArrayFromResourceStream__Ljava_lang_String_2___3B(MJIEnv env, int clsRef, int nameRef) {
752 String name = env.getStringObject(nameRef);
754 // <2do> this is not loading from the classfile location! fix it
755 InputStream is = env.getClass().getResourceAsStream(name);
759 // We assume that the entire input stream can be read at the moment,
760 // although this could break.
761 byte[] content = null;
763 content = new byte[is.available()];
765 } catch (IOException e) {
766 throw new RuntimeException(e);
768 // Now if everything worked, the content should be in the byte buffer.
769 // We put this buffer into the JPF VM.
770 return env.newByteArray(content);
774 public int getEnclosingClass____Ljava_lang_Class_2 (MJIEnv env, int clsRef) {
775 ClassInfo ciEncl = env.getReferredClassInfo( clsRef).getEnclosingClassInfo();
781 if (ciEncl.initializeClass(env.getThreadInfo())) {
782 env.repeatInvocation();
786 return ciEncl.getClassObjectRef();
790 public int getDeclaredClasses_____3Ljava_lang_Class_2 (MJIEnv env, int clsRef){
791 ClassInfo ci = env.getReferredClassInfo(clsRef);
792 String[] innerClassNames = ci.getInnerClasses();
793 int aref = MJIEnv.NULL;
794 ThreadInfo ti = env.getThreadInfo();
796 MethodInfo mi = ti.getTopFrame().getPrevious().getMethodInfo();
797 // class of the method that includes the invocation of Class.getDeclaredClasses
798 ClassInfo cls = mi.getClassInfo();
800 // first resolve all the inner classes
801 int length = innerClassNames.length;
802 ClassInfo[] resolvedInnerClass = new ClassInfo[length];
803 for(int i=0; i<length; i++) {
805 resolvedInnerClass[i] = cls.resolveReferencedClass(innerClassNames[i]);
806 } catch(LoadOnJPFRequired lre) {
807 env.repeatInvocation();
812 aref = env.newObjectArray("Ljava/lang/Class;", innerClassNames.length);
813 for (int i=0; i<length; i++){
814 ClassInfo ici = resolvedInnerClass[i];
815 if (!ici.isRegistered()) {
816 ici.registerClass(ti);
818 env.setReferenceArrayElement(aref, i, ici.getClassObjectRef());
824 private String getCanonicalName (ClassInfo ci){
826 String canonicalName = getCanonicalName(ci.getComponentClassInfo());
827 if (canonicalName != null){
828 return canonicalName + "[]";
833 if (isLocalOrAnonymousClass(ci)) {
836 if (ci.getEnclosingClassInfo() == null){
839 String enclosingName = getCanonicalName(ci.getEnclosingClassInfo());
840 if (enclosingName == null){ return null; }
841 return enclosingName + "." + ci.getSimpleName();
846 public int getCanonicalName____Ljava_lang_String_2 (MJIEnv env, int clsRef){
847 ClassInfo ci = env.getReferredClassInfo(clsRef);
848 return env.newString(getCanonicalName(ci));
852 public boolean isAnnotation____Z (MJIEnv env, int clsObjRef){
853 ClassInfo ci = env.getReferredClassInfo(clsObjRef);
854 return (ci.getModifiers() & 0x2000) != 0;
858 public boolean isAnnotationPresent__Ljava_lang_Class_2__Z (MJIEnv env, int clsObjRef, int annoClsObjRef){
859 ClassInfo ci = env.getReferredClassInfo(clsObjRef);
860 ClassInfo ciAnno = env.getReferredClassInfo(annoClsObjRef);
862 return ci.getAnnotation( ciAnno.getName()) != null;
866 public int getDeclaredAnnotations_____3Ljava_lang_annotation_Annotation_2 (MJIEnv env, int robj){
867 ClassInfo ci = env.getReferredClassInfo(robj);
870 return env.newAnnotationProxies(ci.getDeclaredAnnotations());
871 } catch (ClinitRequired x){
872 env.handleClinitRequest(x.getRequiredClassInfo());
878 public int getEnclosingConstructor____Ljava_lang_reflect_Constructor_2 (MJIEnv env, int robj){
879 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
881 env.repeatInvocation();
884 ClassInfo ci = env.getReferredClassInfo(robj);
885 MethodInfo enclosingMethod = ci.getEnclosingMethodInfo();
887 if ((enclosingMethod != null) && enclosingMethod.isCtor()){
888 return createMethodObject(env, mci, enclosingMethod);
894 public int getEnclosingMethod____Ljava_lang_reflect_Method_2 (MJIEnv env, int robj){
895 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
897 env.repeatInvocation();
900 ClassInfo ci = env.getReferredClassInfo(robj);
901 MethodInfo enclosingMethod = ci.getEnclosingMethodInfo();
903 if ((enclosingMethod != null) && !enclosingMethod.isCtor()){
904 return createMethodObject(env, mci, enclosingMethod);
910 public boolean isAnonymousClass____Z (MJIEnv env, int robj){
911 ClassInfo ci = env.getReferredClassInfo(robj);
913 if (ci.getName().contains("$")){
914 cname = ci.getName().substring(ci.getName().lastIndexOf('$') + 1);
916 return (cname == null) ? false : cname.matches("\\d+?");
920 public boolean isEnum____Z (MJIEnv env, int robj){
921 ClassInfo ci = env.getReferredClassInfo(robj);
925 // Similar to getEnclosingClass() except it returns null for the case of
928 public int getDeclaringClass____Ljava_lang_Class_2 (MJIEnv env, int clsRef){
929 ClassInfo ci = env.getReferredClassInfo(clsRef);
930 if (isLocalOrAnonymousClass(ci)){
933 return getEnclosingClass____Ljava_lang_Class_2(env, clsRef);
938 public boolean isLocalClass____Z (MJIEnv env, int robj){
939 ClassInfo ci = env.getReferredClassInfo(robj);
940 return isLocalOrAnonymousClass(ci) && !isAnonymousClass____Z(env, robj);
943 private boolean isLocalOrAnonymousClass (ClassInfo ci){
944 return (ci.getEnclosingMethodInfo() != null);
948 public boolean isMemberClass____Z (MJIEnv env, int robj){
949 ClassInfo ci = env.getReferredClassInfo(robj);
950 return (ci.getEnclosingClassInfo() != null) && !isLocalOrAnonymousClass(ci);
954 * Append the package name prefix of the class represented by robj, if the name is not
955 * absolute. OW, remove leading "/".
958 public int getResolvedName__Ljava_lang_String_2__Ljava_lang_String_2 (MJIEnv env, int robj, int resRef){
959 String rname = env.getStringObject(resRef);
960 ClassInfo ci = env.getReferredClassInfo(robj);
964 if (!rname.startsWith("/")) {
966 while (c.isArray()) {
967 c = c.getComponentClassInfo();
969 String baseName = c.getName();
970 int index = baseName.lastIndexOf('.');
972 rname = baseName.substring(0, index).replace('.', '/')
976 rname = rname.substring(1);
979 return env.newString(rname);