-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAnonymousImplementation.java
More file actions
48 lines (40 loc) · 1.04 KB
/
AnonymousImplementation.java
File metadata and controls
48 lines (40 loc) · 1.04 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
package thinkinginjava.typeinfo;
import thinkinginjava.typeinfo.interfacea.A;
import static thinkinginjava.util.Print.*;
// Anonymous inner classes can't hide from reflection.
class AnonymousA {
public static A makeA() {
return new A() {
public void f() {
print("public C.f()");
}
public void g() {
print("public C.g()");
}
void u() {
print("package C.u()");
}
protected void v() {
print("protected C.v()");
}
private void w() {
print("private C.w()");
}
};
}
}
public class AnonymousImplementation {
public static void main(String[] args) throws Exception {
A a = AnonymousA.makeA();
a.f();
System.out.println(a.getClass().getName());
// Reflection still gets into the anonymous class:
HiddenImplementation.callHiddenMethod(a, "g");
HiddenImplementation.callHiddenMethod(a, "u");
HiddenImplementation.callHiddenMethod(a, "v");
HiddenImplementation.callHiddenMethod(a, "w");
}
} /*
* Output: public C.f() AnonymousA$1 public C.g() package C.u() protected C.v()
* private C.w()
*/// :~