KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > JPF_java_lang_Class


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm;
20
21
22 /**
23  * MJI NativePeer class for java.lang.Class library abstraction
24  */

25 public class JPF_java_lang_Class {
26   public static boolean isArray (MJIEnv env, int robj) {
27     return getReferringClassInfo(env, robj).isArray();
28   }
29
30   public static int getComponentType (MJIEnv env, int robj) {
31     if (isArray(env, robj)) {
32       ClassInfo ci = getReferringClassInfo(env, robj).getComponentClassInfo();
33
34       if (ci != null) {
35         return ci.getClassObjectRef();
36       }
37     }
38
39     return MJIEnv.NULL;
40   }
41
42   public static boolean isInstance__Ljava_lang_Object_2 (MJIEnv env, int robj,
43                                                          int r1) {
44     ElementInfo sei = env.getClassElementInfo(robj);
45     ClassInfo ci = sei.getClassInfo();
46     ClassInfo ciOther = env.getClassInfo(r1);
47
48     return (ciOther.instanceOf(ci.getName()));
49   }
50
51   public static boolean isAssignableFrom__Ljava_lang_Class_2 (MJIEnv env, int rcls,
52                                                               int r1) {
53     ElementInfo sei1 = env.getClassElementInfo(rcls);
54     ClassInfo ci1 = sei1.getClassInfo();
55
56     ElementInfo sei2 = env.getClassElementInfo(r1);
57     ClassInfo ci2 = sei2.getClassInfo();
58     
59     return ci2.instanceOf( ci1.getName());
60   }
61   
62   public static boolean isPrimitiveClass__ (MJIEnv env, int robj) {
63     return false;
64   }
65
66   public static int getPrimitiveClass__Ljava_lang_String_2 (MJIEnv env,
67                                                             int rcls,
68                                                             int stringRef) {
69     String JavaDoc clsName = env.getStringObject(stringRef);
70
71     // we don't really have to check for a valid class name here, since
72
// this is a package default method that just gets called from
73
// the clinit of box classes
74
// note this does NOT return the box class (e.g. java.lang.Integer), which
75
// is a normal, functional class, but a primitive class (e.g. 'int') that
76
// is rather a strange beast (not even Object derived)
77
StaticArea sa = env.getStaticArea();
78     StaticElementInfo ei = sa.get(clsName);
79     int cref = ei.getClassObjectRef();
80     env.setBooleanField(cref, "isPrimitive", true);
81
82     return cref;
83   }
84
85   public static boolean desiredAssertionStatus (MJIEnv env, int robj) {
86     ClassInfo ci = getReferringClassInfo(env,robj);
87     return ci.areAssertionsEnabled();
88   }
89
90   public static int forName__Ljava_lang_String_2 (MJIEnv env, int rcls,
91                                                   int stringRef) {
92     String JavaDoc clsName = env.getStringObject(stringRef);
93     StaticElementInfo ei = env.getStaticArea().get(clsName);
94     int ref = ei.getClassObjectRef();
95
96     return ref;
97   }
98
99   public static int newInstance (MJIEnv env, int robj) {
100     ClassInfo ci = getReferringClassInfo(env,robj); // what are we
101
ThreadInfo ti = env.getThreadInfo();
102     int ref = env.getDynamicArea().newObject(ci, ti); // create the thing
103

104     MethodInfo mi = ci.getMethod("<init>()", true);
105     
106     if (mi != null) {
107       // <2do> that's still overly simplistic - leave alone SecurityManager, we have to deal with
108
// IllegalAccessException - if the class or its nullary constructor is not accessible.
109
// InstantiationException - if this Class represents an abstract class, interface,
110
// array class, a primitive type, or has no nullary ctor
111
// ExceptionInInitializerError - if the initialization provoked by this method fails.
112
//
113
// for now, this mostly serves as an example of how to call back into JPF execution
114
// from native methods, BUT be aware of this is executed atomically - any blocked insn
115
// in the called method, and it locks up with an BlockedInAtomicException
116

117       ti.push(ref, true);
118       ti.executeMethod(mi);
119     }
120     
121     return ref;
122   }
123   
124   public static int getSuperclass (MJIEnv env, int robj) {
125     ClassInfo ci = getReferringClassInfo(env, robj);
126     ClassInfo sci = ci.getSuperClass();
127     if (sci != null) {
128       return sci.getClassObjectRef();
129     } else {
130       return MJIEnv.NULL;
131     }
132   }
133   
134   static ClassInfo getReferringClassInfo (MJIEnv env, int robj) {
135     // this is only the ElementInfo for the java.lang.Class object
136
ElementInfo ei = env.getElementInfo(robj);
137
138     // get the ClassInfo it refers to
139
int idx = env.getIntField(robj, "cref");
140     StaticArea sa = env.getStaticArea();
141     ElementInfo sei = sa.get(idx);
142
143     return sei.getClassInfo();
144   }
145 }
146
Popular Tags