KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PrivateHideTest


1
2 /**
3  * This class tests accesses to private methods and fields, that can be
4  * accessed via a cast.
5  *
6  * The test class has some problems, that no compiler will produce
7  * correct code. When all constructors are declared as private all
8  * compilers won't even compile it.
9  *
10  * I wish there would be a clear document saying what constructs are
11  * allowed and a compiler that would compile them all...
12  */

13 public class PrivateHideTest {
14
15     public static class Test {
16     private String JavaDoc id;
17
18     Test(int val) {
19         id = "Test"+val;
20     }
21
22     private void a(String JavaDoc descr) {
23         System.err.println(descr+" = "+id);
24     }
25
26     class Mid extends Test {
27         private String JavaDoc id;
28
29         Mid(int val) {
30         super(val);
31         id = "Mid"+val;
32         }
33         
34         private void a(String JavaDoc descr) {
35         System.err.println(descr+" = "+id);
36         }
37
38         private Test getTestThis() {
39         return Test.this;
40         }
41
42         class Inner extends Mid {
43         private String JavaDoc id;
44
45         Inner(int val, Test midOuter) {
46             midOuter.super(val);
47             id = "Inner"+val;
48         }
49
50         private void a(String JavaDoc descr) {
51             System.err.println(descr+" = "+id);
52         }
53
54         private void methodTest() {
55             this.a("this");
56             super.a("super");
57             ((Mid) this).a("(Mid) this");
58             ((Test) this).a("(Test) this");
59             Mid.this.a("Mid.this");
60             ((Test) Mid.this).a("(Test) Mid.this");
61             Test.this.a("Test.this");
62             ((Mid) this).getTestThis().a("((Mid) this).getTestThis()");
63             Mid.this.getTestThis().a("Mid.this.getTestThis()");
64         }
65
66
67         private void fieldTest() {
68             System.err.println("this = "+this.id);
69             System.err.println("(Mid) this = " + ((Mid) this).id);
70             System.err.println("(Test) this = " + ((Test) this).id);
71             System.err.println("Mid.this = " + Mid.this.id);
72             System.err.println("(Test) Mid.this = " + ((Test) Mid.this).id);
73             System.err.println("Test.this = " + Test.this.id);
74             System.err.println("((Mid) this).getTestThis() = " + ((Mid)this).getTestThis().id);
75             System.err.println("Mid.this.getTestThis() = " + Mid.this.getTestThis().id);
76         }
77         }
78     }
79     }
80     
81     public static void main(String JavaDoc[] argv) {
82     Test.Mid.Inner inner =
83         new Test(1).new Mid(2).new Inner(3, new Test(4).new Mid(5));
84     inner.methodTest();
85     System.err.println("--");
86     inner.fieldTest();
87     ((Test.Mid)inner).a("(Test.Mid) inner");
88     }
89 }
90
Popular Tags