forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionTest.java
More file actions
41 lines (35 loc) · 1.16 KB
/
ReflectionTest.java
File metadata and controls
41 lines (35 loc) · 1.16 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
public class ReflectionTest {
public static class TestObject1 {
public TestObject1() { }
}
public static class TestObject2 {
public TestObject2() { }
}
public static class TestObject3 {
public TestObject3() { }
}
public static class TestObject4 {
public TestObject4() { }
}
public static class TestObject4a extends TestObject4 {
public TestObject4a() { }
}
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
// Get class by name
Class.forName("ReflectionTest$TestObject1").newInstance();
// Use classloader
ReflectionTest.class.getClassLoader().loadClass("ReflectionTest$TestObject2").newInstance();
// Store in variable, load from that
Class<?> clazz = Class.forName("ReflectionTest$TestObject3");
clazz.newInstance();
/*
* We cannot determine the class by looking at a String literal, so we should look to the
* type - in this case Class<? extends TestObject4>. We should therefore identify both
* TestObject4 and TestObject4a as live.
*/
getClass4().newInstance();
}
public static Class<? extends TestObject4> getClass4() {
return TestObject4.class;
}
}