-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClass.java
More file actions
474 lines (436 loc) · 17.8 KB
/
Copy pathClass.java
File metadata and controls
474 lines (436 loc) · 17.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.lang;
import java.util.HashMap;
import java.util.Map;
// import sun.reflect.CallerSensitive;
// import sun.reflect.Reflection;
import org.cprover.CProver;
import org.cprover.CProverString;
public final class Class<T> {
private Class() {}
private transient String name;
// TODO: these boolean fields model the internal encoding of classes
// they should be set by the getClass methods of the different classes
private boolean isAnnotation;
private boolean isArray;
private boolean isInterface;
private boolean isSynthetic;
private boolean isLocalClass;
private boolean isMemberClass;
private boolean isEnum;
public String toString() {
return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
+ getName();
}
public String toGenericString() {
if (isPrimitive()) {
return toString();
} else {
StringBuilder sb = new StringBuilder();
// Class modifiers are a superset of interface modifiers
/* TODO: No implementation for modifiers yet
int modifiers = getModifiers() & Modifier.classModifiers();
if (modifiers != 0) {
sb.append(Modifier.toString(modifiers));
sb.append(' ');
}
*/
if (isAnnotation()) {
sb.append('@');
}
if (isInterface()) { // Note: all annotation types are interfaces
sb.append("interface");
} else {
if (isEnum())
sb.append("enum");
else
sb.append("class");
}
sb.append(' ');
sb.append(getName());
/* TODO: No implementation for TypeVariable yet
TypeVariable<?>[] typeparms = getTypeParameters();
if (typeparms.length > 0) {
boolean first = true;
sb.append('<');
for(TypeVariable<?> typeparm: typeparms) {
if (!first)
sb.append(',');
sb.append(typeparm.getTypeName());
first = false;
}
sb.append('>');
}
*/
return sb.toString();
}
}
// TODO: This is a very partial model of the actual behaviour of the Java
// forName method. The goal is to correctly model combinations of forName
// and getName, but precisely following the JDK behaviour is more involved.
public static Class<?> forName(String className) {
Class c=new Class();
c.name=className;
return c;
}
public static Class<?> forName(String name, boolean initialize,
ClassLoader loader)
throws ClassNotFoundException {
return Class.forName(name);
}
public boolean isInstance(Object obj) { return obj.getClass()==this; }
public boolean isInterface() { return isInterface; }
public boolean isArray() { return isArray; }
public boolean isPrimitive() {
// DIFFBLUE MODEL LIBRARY
// We use pointer equality instead of string equality because
// it is more efficient.
// This will only work if the name is defined through a constant literal,
// which should be the case for primitive classes.
return name == "boolean" ||
name == "char" ||
name == "byte" ||
name == "short" ||
name == "int" ||
name == "long" ||
name == "float" ||
name == "double"||
name == "void";
}
public boolean isAnnotation() { return isAnnotation; }
public boolean isSynthetic() { return isSynthetic; }
public boolean isLocalClass() { return isLocalClass; }
public boolean isMemberClass() { return isMemberClass; }
public boolean isAnonymousClass() { return "".equals(getSimpleName()); }
public boolean isEnum() { return isEnum; }
private boolean isLocalOrAnonymousClass() {
return isLocalClass() || isAnonymousClass();
}
public String getName() {
// TODO: this is only for objects, and primitive types and arrays need
// a special treatment
return this.name;
}
/**
* Returns the class loader for the class. Some implementations may use
* null to represent the bootstrap class loader. This method will return
* null in such implementations if this class was loaded by the bootstrap
* class loader.
*
* <p> If a security manager is present, and the caller's class loader is
* not null and the caller's class loader is not the same as or an ancestor of
* the class loader for the class whose class loader is requested, then
* this method calls the security manager's {@code checkPermission}
* method with a {@code RuntimePermission("getClassLoader")}
* permission to ensure it's ok to access the class loader for the class.
*
* <p>If this object
* represents a primitive type or void, null is returned.
*
* @return the class loader that loaded the class or interface
* represented by this object.
* @throws SecurityException
* if a security manager exists and its
* {@code checkPermission} method denies
* access to the class loader for the class.
* @see java.lang.ClassLoader
* @see SecurityManager#checkPermission
* @see java.lang.RuntimePermission
*/
// @CallerSensitive
public ClassLoader getClassLoader() {
// ClassLoader cl = getClassLoader0();
// if (cl == null)
// return null;
// SecurityManager sm = System.getSecurityManager();
// if (sm != null) {
// ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass());
// }
// return cl;
return null;
}
ClassLoader getClassLoader0() {
// DIFFBLUE MODEL LIBRARY The real java.lang.Class stores a
// `private final ClassLoader classLoader` which is initialised by the
// jvm rather than by the constructor of this object.
// TODO test-gen should understand this method natively.
return null;
}
public String getSimpleName() {
/* DIFFBLUE MODEL LIBRARY TODO: No implementation for getComponentType
if (isArray())
return getComponentType().getSimpleName()+"[]";
*/
// DIFFBLUE MODEL LIBRARY: instead of calling getSimpleBinaryName()
// we inline function calls and simplify
String name = getName();
int index = name.lastIndexOf('$');
if(index == -1) { // top level class
return CProverString.substring(name, name.lastIndexOf('.') + 1); // strip the package name
}
else {
// DIFFBLUE MODEL LIBRARY: in the original JDK getSimpleBinary
// looks for "$1", instead we looked for '$' and assume the next
// character will be '1'
CProver.assume(CProverString.charAt(name, index + 1) == '1');
// DIFFBLUE MODEL LIBRARY: $1 should be preceded by a class name
CProver.assume(index >= 1);
// DIFFBLUE MODEL LIBRARY: in the original JDK getSimpleName
// removes the digits that follow
CProver.assume(name.length() > index + 2);
CProver.assume(!isAsciiDigit(CProverString.charAt(name, index + 2)));
return CProverString.substring(name, index + 2);
}
}
private static boolean isAsciiDigit(char c) {
return '0' <= c && c <= '9';
}
public Class getEnclosingClass() {
int index = name.lastIndexOf("$1");
if(index==-1)
{
return null;
}
else
{
String enclosing_name = CProverString.substring(name, 0, index);
return Class.forName(enclosing_name);
}
}
public String getCanonicalName() {
if (isArray()) {
String canonicalName = "";
/* TODO: No implementation for getComponentType yet
String canonicalName = getComponentType().getCanonicalName();
*/
if (canonicalName != null)
return canonicalName + "[]";
else
return null;
}
if (isLocalOrAnonymousClass())
return null;
Class<?> enclosingClass = getEnclosingClass();
if (enclosingClass == null) { // top level class
return getName();
}
else {
String enclosingName = enclosingClass.getCanonicalName();
if (enclosingName == null)
return null;
return enclosingName + "." + getSimpleName();
}
}
private String getSimpleBinaryName() {
Class<?> enclosingClass = getEnclosingClass();
if (enclosingClass == null) // top level class
return null;
// Otherwise, strip the enclosing class' name
try {
return CProverString.substring(getName(), enclosingClass.getName().length());
} catch (IndexOutOfBoundsException ex) {
throw new InternalError("Malformed class name", ex);
}
}
private String resolveName(String name) {
if (name == null) {
return name;
}
if (!name.startsWith("/")) {
Class<?> c = this;
/* TODO: No implementation for getComponentType yet
while (c.isArray()) {
c = c.getComponentType();
}
*/
String baseName = c.getName();
int index = baseName.lastIndexOf('.');
if (index != -1) {
name = CProverString.substring(baseName, 0, index).replace('.', '/')
+"/"+name;
}
} else {
name = CProverString.substring(name, 1);
}
return name;
}
public Class getSuperclass(){
// TODO: here we assume no superclass which may not be correct
return Class.forName(null);
}
public static Class getPrimitiveClass(String s){
if("boolean".equals(s))
return Class.forName("boolean");
if("char".equals(s))
return Class.forName("char");
if("byte".equals(s))
return Class.forName("byte");
if("short".equals(s))
return Class.forName("short");
if("int".equals(s))
return Class.forName("int");
if("long".equals(s))
return Class.forName("long");
if("float".equals(s))
return Class.forName("float");
if("double".equals(s))
return Class.forName("double");
if("void".equals(s))
return Class.forName("void");
// TODO: we should throw an exception but this does not seem to work well
// at the moment, so we will assume it does not happen instead.
// throw new IllegalArgumentException("Not primitive type : " + s);
CProver.assume(false);
return Class.forName("");
}
// This version is nicer for the symbolic execution as it knows how to
// compare integers but not Strings.
// This method should be used instead of the String version whenever
// possible by our models.
// Experimenting with the test booleanValue_Fail, the String version
// takes 8 seconds while the int version takes 3 seconds.
static Class getPrimitiveClass(int i){
if(i==0)
return Class.forName("boolean");
if(i==1)
return Class.forName("char");
if(i==2)
return Class.forName("byte");
if(i==3)
return Class.forName("short");
if(i==4)
return Class.forName("int");
if(i==5)
return Class.forName("long");
if(i==6)
return Class.forName("float");
if(i==7)
return Class.forName("double");
return Class.forName("void");
}
Map<String, T> enumConstantDirectory() {
if (enumConstantDirectory == null) {
T[] universe = getEnumConstantsShared();
if (universe == null)
throw new IllegalArgumentException(
getName() + " is not an enum type");
Map<String, T> m = new HashMap<>(2 * universe.length);
for (T constant : universe)
m.put(((Enum<?>)constant).name(), constant);
enumConstantDirectory = m;
}
return enumConstantDirectory;
}
private volatile transient Map<String, T> enumConstantDirectory = null;
// This method use calls that we cannot model here and
// would probably need to be modeled internally in our tools
T[] getEnumConstantsShared() {
// DIFFBLUE MODEL LIBRARY @TODO: implement this method internally in CBMC
return CProver.nondetWithoutNullForNotModelled();
}
/**
* Returns the assertion status that would be assigned to this
* class if it were to be initialized at the time this method is invoked.
* If this class has had its assertion status set, the most recent
* setting will be returned; otherwise, if any package default assertion
* status pertains to this class, the most recent setting for the most
* specific pertinent package default assertion status is returned;
* otherwise, if this class is not a system class (i.e., it has a
* class loader) its class loader's default assertion status is returned;
* otherwise, the system class default assertion status is returned.
* <p>
* Few programmers will have any need for this method; it is provided
* for the benefit of the JRE itself. (It allows a class to determine at
* the time that it is initialized whether assertions should be enabled.)
* Note that this method is not guaranteed to return the actual
* assertion status that was (or will be) associated with the specified
* class when it was (or will be) initialized.
*
* @return the desired assertion status of the specified class.
* @see java.lang.ClassLoader#setClassAssertionStatus
* @see java.lang.ClassLoader#setPackageAssertionStatus
* @see java.lang.ClassLoader#setDefaultAssertionStatus
* @since 1.4
*/
public boolean desiredAssertionStatus() {
ClassLoader loader = getClassLoader();
// If the loader is null this is a system class, so ask the VM
if (loader == null)
return desiredAssertionStatus0(this);
// If the classloader has been initialized with the assertion
// directives, ask it. Otherwise, ask the VM.
synchronized(loader.assertionLock) {
if (loader.classAssertionStatus != null) {
return loader.desiredAssertionStatus(getName());
}
}
return desiredAssertionStatus0(this);
}
// Retrieves the desired assertion status of this class from the VM
private static boolean desiredAssertionStatus0(Class<?> clazz) {
// DIFFBLUE MODEL LIBRARY This would normally be a native method which
// queries the JVM.
// TODO does this need native handling, or is this acceptable?
return true;
}
// DIFFBLUE MODEL LIBRARY
// This method is called by CBMC just after nondeterministic object creation,
// i.e., the constraints that it specifies are enforced only on objects that
// are passed as an argument to a method, and only at the time when they are
// first created.
// We generally want to make sure that all necessary invariants of the class
// are satisfied, and potentially restrict some fields to speed up test
// generation.
@org.cprover.MustNotThrow
protected void cproverNondetInitialize() {
CProver.assume(name != null);
CProver.assume(enumConstantDirectory == null);
}
// DIFFBLUE MODEL LIBRARY
// This method is called by CBMC to try to set class constants, which can
// avoid the time-consuming process of enumerating over the constant
// dictionary's internal array, when generating the Class object non-
// deterministically.
@org.cprover.MustNotThrow
public void cproverInitializeClassLiteral(
String name,
boolean isAnnotation,
boolean isArray,
boolean isInterface,
boolean isSynthetic,
boolean isLocalClass,
boolean isMemberClass,
boolean isEnum) {
this.name = name;
this.isAnnotation = isAnnotation;
this.isArray = isArray;
this.isInterface = isInterface;
this.isSynthetic = isSynthetic;
this.isLocalClass = isLocalClass;
this.isMemberClass = isMemberClass;
this.isEnum = isEnum;
}
}