You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SubclassAccessToEnclosingSuperClassPrivate.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import org.aspectj.testing.Tester;
  2. /** @testcase PR#752 PUREJAVA subclass access to enclosing super class private members */
  3. public class SubclassAccessToEnclosingSuperClassPrivate {
  4. public static void main (String[] args) {
  5. ClassLoader loader = Expect.class.getClassLoader(); // load expectations..
  6. // nested classes
  7. new SuperEnclosing.SubNested().doit();
  8. new SuperEnclosing.SubNested.SubSubNested().doit();
  9. new SuperEnclosing.SubNested.SubSubNestedNoExtends().doit();
  10. // inner (non-static nested) classes
  11. SuperEnclosing me = new SuperEnclosing();
  12. me.new SubInner("main").doit(); // run directly
  13. me.runSubInner(); // run indirectly
  14. me.runMethodLocalInnerSubclass();
  15. Tester.checkAllEvents();
  16. }
  17. }
  18. /**
  19. * <pre>
  20. * Access private static and instance method and field from
  21. * - static nested subclass
  22. * - inner (named) member subclass
  23. * - inner method-local subclass
  24. * - inner anonymous subclass
  25. * other variants:
  26. * - more deeply nested - skip over intervening superclass or enclosing class
  27. *
  28. * incorrect compiler error lines flagged as i-c-e
  29. * </pre>
  30. */
  31. class SuperEnclosing {
  32. static private int staticPrivateInt = 42;
  33. private int privateInt = 84;
  34. static private void staticPrivateMethod(String caller) {
  35. Tester.event(caller + " -> SuperEnclosing.staticPrivateMethod()");
  36. }
  37. private void privateMethod(String caller) {
  38. Tester.event(caller + " -> SuperEnclosing.privateMethod()");
  39. }
  40. static class SubNested extends SuperEnclosing {
  41. static void doit() {
  42. String label = "SubNested.run()";
  43. label = label + "(" + staticPrivateInt + ")"; // i-c-e
  44. Tester.event(label);
  45. staticPrivateMethod(label); // i-c-e
  46. }
  47. static class SubSubNestedNoExtends {
  48. static void doit() {
  49. String label = "SubSubNestedNoExtends.run()";
  50. label = label + "(" + staticPrivateInt + ")"; // i-c-e
  51. Tester.event(label);
  52. staticPrivateMethod(label); // i-c-e
  53. }
  54. }
  55. static class SubSubNested extends SuperEnclosing {
  56. static void doit() {
  57. String label = "SubSubNestedNoExtends.run()";
  58. label = label + "(" + staticPrivateInt + ")"; // i-c-e
  59. Tester.event(label);
  60. staticPrivateMethod(label); // i-c-e
  61. }
  62. }
  63. }
  64. void runSubInner() {
  65. new SubInner("Constructed in SuperEnclosing.run()").doit();
  66. }
  67. void runMethodLocalInnerSubclass() {
  68. new SuperEnclosing() {
  69. void doit() {
  70. String label = "runMethodLocalInnerSubclass anonymous SuperEnclosing subclass";
  71. label = label + "(" + staticPrivateInt + ", " + privateInt + ")"; // i-c-e
  72. Tester.event(label);
  73. privateMethod(label); // i-c-e
  74. staticPrivateMethod(label); // i-c-e
  75. }
  76. }.doit();
  77. new SuperEnclosing() {
  78. void doit() {
  79. new SuperEnclosing() {
  80. void doit() {
  81. String label = "runMethodLocalInnerSubclass anonymous SuperEnclosing (inner) subclass";
  82. label = label + "(" + staticPrivateInt + ", " + privateInt + ")"; // i-c-e
  83. Tester.event(label);
  84. privateMethod(label); // i-c-e
  85. staticPrivateMethod(label); // i-c-e
  86. }
  87. }.doit();
  88. }
  89. }.doit();
  90. }
  91. // ---------- non-static inner
  92. class SubInner extends SuperEnclosing {
  93. String name;
  94. SubInner(String name) { this.name = name; }
  95. void doit() {
  96. String label = "SubInner=\"" + name + "\".run() " ;
  97. label = label + "(" + staticPrivateInt + ", " + privateInt + ")"; // i-c-e
  98. Tester.event(label);
  99. privateMethod(label); // i-c-e
  100. staticPrivateMethod(label); // i-c-e
  101. }
  102. class SubSubInnerNoExtends {
  103. void doit() {
  104. String label = "SubSubInnerNoExtends.run()";
  105. label = label + "(" + staticPrivateInt + ", " + privateInt + ")"; // i-c-e
  106. Tester.event(label);
  107. staticPrivateMethod(label); // i-c-e
  108. privateMethod(label); // i-c-e
  109. }
  110. }
  111. class SubSubInner extends SuperEnclosing {
  112. void doit() {
  113. String label = "SubSubInnerNoExtends.run()";
  114. label = label + "(" + staticPrivateInt + ", " + privateInt + ")"; // i-c-e
  115. Tester.event(label);
  116. staticPrivateMethod(label); // i-c-e
  117. privateMethod(label); // i-c-e
  118. }
  119. }
  120. }
  121. }
  122. class Expect {
  123. static {
  124. Tester.expectEvent("SubNested.run()(42)");
  125. Tester.expectEvent("SubNested.run()(42) -> SuperEnclosing.staticPrivateMethod()");
  126. Tester.expectEvent("SubSubNestedNoExtends.run()(42)");
  127. Tester.expectEvent("SubSubNestedNoExtends.run()(42) -> SuperEnclosing.staticPrivateMethod()");
  128. Tester.expectEvent("SubSubNestedNoExtends.run()(42)");
  129. Tester.expectEvent("SubSubNestedNoExtends.run()(42) -> SuperEnclosing.staticPrivateMethod()");
  130. Tester.expectEvent("SubInner=\"main\".run() (42, 84)");
  131. Tester.expectEvent("SubInner=\"main\".run() (42, 84) -> SuperEnclosing.privateMethod()");
  132. Tester.expectEvent("SubInner=\"main\".run() (42, 84) -> SuperEnclosing.staticPrivateMethod()");
  133. Tester.expectEvent("SubInner=\"Constructed in SuperEnclosing.run()\".run() (42, 84)");
  134. Tester.expectEvent("SubInner=\"Constructed in SuperEnclosing.run()\".run() (42, 84) -> SuperEnclosing.privateMethod()");
  135. Tester.expectEvent("SubInner=\"Constructed in SuperEnclosing.run()\".run() (42, 84) -> SuperEnclosing.staticPrivateMethod()");
  136. Tester.expectEvent("runMethodLocalInnerSubclass anonymous SuperEnclosing subclass(42, 84)");
  137. Tester.expectEvent("runMethodLocalInnerSubclass anonymous SuperEnclosing subclass(42, 84) -> SuperEnclosing.privateMethod()");
  138. Tester.expectEvent("runMethodLocalInnerSubclass anonymous SuperEnclosing subclass(42, 84) -> SuperEnclosing.staticPrivateMethod()");
  139. Tester.expectEvent("runMethodLocalInnerSubclass anonymous SuperEnclosing (inner) subclass(42, 84)");
  140. Tester.expectEvent("runMethodLocalInnerSubclass anonymous SuperEnclosing (inner) subclass(42, 84) -> SuperEnclosing.privateMethod()");
  141. Tester.expectEvent("runMethodLocalInnerSubclass anonymous SuperEnclosing (inner) subclass(42, 84) -> SuperEnclosing.staticPrivateMethod()");
  142. }
  143. }