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.

IfPCDAdviceMethods.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import org.aspectj.testing.Tester;
  2. import org.aspectj.testing.Tester;
  3. class TestContext {
  4. public static void signal(String event) {
  5. Tester.event(event);
  6. }
  7. public static void expectSignal(String event) {
  8. Tester.expectEvent(event);
  9. }
  10. public static void startTest() {
  11. }
  12. public static void endTest() {
  13. Tester.checkAllEventsIgnoreDups();
  14. }
  15. public static void testFailed(String failureMessage) {
  16. Tester.check(false,failureMessage);
  17. }
  18. }
  19. class BaseApp {
  20. int i;
  21. int get() { return i; }
  22. void set(int i) { this.i = i; }
  23. void uncountedCall() { }
  24. // permits call to be restricted to within(BaseApp)
  25. void callFromOutside(int i) {
  26. call(i);
  27. }
  28. private void call(int i) { TestContext.signal("call(int)"); }
  29. }
  30. /**
  31. * Test touching pcd if() variants:
  32. * <table>
  33. * <tr><td>pcd if(expr)</td><td>anonymous, named</td></tr>
  34. * <tr><td>pcd combination</td><td>execution, call, callTyped,initialization,set,get</td></tr>
  35. * <tr><td>advice</td><td></td>before, after, around</tr>
  36. * </table>
  37. * (callTyped is probably not a relevant difference).
  38. * Currently passing.
  39. * @author wes
  40. * History
  41. * 8/20/01 initial draft
  42. * 8/21/01 fixed namedIf call, added initializations
  43. * Todo
  44. *
  45. */
  46. public class IfPCDAdviceMethods {
  47. public static void main(String[] args) {
  48. TestContext.startTest();
  49. BaseApp target = new BaseApp();
  50. target.callFromOutside(0);
  51. target.uncountedCall();
  52. target.set(1);
  53. if (!(1 == target.get())) {
  54. TestContext.testFailed("1 != target.get()");
  55. }
  56. TestContext.endTest();
  57. }
  58. static {
  59. // variants of advice defined below
  60. String[] cuts = { "call_pc", "callType_pc", "execution_pc",
  61. "get_pc", "set_pc", "initialization_pc" } ;
  62. String[] kinds = { "before", "after", "around" };
  63. String[] ifs = { "if(true)", "namedIf()", "anonymous" };
  64. for (int i = 0; i < cuts.length; i++) {
  65. for (int j = 0; j < kinds.length; j++) {
  66. for (int k = 0; k < ifs.length; k++) {
  67. TestContext.expectSignal(kinds[j] + "." + cuts[i] + "." + ifs[k]);
  68. }
  69. }
  70. }
  71. // ensure BaseApp method was called
  72. TestContext.expectSignal("call(int)");
  73. // Aspect namedIf delegate should have been called this many times
  74. // (todo: only checks for at-least, not for extra calls)
  75. final int namedIfCalls = 2*cuts.length * (ifs.length-1); // -1 for anonymous, 2* for two calls
  76. for (int i = 0; i < namedIfCalls; i++) {
  77. TestContext.expectSignal("executedNamedIf:"+i);
  78. }
  79. }
  80. }
  81. /** Catch test failures using aspects - mainly to surface caller/join point easily */
  82. aspect TestSignals {
  83. /** identify methods that should never be called */
  84. pointcut errorIfCalled () : call(boolean *..executedNamedIfNever(..));
  85. /** signal failure if method that wasn't supposed to be called is in fact invoked */
  86. after () : errorIfCalled() {
  87. // todo: will StaticPart will always have null for source ?
  88. StringBuffer sb = new StringBuffer();
  89. sb.append("TestSignals.after() : errorIfCalled()");
  90. org.aspectj.lang.JoinPoint.StaticPart sp = thisJoinPointStaticPart;
  91. if (null == sp) {
  92. sb.append("null thisJoinPointStaticPart");
  93. } else {
  94. sb.append(" kind=" + sp.getKind());
  95. sb.append(" signature=" + sp.getSignature());
  96. sb.append(" source=" + sp.getSourceLocation());
  97. }
  98. TestContext.testFailed(sb.toString());
  99. }
  100. }
  101. /** named pointcuts including if(expr) - call, execution only */
  102. aspect Aspect {
  103. static int namedIfCounter;
  104. static int namedIfNeverCounter;
  105. static int i;
  106. static boolean executedNamedIf() {
  107. //System.err.println("named if + " + namedIfCounter);
  108. a("executedNamedIf:"+namedIfCounter++);
  109. return true;
  110. }
  111. static boolean executedNamedIfNever() {
  112. a("executedNamedIfNever:"+namedIfNeverCounter++);
  113. return true;
  114. }
  115. /**
  116. * should not short-circuit, but instead call executedNamedIf
  117. * @testTarget ifpcd.run.expr.java.group no short-circuit of false "or"
  118. * @testTarget ifpcd.run.expr.java.group method invocation in if-expression
  119. */
  120. pointcut namedIf ()
  121. : !within(Aspect) && if( ((1 == 0) || executedNamedIf()) ) ;
  122. /**
  123. * should short-circuit, never calling executedNamedIfNever
  124. * @testTarget ifpcd.run.expr.java.group short-circuit of false "and"
  125. * @testTarget ifpcd.run.expr.java.group assignment in if-expression
  126. */
  127. pointcut namedIfNever () // should short-circuit, never calling executedNamedIfNever
  128. : if( ((1 == 1) && executedNamedIfNever()) ) ;
  129. /**
  130. * @testTarget ifpcd.run.expr.literal literal operations not optimized away fyi
  131. */
  132. pointcut ifTrue () : if(true);
  133. /**
  134. * @testTarget ifpcd.run.expr.literal literal operations not optimized away fyi
  135. */
  136. pointcut ifFalse () : if(false);
  137. // ------------------------------------- pointcuts
  138. /** @testTarget ifpcd.compile.pcds.named.set */
  139. pointcut set_pc (): if (true) && set(int BaseApp.i) ;
  140. /** @testTarget ifpcd.compile.pcds.named.get */
  141. pointcut get_pc (): if (true) && get(int BaseApp.i) ;
  142. /** @testTarget ifpcd.compile.pcds.named.call */
  143. pointcut call_pc (): if (true) && call(void *.call(int)) && within(BaseApp);
  144. /** @testTarget ifpcd.compile.pcds.named.call with Type */
  145. pointcut callType_pc(): if(true) && call(void BaseApp.call(int));
  146. /** @testTarget ifpcd.compile.pcds.named.execution */
  147. pointcut execution_pc(): if(true) && within(BaseApp) && execution(void *(int));
  148. /** @testTarget ifpcd.compile.pcds.named.initialization */
  149. pointcut initialization_pc(): if(true) && initialization(BaseApp.new(..));
  150. // currently broken
  151. /** @testTarget ifpcd.compile.pcds.named.initialization */
  152. //pointcut staticinitialization_pc(): if(true) && staticinitialization(BaseApp);
  153. /** @testTarget ifpcd.compile.pcds.namedIf.set */
  154. pointcut named_set_pc (): namedIf() && set(int BaseApp.i) ;
  155. /** @testTarget ifpcd.compile.pcds.namedIf.get */
  156. pointcut named_get_pc (): namedIf() && get(int BaseApp.i) ;
  157. /** @testTarget ifpcd.compile.pcds.namedIf.call if pcd by name composition */
  158. pointcut named_call_pc() : namedIf() && call(void *.call(int)) && within(BaseApp);
  159. /** @testTarget ifpcd.compile.pcds.namedIf.call with Type, if pcd by name composition */
  160. pointcut named_callType_pc() : namedIf() && call(void BaseApp.call(int)) && within(BaseApp);;
  161. /** @testTarget ifpcd.compile.pcds.namedIf.execution if pcd by name composition */
  162. pointcut named_execution_pc(): namedIf() && execution(void *(int));
  163. /** @testTarget ifpcd.compile.pcds.namedIf.initialization */
  164. pointcut named_initialization_pc(): namedIf() && initialization(BaseApp.new(..));
  165. before(): set_pc () { a("before.set_pc.if(true)"); }
  166. before(): get_pc () { a("before.get_pc.if(true)"); }
  167. before(): call_pc () { a("before.call_pc.if(true)"); }
  168. before(): callType_pc() { a("before.callType_pc.if(true)"); }
  169. before(): execution_pc() { a("before.execution_pc.if(true)"); }
  170. before(): initialization_pc() { a("before.initialization_pc.if(true)"); }
  171. //before(): staticinitialization_pc() { a("before.staticinitialization_pc.if(true)"); }
  172. before(): named_set_pc () { a("before.set_pc.namedIf()"); }
  173. before(): named_get_pc () { a("before.get_pc.namedIf()"); }
  174. before(): named_call_pc () { a("before.call_pc.namedIf()"); }
  175. before(): named_callType_pc() { a("before.callType_pc.namedIf()"); }
  176. before(): named_execution_pc() { a("before.execution_pc.namedIf()"); }
  177. before(): named_initialization_pc() { a("before.initialization_pc.namedIf()"); }
  178. Object around() : set_pc () { a("around.set_pc.if(true)"); return proceed(); }
  179. int around() : get_pc () { a("around.get_pc.if(true)"); return proceed(); }
  180. void around() : call_pc () { a("around.call_pc.if(true)"); proceed(); }
  181. void around() : callType_pc() { a("around.callType_pc.if(true)"); proceed(); }
  182. void around() : execution_pc() { a("around.execution_pc.if(true)"); proceed(); }
  183. void around() : initialization_pc() { a("around.initialization_pc.if(true)"); proceed(); }
  184. Object around() : named_set_pc () { a("around.set_pc.namedIf()"); return proceed(); }
  185. int around() : named_get_pc () { a("around.get_pc.namedIf()"); return proceed(); }
  186. void around() : named_call_pc () { a("around.call_pc.namedIf()"); proceed(); }
  187. void around() : named_callType_pc() { a("around.callType_pc.namedIf()"); proceed(); }
  188. void around() : named_execution_pc() { a("around.execution_pc.namedIf()"); proceed(); }
  189. void around() : named_initialization_pc() { a("around.initialization_pc.namedIf()"); proceed(); }
  190. // ------------------------------------- after
  191. after(): set_pc () { a("after.set_pc.if(true)"); }
  192. after(): get_pc () { a("after.get_pc.if(true)"); }
  193. after(): call_pc () { a("after.call_pc.if(true)"); }
  194. after(): callType_pc() { a("after.callType_pc.if(true)"); }
  195. after(): execution_pc() { a("after.execution_pc.if(true)"); }
  196. after(): initialization_pc() { a("after.initialization_pc.if(true)"); }
  197. //after(): staticinitialization_pc() { a("after.staticinitialization_pc.if(true)"); }
  198. after(): named_set_pc () { a("after.set_pc.namedIf()"); }
  199. after(): named_get_pc () { a("after.get_pc.namedIf()"); }
  200. after(): named_call_pc () { a("after.call_pc.namedIf()"); }
  201. after(): named_callType_pc() { a("after.callType_pc.namedIf()"); }
  202. after(): named_execution_pc() { a("after.execution_pc.namedIf()"); }
  203. after(): named_initialization_pc() { a("after.initialization_pc.namedIf()"); }
  204. static void a(String msg) {
  205. TestContext.signal(msg);
  206. }
  207. }
  208. /** anonymous pointcuts including if(expr) - call, execution only */
  209. aspect Aspect2 {
  210. /** @testTarget ifpcd.compile.pcds.unnamed.set.before */
  211. before() : if(true) && set(int BaseApp.i) {
  212. a("before.set_pc.anonymous");
  213. }
  214. /** @testTarget ifpcd.compile.pcds.unnamed.get.before */
  215. before() : if(true) && get(int BaseApp.i) {
  216. a("before.get_pc.anonymous");
  217. }
  218. /** @testTarget ifpcd.compile.pcds.unnamed.call.before */
  219. before() : if(true) && call(void *.uncountedCall()) {
  220. a("before.call_pc.anonymous");
  221. }
  222. /** @testTarget ifpcd.compile.pcds.unnamed.call.before with Type */
  223. before() : if(true) && call(void BaseApp.uncountedCall()) {
  224. a("before.callType_pc.anonymous");
  225. }
  226. /** @testTarget ifpcd.compile.pcds.unnamed.execution.before */
  227. before() : if(true) && execution(void BaseApp.uncountedCall()) {
  228. a("before.execution_pc.anonymous");
  229. }
  230. /** @testTarget ifpcd.compile.pcds.unnamed.initialization.before */
  231. before() : if(true) && initialization(BaseApp.new(..)) {
  232. a("before.initialization_pc.anonymous");
  233. }
  234. /** @testTarget ifpcd.compile.pcds.unnamed.set.around */
  235. Object around() : if(true) && set(int BaseApp.i) {
  236. a("around.set_pc.anonymous");
  237. return proceed();
  238. }
  239. /** @testTarget ifpcd.compile.pcds.unnamed.get.around */
  240. int around() : if(true) && get(int BaseApp.i) {
  241. a("around.get_pc.anonymous");
  242. return proceed();
  243. }
  244. /** @testTarget ifpcd.compile.pcds.unnamed.call.around */
  245. void around() : if(true) && call(void *.uncountedCall()) {
  246. a("around.call_pc.anonymous");
  247. proceed();
  248. }
  249. /** @testTarget ifpcd.compile.pcds.unnamed.call.around with Type */
  250. void around() : if(true) && call(void BaseApp.uncountedCall()) {
  251. a("around.callType_pc.anonymous");
  252. proceed();
  253. }
  254. /** @testTarget ifpcd.compile.pcds.unnamed.execution.around */
  255. void around() : if(true) && execution(void BaseApp.uncountedCall()) {
  256. a("around.execution_pc.anonymous");
  257. proceed();
  258. }
  259. /** @testTarget ifpcd.compile.pcds.unnamed.initialization.around */
  260. void around() : if(true) && initialization(BaseApp.new(..)) {
  261. a("around.initialization_pc.anonymous");
  262. proceed();
  263. }
  264. /** @testTarget ifpcd.compile.pcds.unnamed.set.after */
  265. after() : if(true) && set(int BaseApp.i) {
  266. a("after.set_pc.anonymous");
  267. }
  268. /** @testTarget ifpcd.compile.pcds.unnamed.get.after */
  269. after() : if(true) && get(int BaseApp.i) {
  270. a("after.get_pc.anonymous");
  271. }
  272. /** @testTarget ifpcd.compile.pcds.unnamed.call.after */
  273. after() : if(true) && call(void *.uncountedCall()) {
  274. a("after.call_pc.anonymous");
  275. }
  276. /** @testTarget ifpcd.compile.pcds.unnamed.call.after with Type */
  277. after() : if(true) && call(void BaseApp.uncountedCall()) {
  278. a("after.callType_pc.anonymous");
  279. }
  280. /** @testTarget ifpcd.compile.pcds.unnamed.execution.after */
  281. after() : if(true) && execution(void BaseApp.uncountedCall()) {
  282. a("after.execution_pc.anonymous");
  283. }
  284. /** @testTarget ifpcd.compile.pcds.unnamed.initialization.after */
  285. after() : if(true) && initialization(BaseApp.new(..)) {
  286. a("after.initialization_pc.anonymous");
  287. }
  288. static void a(String msg) {
  289. TestContext.signal(msg);
  290. }
  291. }