-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathToyTest.java
More file actions
69 lines (59 loc) · 1.34 KB
/
ToyTest.java
File metadata and controls
69 lines (59 loc) · 1.34 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
//: typeinfo/toys/ToyTest.java
// Testing class Class.
package thinkinginjava.typeinfo;
interface HasBatteries {
}
interface Waterproof {
}
interface Shoots {
}
class Toy {
// Comment out the following default constructor
// to see NoSuchMethodError from (*1*)
/*
* Toy() { }
*/
Toy(int i) {
}
}
class FancyToy extends Toy implements HasBatteries, Waterproof, Shoots {
FancyToy() {
super(1);
}
}
public class ToyTest {
public static void print(Object s) {
System.out.println(s);
}
static void printInfo(Class cc) {
print("Class name: " + cc.getName() + " is interface? ["
+ cc.isInterface() + "]");
print("Simple name: " + cc.getSimpleName());
print("Canonical name : " + cc.getCanonicalName());
}
public static void main(String[] args) {
Class c = null;
try {
c = Class.forName("thinkinginjava.typeinfo.FancyToy");
} catch (ClassNotFoundException e) {
print("Can't find FancyToy");
System.exit(1);
}
printInfo(c);
for (Class face : c.getInterfaces())
printInfo(face);
Class up = c.getSuperclass();
Object obj = null;
try {
// Requires default constructor:
obj = up.newInstance();
} catch (InstantiationException e) {
print("Cannot instantiate");
System.exit(1);
} catch (IllegalAccessException e) {
print("Cannot access");
System.exit(1);
}
printInfo(obj.getClass());
}
}