-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathClass.java
More file actions
425 lines (325 loc) · 11.8 KB
/
Copy pathClass.java
File metadata and controls
425 lines (325 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
* Copyright (C) 2014, United States Government, as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All rights reserved.
*
* The Java Pathfinder core (jpf-core) platform is licensed under the
* Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package java.lang;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import jdk.internal.loader.BootLoader;
import jdk.internal.reflect.ConstantPool;
import sun.reflect.annotation.AnnotationType;
/**
* MJI model class for java.lang.Class library abstraction
*
* This is a JPF specific version of a system class because we can't use the real,
* platform VM specific version (it's native all over the place, its field
* structure isn't documented, most of its methods are private, hence we can't
* even instantiate it properly).
*
* Note that this class never gets seen by the real VM - it's for JPF's eyes only.
*
* For now, it's only a fragment to test the mechanism, and provide basic
* class.getName() / Class.ForName() support (which is (almost) enough for
* Java assertion support
*/
@SuppressWarnings("unused") // native peer uses
public final class Class<T> implements Serializable, GenericDeclaration, Type, AnnotatedElement {
/** don't use serialVersionUID from JDK 1.1 for interoperability */
private static final long serialVersionUID = 3206093459760846163L + 1;
// we init this on demand (from MJIEnv) since it's not used too often
private static Annotation[] emptyAnnotations; // = new Annotation[0];
private String name;
// set by VM
private transient Module module;
private ClassLoader classLoader;
/**
* search global id of the corresponding ClassInfo, which factors in the classloader
*/
private int nativeId;
/**
* to be set during <clinit> of the corresponding class
*/
private boolean isPrimitive;
transient ClassValue.ClassValueMap classValueMap;
private Class() {}
public native boolean isArray ();
@Override
public native Annotation[] getAnnotations();
@Override
public native <A extends Annotation> A getAnnotation( Class<A> annotationCls);
// those are from Java 6
public native boolean isAnnotation ();
@Override
public native boolean isAnnotationPresent(Class<? extends Annotation> annotationClass);
public native Class<?> getComponentType ();
public native Field[] getFields() throws SecurityException;
public native Field getDeclaredField (String fieldName) throws NoSuchFieldException,
SecurityException;
public native Field[] getDeclaredFields () throws SecurityException;
public native Method getDeclaredMethod (String mthName, Class<?>... paramTypes)
throws NoSuchMethodException, SecurityException;
public native Method getMethod (String mthName, Class<?>... paramTypes)
throws NoSuchMethodException, SecurityException;
public native Method[] getDeclaredMethods () throws SecurityException;
public native Method[] getMethods () throws SecurityException;
public native Constructor<?>[] getDeclaredConstructors() throws SecurityException;
public native Constructor<?>[] getConstructors() throws SecurityException;
private native byte[] getByteArrayFromResourceStream(String name);
public InputStream getResourceAsStream (String name) {
byte[] byteArray = getByteArrayFromResourceStream(name);
if (byteArray == null) return null;
return new ByteArrayInputStream(byteArray);
}
private native String getResolvedName (String rname);
public native String getModuleName();
public native String getModuleNameFromClassFileURL();
public native boolean isJPFClass();
public URL getResource (String rname) {
String resolvedName = getResolvedName(rname);
String moduleName = getModuleName();
if (moduleName != null) {
if(isJPFClass())
resolvedName = "modules/" + moduleName + "/" + resolvedName;
else
resolvedName = moduleName + "/" + resolvedName;
}
return getClassLoader().getResource(resolvedName);
}
/*
public Package getPackage() {
// very very crude version for now, only supports the package name
String pkgName = null;
int idx = name.lastIndexOf('.');
if (idx >=0){
pkgName = name.substring(0,idx);
Package pkg = new Package(pkgName,
"spectitle", "specversion", "specvendor",
"impltitle", "implversion", "implvendor",
null, getClassLoader());
return pkg;
} else { // weird, but there is no Package object for the default package
return null;
}
}
*/
public Package getPackage() {
// very very crude version for now, only supports the package name
if (isPrimitive() || isArray()) {
return null;
}
ClassLoader cl = getClassLoader0();
return cl != null ? cl.definePackage(this)
: BootLoader.definePackage(this);
}
ClassLoader getClassLoader0() { return classLoader; }
public String getPackageName() {
int idx = name.lastIndexOf('.');
if (idx >= 0){
return name.substring(0,idx);
} else {
return "";
}
}
//--- enum support ()
// Java 1.5
public native T[] getEnumConstants();
// Java 6
T[] getEnumConstantsShared() {
return getEnumConstants();
}
// lazy initialized map for field name -> Enum constants
// <2do> we should move this to the native side, since Enum constants don't change
private transient Map<String, T> enumConstantDirectory = null;
// package private helper for Enum.valueOf()
Map<String,T> enumConstantDirectory() {
if (enumConstantDirectory == null) {
Map<String,T> map = new HashMap<String,T>();
T[] ae = getEnumConstants();
for (T e: ae) {
map.put(((Enum)e).name(), e);
}
enumConstantDirectory = map;
}
return enumConstantDirectory;
}
public native Constructor<T> getDeclaredConstructor (Class<?>... paramTypes)
throws NoSuchMethodException, SecurityException;
public native Field getField (String fieldName) throws NoSuchFieldException,
SecurityException;
public native boolean isInstance (Object o);
public native boolean isAssignableFrom (Class<?> clazz);
public native boolean isInterface();
public native Constructor<T> getConstructor (Class<?>... argTypes) throws NoSuchMethodException, SecurityException;
public native int getModifiers();
public native Class<?>[] getInterfaces();
// no use to have a ctor, we can't call it
public String getName () {
return name;
}
public String getSimpleName () {
int idx; // <2do> not really - inner classes?
Class<?> enclosingClass = getEnclosingClass();
if(enclosingClass!=null){
idx = enclosingClass.getName().length();
} else{
idx = name.lastIndexOf('.');
}
return name.substring(idx+1);
}
static native Class<?> getPrimitiveClass (String clsName);
/**
* this one is in JPF reflection land, it's 'native' for us
*/
public static native Class<?> forName (String clsName) throws ClassNotFoundException;
public static Class<?> forName (String clsName, boolean initialize, ClassLoader loader) throws ClassNotFoundException {
Class<?> cls;
if (loader == null){
cls = forName(clsName);
} else {
cls = loader.loadClass(clsName);
}
if (initialize) {
cls.initialize0();
}
return cls;
}
/**
* forces clinit without explicit field or method access
*/
private native void initialize0 ();
public boolean isPrimitive () {
return isPrimitive;
}
public native Class<? super T> getSuperclass ();
@Deprecated(since = "9")
public native T newInstance () throws InstantiationException,
IllegalAccessException;
@Override
public String toString () {
return (isInterface() ? "interface " : "class ") + name;
}
@SuppressWarnings("unchecked")
public T cast(Object o) {
if (o != null && !isInstance(o)) throw new ClassCastException();
return (T) o;
}
@SuppressWarnings("unchecked")
public <U> Class<? extends U> asSubclass(Class<U> clazz) {
if (clazz.isAssignableFrom(this)) {
return (Class<? extends U>) this;
} else {
throw new ClassCastException("" + this + " is not a " + clazz);
}
}
native public boolean desiredAssertionStatus ();
public ClassLoader getClassLoader() {
return classLoader;
}
native ConstantPool getConstantPool();
native void setAnnotationType (AnnotationType at);
native AnnotationType getAnnotationType();
@Override
public TypeVariable<Class<T>>[] getTypeParameters() {
throw new UnsupportedOperationException();
}
public Type getGenericSuperclass() {
throw new UnsupportedOperationException();
}
public Type[] getGenericInterfaces() {
throw new UnsupportedOperationException();
}
public Object[] getSigners() {
throw new UnsupportedOperationException();
}
void setSigners(Object[] signers) {
signers = null; // Get rid of IDE warning
throw new UnsupportedOperationException();
}
public Method getEnclosingMethod() {
throw new UnsupportedOperationException();
}
public Constructor<?> getEnclosingConstructor() {
throw new UnsupportedOperationException();
}
public Class<?> getDeclaringClass() {
throw new UnsupportedOperationException();
}
public native Class<?> getEnclosingClass();
public String getCanonicalName() {
throw new UnsupportedOperationException();
}
public boolean isAnonymousClass() {
throw new UnsupportedOperationException();
}
public boolean isLocalClass() {
throw new UnsupportedOperationException();
}
public boolean isMemberClass() {
throw new UnsupportedOperationException();
}
public Class<?>[] getClasses() {
throw new UnsupportedOperationException();
}
public Class<?>[] getDeclaredClasses() throws SecurityException {
throw new UnsupportedOperationException();
}
public java.security.ProtectionDomain getProtectionDomain() {
throw new UnsupportedOperationException();
}
void setProtectionDomain0(java.security.ProtectionDomain pd) {
pd = null; // Get rid of IDE warning
throw new UnsupportedOperationException();
}
public boolean isEnum() {
throw new UnsupportedOperationException();
}
@Override
public Annotation[] getDeclaredAnnotations() {
throw new UnsupportedOperationException();
}
public boolean isSynthetic (){
final int SYNTHETIC = 0x00001000;
return (getModifiers() & SYNTHETIC) != 0;
}
public Module getModule() {
return module;
}
public Class<?> getNestHost() {
Class<?> host = this;
while (host.getEnclosingClass() != null) {
host = host.getEnclosingClass();
}
return host;
}
public boolean isNestmateOf(Class<?> c) {
if (c == null) {
return false;
}
return this.getNestHost() == c.getNestHost();
}
}