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.

InnerClassInPrivilegedAspect.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import org.aspectj.testing.Tester;
  2. /** @testcase PR#555 inner classes of privileged aspects cannot see target class private members */
  3. public class InnerClassInPrivilegedAspect {
  4. public static void main(String[] args) {
  5. Util.start();
  6. Target.main(args);
  7. new Target().toString();
  8. Util.end();
  9. }
  10. }
  11. class Util {
  12. public static void event(String s) {
  13. //System.out.println(s);
  14. Tester.event(s);
  15. }
  16. public static void start() {
  17. // failing
  18. Tester.expectEvent("before runner1");
  19. Tester.expectEvent("before intro1");
  20. Tester.expectEvent("new1");
  21. Tester.expectEvent("around runner1");
  22. Tester.expectEvent("around intro1");
  23. Tester.expectEvent("before instance1");
  24. Tester.expectEvent("around instance1");
  25. Tester.expectEvent("pcd if()1");
  26. Tester.expectEvent("before run1");
  27. Tester.expectEvent("around run1");
  28. // passing
  29. Tester.expectEvent("before pcd if()1");
  30. Tester.expectEvent("before1");
  31. Tester.expectEvent("around1");
  32. }
  33. public static void end() {
  34. Tester.checkAllEvents();
  35. }
  36. }
  37. class Target {
  38. private Target(String foo) {}
  39. private static int privateStaticInt = 1;
  40. private int privateInt = 1;
  41. public static void main(String args[]) { }
  42. }
  43. interface Test {
  44. public boolean t();
  45. }
  46. privileged aspect PrivilegedAspectBefore {
  47. pointcut p() : execution(static void Target.main(..));
  48. static class IfTest {
  49. public boolean t(){ Util.event("pcd if()"+Target.privateStaticInt); return true;}
  50. }
  51. pointcut q() : p()
  52. && if(new IfTest().t());
  53. before() : q() { Util.event("before pcd if()" + Target.privateStaticInt); }
  54. /** @testcase privileged access to target private static variables in introduced member (of anonymous class type)*/
  55. static Runnable Target.runner = new Runnable() {
  56. public void run() { Util.event("before runner"+Target.privateStaticInt); }
  57. };
  58. before() : p() { Target.runner.run(); }
  59. before() : p() {
  60. /** @testcase privileged access to target private static variables in before advice (ok) */
  61. Util.event("before" +Target.privateStaticInt);
  62. }
  63. before() : p() {
  64. /** @testcase privileged access to target private static variables from inner class inside before advice */
  65. Runnable inner = new Runnable() {
  66. public void run() {
  67. Util.event("before run" +Target.privateStaticInt);
  68. }
  69. };
  70. inner.run();
  71. }
  72. /** @testcase privileged access to target private static variables from inner class inside introduced method */
  73. before() : p() { Target.runbefore(); }
  74. static void Target.runbefore() {
  75. Runnable inner = new Runnable() {
  76. public void run() {
  77. Util.event("before intro" +Target.privateStaticInt);
  78. }
  79. };
  80. inner.run();
  81. }
  82. }
  83. /** differs from PrivilegedAspectBefore only in using around advice */
  84. privileged aspect PrivilegedAspectAround {
  85. pointcut p() : execution(static void Target.main(..));
  86. /** @testcase privileged access to target private static variables from inner class in introduced constructor */
  87. Target.new() {
  88. this("hi");
  89. int i = privateStaticInt;
  90. Runnable p = new Runnable() {
  91. public void run() { Util.event("new"+Target.privateStaticInt); }
  92. };
  93. p.run();
  94. }
  95. /** @testcase privileged access to target private static variables in introduced member (of anonymous class type)*/
  96. static Runnable Target.runner2 = new Runnable() {
  97. public void run() { Util.event("around runner"+Target.privateStaticInt); }
  98. };
  99. Object around() : p() { Target.runner2.run(); return proceed();}
  100. Object around() : p() {
  101. /** @testcase privileged access to target private static variables in before advice (ok) */
  102. Util.event("around" +Target.privateStaticInt);
  103. return proceed();
  104. }
  105. Object around() : p() {
  106. /** @testcase privileged access to target private static variables from inner class inside around advice */
  107. Runnable inner = new Runnable() {
  108. public void run() {
  109. Util.event("around run" +Target.privateStaticInt);
  110. }
  111. };
  112. inner.run();
  113. return proceed();
  114. }
  115. Object around() : p() { Target.runaround(); return proceed(); }
  116. /** @testcase privileged access to target private static variables from inner class inside introduced method */
  117. static void Target.runaround() {
  118. Runnable inner = new Runnable() {
  119. public void run() {
  120. Util.event("around intro" +Target.privateStaticInt);
  121. }
  122. };
  123. inner.run();
  124. }
  125. }
  126. privileged aspect PrivilegedAspectAroundNonStatic {
  127. /** @testcase privileged access to target private variables from inner class inside introduced method */
  128. before(Target t) : call(String Object.toString()) && target(t){ t.runbefore2(); }
  129. void Target.runbefore2() {
  130. Runnable inner = new Runnable() {
  131. public void run() {
  132. Util.event("before instance" +privateInt);
  133. }
  134. };
  135. inner.run();
  136. }
  137. /** @testcase privileged access to target private variables from inner class inside introduced method */
  138. Object around(Target t) : call(String Object.toString()) && target(t){ t.runbefore3(); return proceed(t); }
  139. void Target.runbefore3() {
  140. Runnable inner = new Runnable() {
  141. public void run() {
  142. Util.event("around instance" +privateInt);
  143. }
  144. };
  145. inner.run();
  146. }
  147. }