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
113 // TODO: THIS NEEDS TO BE SUBSTITUTED BY THE PROPER METHODS! VERY DIRTY RIGHT NOW!
115 public int getTypeParameters_____3Ljava_lang_reflect_TypeVariable_2 (MJIEnv env, int robj){
116 ClassInfo ci = env.getReferredClassInfo( robj);
117 AnnotationInfo[] ai = ci.getAnnotations();
120 return env.newAnnotationProxies(ai);
121 } catch (ClinitRequired x){
122 env.handleClinitRequest(x.getRequiredClassInfo());
128 public int getAnnotation__Ljava_lang_Class_2__Ljava_lang_annotation_Annotation_2 (MJIEnv env, int robj,
130 ClassInfo ci = env.getReferredClassInfo( robj);
131 ClassInfo aci = env.getReferredClassInfo(annoClsRef);
133 AnnotationInfo ai = ci.getAnnotation(aci.getName());
135 ClassInfo aciProxy = aci.getAnnotationProxy();
138 return env.newAnnotationProxy(aciProxy, ai);
139 } catch (ClinitRequired x){
140 env.handleClinitRequest(x.getRequiredClassInfo());
149 public int getPrimitiveClass__Ljava_lang_String_2__Ljava_lang_Class_2 (MJIEnv env,
150 int rcls, int stringRef) {
151 // we don't really have to check for a valid class name here, since
152 // this is a package default method that just gets called from
153 // the clinit of box classes
154 // note this does NOT return the box class (e.g. java.lang.Integer), which
155 // is a normal, functional class, but a primitive class (e.g. 'int') that
156 // is rather a strange beast (not even Object derived)
158 ClassLoaderInfo scli = env.getSystemClassLoaderInfo(); // this is the one responsible for builtin classes
159 String primClsName = env.getStringObject(stringRef); // always initialized
161 ClassInfo ci = scli.getResolvedClassInfo(primClsName);
162 return ci.getClassObjectRef();
166 public boolean desiredAssertionStatus____Z (MJIEnv env, int robj) {
167 ClassInfo ci = env.getReferredClassInfo(robj);
168 return ci.desiredAssertionStatus();
171 public static int getClassObject (MJIEnv env, ClassInfo ci){
172 ThreadInfo ti = env.getThreadInfo();
173 Instruction insn = ti.getPC();
175 if (ci.initializeClass(ti)){
176 env.repeatInvocation();
180 StaticElementInfo ei = ci.getStaticElementInfo();
181 int ref = ei.getClassObjectRef();
187 public int forName__Ljava_lang_String_2__Ljava_lang_Class_2 (MJIEnv env,
190 if (clsNameRef == MJIEnv.NULL){
191 env.throwException("java.lang.NullPointerException", "no class name provided");
195 String clsName = env.getStringObject(clsNameRef);
197 if (clsName.isEmpty()){
198 env.throwException("java.lang.ClassNotFoundException", "empty class name");
202 ThreadInfo ti = env.getThreadInfo();
203 MethodInfo mi = ti.getTopFrame().getPrevious().getMethodInfo();
204 // class of the method that includes the invocation of Class.forName()
205 ClassInfo cls = mi.getClassInfo();
208 // for array type, the component terminal must be resolved
209 if(clsName.charAt(0)=='[') {
210 name = Types.getComponentTerminal(clsName);
215 // make the classloader of the class including the invocation of
216 // Class.forName() resolve the class with the given name
218 cls.resolveReferencedClass(name);
219 } catch(LoadOnJPFRequired lre) {
220 env.repeatInvocation();
224 // The class obtained here is the same as the resolved one, except
225 // if it represents an array type
226 ClassInfo ci = cls.getClassLoaderInfo().getResolvedClassInfo(clsName);
228 return getClassObject(env, ci);
232 * this is an example of a native method issuing direct calls - otherwise known
234 * We don't have to deal with class init here anymore, since this is called
235 * via the class object of the class to instantiate
238 public int newInstance____Ljava_lang_Object_2 (MJIEnv env, int robj) {
239 ThreadInfo ti = env.getThreadInfo();
240 DirectCallStackFrame frame = ti.getReturnedDirectCall();
242 ClassInfo ci = env.getReferredClassInfo(robj); // what are we
243 MethodInfo miCtor = ci.getMethod("<init>()V", true); // note there always is one since something needs to call Object()
245 if (frame == null){ // first time around
246 if(ci.isAbstract()){ // not allowed to instantiate
247 env.throwException("java.lang.InstantiationException");
251 // <2do> - still need to handle protected
252 if (miCtor.isPrivate()) {
253 env.throwException("java.lang.IllegalAccessException", "cannot access non-public member of class " + ci.getName());
257 int objRef = env.newObjectOfUncheckedClass(ci); // create the thing
259 frame = miCtor.createDirectCallStackFrame(ti, 1);
260 // note that we don't set this as a reflection call since it is supposed to propagate exceptions
261 frame.setReferenceArgument(0, objRef, null);
262 frame.setLocalReferenceVariable(0, objRef); // (1) store ref for retrieval during re-exec
265 // check if we have to push clinits
266 ci.initializeClass(ti);
268 env.repeatInvocation();
271 } else { // re-execution
272 int objRef = frame.getLocalVariable(0); // that's the object ref we set in (1)
278 public int getSuperclass____Ljava_lang_Class_2 (MJIEnv env, int robj) {
279 ClassInfo ci = env.getReferredClassInfo( robj);
280 ClassInfo sci = ci.getSuperClass();
282 return sci.getClassObjectRef();
288 int getMethod (MJIEnv env, int clsRef, ClassInfo ciMethod, String mname, int argTypesRef,
289 boolean isRecursiveLookup, boolean publicOnly) {
291 ClassInfo ci = env.getReferredClassInfo( clsRef);
293 StringBuilder sb = new StringBuilder(mname);
295 int nParams = argTypesRef != MJIEnv.NULL ? env.getArrayLength(argTypesRef) : 0;
296 for (int i=0; i<nParams; i++) {
297 int cRef = env.getReferenceArrayElement(argTypesRef, i);
298 ClassInfo cit = env.getReferredClassInfo( cRef);
299 String tname = cit.getName();
300 String tcode = tname;
301 tcode = Types.getTypeSignature(tcode, false);
305 String fullMthName = sb.toString();
307 MethodInfo mi = ci.getReflectionMethod(fullMthName, isRecursiveLookup);
308 if (mi == null || (publicOnly && !mi.isPublic())) {
309 env.throwException("java.lang.NoSuchMethodException", ci.getName() + '.' + fullMthName);
313 return createMethodObject(env, ciMethod, mi);
317 int createMethodObject (MJIEnv env, ClassInfo objectCi, MethodInfo mi) {
318 // NOTE - we rely on Constructor and Method peers being initialized
320 return JPF_java_lang_reflect_Constructor.createConstructorObject(env, objectCi, mi);
322 return JPF_java_lang_reflect_Method.createMethodObject(env, objectCi, mi);
327 public int getDeclaredMethod__Ljava_lang_String_2_3Ljava_lang_Class_2__Ljava_lang_reflect_Method_2 (MJIEnv env, int clsRef,
328 int nameRef, int argTypesRef) {
329 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
331 env.repeatInvocation();
335 String mname = env.getStringObject(nameRef);
336 return getMethod(env, clsRef, mci, mname, argTypesRef, false, false);
340 public int getDeclaredConstructor___3Ljava_lang_Class_2__Ljava_lang_reflect_Constructor_2 (MJIEnv env,
343 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
345 env.repeatInvocation();
349 int ctorRef = getMethod(env,clsRef, mci, "<init>",argTypesRef,false, false);
354 public int getMethod__Ljava_lang_String_2_3Ljava_lang_Class_2__Ljava_lang_reflect_Method_2 (MJIEnv env, int clsRef,
355 int nameRef, int argTypesRef) {
356 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
358 env.repeatInvocation();
362 String mname = env.getStringObject(nameRef);
363 return getMethod( env, clsRef, mci, mname, argTypesRef, true, true);
366 private void addDeclaredMethodsRec (boolean includeSuperClasses, HashMap<String,MethodInfo>methods, ClassInfo ci){
368 if (includeSuperClasses){ // do NOT include Object methods for interfaces
369 ClassInfo sci = ci.getSuperClass();
371 addDeclaredMethodsRec( includeSuperClasses, methods,sci);
375 ClassLoaderInfo cl = ci.getClassLoaderInfo();
376 for (String ifcName : ci.getDirectInterfaceNames()){
377 ClassInfo ici = cl.getResolvedClassInfo(ifcName); // has to be already defined, so no exception
378 addDeclaredMethodsRec( includeSuperClasses, methods,ici);
381 for (MethodInfo mi : ci.getDeclaredMethodInfos()) {
382 // filter out non-public, <clinit> and <init>
383 if (mi.isPublic() && (mi.getName().charAt(0) != '<')) {
384 String mname = mi.getUniqueName();
386 if (!(ci.isInterface() && methods.containsKey(mname))){
387 methods.put(mname, mi);
394 public int getMethods_____3Ljava_lang_reflect_Method_2 (MJIEnv env, int objref) {
395 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
397 env.repeatInvocation();
401 ClassInfo ci = env.getReferredClassInfo(objref);
403 // collect all the public, non-ctor instance methods
404 if (!ci.isPrimitive()) {
405 HashMap<String,MethodInfo> methods = new HashMap<String,MethodInfo>();
406 addDeclaredMethodsRec( !ci.isInterface(), methods,ci);
408 int n = methods.size();
409 int aref = env.newObjectArray("Ljava/lang/reflect/Method;", n);
412 for (MethodInfo mi : methods.values()){
413 int mref = createMethodObject(env, mci, mi);
414 env.setReferenceArrayElement(aref,i++,mref);
420 return env.newObjectArray("Ljava/lang/reflect/Method;", 0);
425 public int getDeclaredMethods_____3Ljava_lang_reflect_Method_2 (MJIEnv env, int objref) {
426 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
428 env.repeatInvocation();
432 ClassInfo ci = env.getReferredClassInfo(objref);
433 MethodInfo[] methodInfos = ci.getDeclaredMethodInfos();
435 // we have to filter out the ctors and the static init
436 int nMth = methodInfos.length;
437 for (int i=0; i<methodInfos.length; i++){
438 if (methodInfos[i].getName().charAt(0) == '<'){
439 methodInfos[i] = null;
444 int aref = env.newObjectArray("Ljava/lang/reflect/Method;", nMth);
446 for (int i=0, j=0; i<methodInfos.length; i++) {
447 if (methodInfos[i] != null){
448 int mref = createMethodObject(env, mci, methodInfos[i]);
449 env.setReferenceArrayElement(aref,j++,mref);
456 int getConstructors (MJIEnv env, int objref, boolean publicOnly){
457 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
459 env.repeatInvocation();
463 ClassInfo ci = env.getReferredClassInfo(objref);
464 ArrayList<MethodInfo> ctors = new ArrayList<MethodInfo>();
466 // we have to filter out the ctors and the static init
467 for (MethodInfo mi : ci.getDeclaredMethodInfos()){
468 if (mi.getName().equals("<init>")){
469 if (!publicOnly || mi.isPublic()) {
475 int nCtors = ctors.size();
476 int aref = env.newObjectArray("Ljava/lang/reflect/Constructor;", nCtors);
478 for (int i=0; i<nCtors; i++){
479 env.setReferenceArrayElement(aref, i, createMethodObject(env, mci, ctors.get(i)));
486 public int getConstructors_____3Ljava_lang_reflect_Constructor_2 (MJIEnv env, int objref){
487 return getConstructors(env, objref, true);
491 public int getDeclaredConstructors_____3Ljava_lang_reflect_Constructor_2 (MJIEnv env, int objref){
492 return getConstructors(env, objref, false);
496 public int getConstructor___3Ljava_lang_Class_2__Ljava_lang_reflect_Constructor_2 (MJIEnv env, int clsRef,
498 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
500 env.repeatInvocation();
504 // <2do> should only return a public ctor
505 return getMethod(env,clsRef, mci, "<init>",argTypesRef,false,true);
508 // this is only used for system classes such as java.lang.reflect.Method
509 ClassInfo getInitializedClassInfo (MJIEnv env, String clsName){
510 ThreadInfo ti = env.getThreadInfo();
511 Instruction insn = ti.getPC();
512 ClassInfo ci = ClassLoaderInfo.getSystemResolvedClassInfo( clsName);
514 if (ci.initializeClass(ti)){
522 public void initialize0____V (MJIEnv env, int clsObjRef){
523 ClassInfo ci = env.getReferredClassInfo( clsObjRef);
524 ci.initializeClass(ThreadInfo.currentThread);
527 Set<ClassInfo> getInitializedInterfaces (MJIEnv env, ClassInfo ci){
528 ThreadInfo ti = env.getThreadInfo();
529 Instruction insn = ti.getPC();
531 Set<ClassInfo> ifcs = ci.getAllInterfaceClassInfos();
532 for (ClassInfo ciIfc : ifcs){
533 if (ciIfc.initializeClass(ti)){
541 static int createFieldObject (MJIEnv env, FieldInfo fi, ClassInfo fci){
542 int regIdx = JPF_java_lang_reflect_Field.registerFieldInfo(fi);
544 int eidx = env.newObject(fci);
545 ElementInfo ei = env.getModifiableElementInfo(eidx);
546 ei.setIntField("regIdx", regIdx);
552 public int getDeclaredFields_____3Ljava_lang_reflect_Field_2 (MJIEnv env, int objRef) {
553 ClassInfo fci = getInitializedClassInfo(env, FIELD_CLASSNAME);
555 env.repeatInvocation();
559 ClassInfo ci = env.getReferredClassInfo(objRef);
560 int nInstance = ci.getNumberOfDeclaredInstanceFields();
561 int nStatic = ci.getNumberOfStaticFields();
562 int aref = env.newObjectArray("Ljava/lang/reflect/Field;", nInstance + nStatic);
565 for (i=0; i<nStatic; i++) {
566 FieldInfo fi = ci.getStaticField(i);
567 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
570 for (i=0; i<nInstance; i++) {
571 FieldInfo fi = ci.getDeclaredInstanceField(i);
572 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
579 public int getFields_____3Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef){
580 ClassInfo fci = getInitializedClassInfo(env, FIELD_CLASSNAME);
582 env.repeatInvocation();
586 ClassInfo ci = env.getReferredClassInfo(clsRef);
587 // interfaces might not be initialized yet, so we have to check first
588 Set<ClassInfo> ifcs = getInitializedInterfaces( env, ci);
590 env.repeatInvocation();
594 ArrayList<FieldInfo> fiList = new ArrayList<FieldInfo>();
595 for (; ci != null; ci = ci.getSuperClass()){
596 // the host VM returns them in order of declaration, but the spec says there is no guaranteed order so we keep it simple
597 for (FieldInfo fi : ci.getDeclaredInstanceFields()){
602 for (FieldInfo fi : ci.getDeclaredStaticFields()){
609 for (ClassInfo ciIfc : ifcs){
610 for (FieldInfo fi : ciIfc.getDeclaredStaticFields()){
611 fiList.add(fi); // there are no non-public fields in interfaces
615 int aref = env.newObjectArray("Ljava/lang/reflect/Field;", fiList.size());
617 for (FieldInfo fi : fiList){
618 env.setReferenceArrayElement(aref, j++, createFieldObject(env, fi, fci));
624 int getField (MJIEnv env, int clsRef, int nameRef, boolean isRecursiveLookup) {
625 ClassInfo ci = env.getReferredClassInfo( clsRef);
626 String fname = env.getStringObject(nameRef);
629 if (isRecursiveLookup) {
630 fi = ci.getInstanceField(fname);
632 fi = ci.getStaticField(fname);
635 fi = ci.getDeclaredInstanceField(fname);
637 fi = ci.getDeclaredStaticField(fname);
642 env.throwException("java.lang.NoSuchFieldException", fname);
646 // don't do a Field clinit before we know there is such a field
647 ClassInfo fci = getInitializedClassInfo( env, FIELD_CLASSNAME);
649 env.repeatInvocation();
653 return createFieldObject( env, fi, fci);
658 public int getDeclaredField__Ljava_lang_String_2__Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef, int nameRef) {
659 return getField(env,clsRef,nameRef, false);
663 public int getField__Ljava_lang_String_2__Ljava_lang_reflect_Field_2 (MJIEnv env, int clsRef, int nameRef) {
664 return getField(env,clsRef,nameRef, true);
668 public int getModifiers____I (MJIEnv env, int clsRef){
669 ClassInfo ci = env.getReferredClassInfo(clsRef);
670 return ci.getModifiers();
674 public int getEnumConstants_____3Ljava_lang_Object_2 (MJIEnv env, int clsRef){
675 ClassInfo ci = env.getReferredClassInfo(clsRef);
677 if (env.requiresClinitExecution(ci)){
678 env.repeatInvocation();
682 if (ci.getSuperClass().getName().equals("java.lang.Enum")) {
683 ArrayList<FieldInfo> list = new ArrayList<FieldInfo>();
684 String cName = ci.getName();
686 for (FieldInfo fi : ci.getDeclaredStaticFields()) {
687 if (fi.isFinal() && cName.equals(fi.getType())){
692 int aRef = env.newObjectArray(cName, list.size());
693 StaticElementInfo sei = ci.getStaticElementInfo();
695 for (FieldInfo fi : list){
696 env.setReferenceArrayElement( aRef, i++, sei.getReferenceField(fi));
705 public int getInterfaces_____3Ljava_lang_Class_2 (MJIEnv env, int clsRef){
706 ClassInfo ci = env.getReferredClassInfo(clsRef);
707 int aref = MJIEnv.NULL;
708 ThreadInfo ti = env.getThreadInfo();
710 // contrary to the API doc, this only returns the interfaces directly
711 // implemented by this class, not it's bases
712 // <2do> this is not exactly correct, since the interfaces should be ordered
713 Set<ClassInfo> interfaces = ci.getInterfaceClassInfos();
714 aref = env.newObjectArray("Ljava/lang/Class;", interfaces.size());
717 for (ClassInfo ifc: interfaces){
718 env.setReferenceArrayElement(aref, i++, ifc.getClassObjectRef());
726 * <2do> needs to load from the classfile location, NOT the MJIEnv (native) class
728 * @author Sebastian Gfeller (sebastian.gfeller@gmail.com)
729 * @author Tihomir Gvero (tihomir.gvero@gmail.com)
732 public int getByteArrayFromResourceStream__Ljava_lang_String_2___3B(MJIEnv env, int clsRef, int nameRef) {
733 String name = env.getStringObject(nameRef);
735 // <2do> this is not loading from the classfile location! fix it
736 InputStream is = env.getClass().getResourceAsStream(name);
740 // We assume that the entire input stream can be read at the moment,
741 // although this could break.
742 byte[] content = null;
744 content = new byte[is.available()];
746 } catch (IOException e) {
747 throw new RuntimeException(e);
749 // Now if everything worked, the content should be in the byte buffer.
750 // We put this buffer into the JPF VM.
751 return env.newByteArray(content);
755 public int getEnclosingClass____Ljava_lang_Class_2 (MJIEnv env, int clsRef) {
756 ClassInfo ciEncl = env.getReferredClassInfo( clsRef).getEnclosingClassInfo();
762 if (ciEncl.initializeClass(env.getThreadInfo())) {
763 env.repeatInvocation();
767 return ciEncl.getClassObjectRef();
771 public int getDeclaredClasses_____3Ljava_lang_Class_2 (MJIEnv env, int clsRef){
772 ClassInfo ci = env.getReferredClassInfo(clsRef);
773 String[] innerClassNames = ci.getInnerClasses();
774 int aref = MJIEnv.NULL;
775 ThreadInfo ti = env.getThreadInfo();
777 MethodInfo mi = ti.getTopFrame().getPrevious().getMethodInfo();
778 // class of the method that includes the invocation of Class.getDeclaredClasses
779 ClassInfo cls = mi.getClassInfo();
781 // first resolve all the inner classes
782 int length = innerClassNames.length;
783 ClassInfo[] resolvedInnerClass = new ClassInfo[length];
784 for(int i=0; i<length; i++) {
786 resolvedInnerClass[i] = cls.resolveReferencedClass(innerClassNames[i]);
787 } catch(LoadOnJPFRequired lre) {
788 env.repeatInvocation();
793 aref = env.newObjectArray("Ljava/lang/Class;", innerClassNames.length);
794 for (int i=0; i<length; i++){
795 ClassInfo ici = resolvedInnerClass[i];
796 if (!ici.isRegistered()) {
797 ici.registerClass(ti);
799 env.setReferenceArrayElement(aref, i, ici.getClassObjectRef());
805 private String getCanonicalName (ClassInfo ci){
807 String canonicalName = getCanonicalName(ci.getComponentClassInfo());
808 if (canonicalName != null){
809 return canonicalName + "[]";
814 if (isLocalOrAnonymousClass(ci)) {
817 if (ci.getEnclosingClassInfo() == null){
820 String enclosingName = getCanonicalName(ci.getEnclosingClassInfo());
821 if (enclosingName == null){ return null; }
822 return enclosingName + "." + ci.getSimpleName();
827 public int getCanonicalName____Ljava_lang_String_2 (MJIEnv env, int clsRef){
828 ClassInfo ci = env.getReferredClassInfo(clsRef);
829 return env.newString(getCanonicalName(ci));
833 public boolean isAnnotation____Z (MJIEnv env, int clsObjRef){
834 ClassInfo ci = env.getReferredClassInfo(clsObjRef);
835 return (ci.getModifiers() & 0x2000) != 0;
839 public boolean isAnnotationPresent__Ljava_lang_Class_2__Z (MJIEnv env, int clsObjRef, int annoClsObjRef){
840 ClassInfo ci = env.getReferredClassInfo(clsObjRef);
841 ClassInfo ciAnno = env.getReferredClassInfo(annoClsObjRef);
843 return ci.getAnnotation( ciAnno.getName()) != null;
847 public int getDeclaredAnnotations_____3Ljava_lang_annotation_Annotation_2 (MJIEnv env, int robj){
848 ClassInfo ci = env.getReferredClassInfo(robj);
851 return env.newAnnotationProxies(ci.getDeclaredAnnotations());
852 } catch (ClinitRequired x){
853 env.handleClinitRequest(x.getRequiredClassInfo());
859 public int getEnclosingConstructor____Ljava_lang_reflect_Constructor_2 (MJIEnv env, int robj){
860 ClassInfo mci = getInitializedClassInfo(env, CONSTRUCTOR_CLASSNAME);
862 env.repeatInvocation();
865 ClassInfo ci = env.getReferredClassInfo(robj);
866 MethodInfo enclosingMethod = ci.getEnclosingMethodInfo();
868 if ((enclosingMethod != null) && enclosingMethod.isCtor()){
869 return createMethodObject(env, mci, enclosingMethod);
875 public int getEnclosingMethod____Ljava_lang_reflect_Method_2 (MJIEnv env, int robj){
876 ClassInfo mci = getInitializedClassInfo(env, METHOD_CLASSNAME);
878 env.repeatInvocation();
881 ClassInfo ci = env.getReferredClassInfo(robj);
882 MethodInfo enclosingMethod = ci.getEnclosingMethodInfo();
884 if ((enclosingMethod != null) && !enclosingMethod.isCtor()){
885 return createMethodObject(env, mci, enclosingMethod);
891 public boolean isAnonymousClass____Z (MJIEnv env, int robj){
892 ClassInfo ci = env.getReferredClassInfo(robj);
894 if (ci.getName().contains("$")){
895 cname = ci.getName().substring(ci.getName().lastIndexOf('$') + 1);
897 return (cname == null) ? false : cname.matches("\\d+?");
901 public boolean isEnum____Z (MJIEnv env, int robj){
902 ClassInfo ci = env.getReferredClassInfo(robj);
906 // Similar to getEnclosingClass() except it returns null for the case of
909 public int getDeclaringClass____Ljava_lang_Class_2 (MJIEnv env, int clsRef){
910 ClassInfo ci = env.getReferredClassInfo(clsRef);
911 if (isLocalOrAnonymousClass(ci)){
914 return getEnclosingClass____Ljava_lang_Class_2(env, clsRef);
919 public boolean isLocalClass____Z (MJIEnv env, int robj){
920 ClassInfo ci = env.getReferredClassInfo(robj);
921 return isLocalOrAnonymousClass(ci) && !isAnonymousClass____Z(env, robj);
924 private boolean isLocalOrAnonymousClass (ClassInfo ci){
925 return (ci.getEnclosingMethodInfo() != null);
929 public boolean isMemberClass____Z (MJIEnv env, int robj){
930 ClassInfo ci = env.getReferredClassInfo(robj);
931 return (ci.getEnclosingClassInfo() != null) && !isLocalOrAnonymousClass(ci);
935 * Append the package name prefix of the class represented by robj, if the name is not
936 * absolute. OW, remove leading "/".
939 public int getResolvedName__Ljava_lang_String_2__Ljava_lang_String_2 (MJIEnv env, int robj, int resRef){
940 String rname = env.getStringObject(resRef);
941 ClassInfo ci = env.getReferredClassInfo(robj);
945 if (!rname.startsWith("/")) {
947 while (c.isArray()) {
948 c = c.getComponentClassInfo();
950 String baseName = c.getName();
951 int index = baseName.lastIndexOf('.');
953 rname = baseName.substring(0, index).replace('.', '/')
957 rname = rname.substring(1);
960 return env.newString(rname);