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

21 yıl önce
21 yıl önce
21 yıl önce
21 yıl önce
21 yıl önce
21 yıl önce
21 yıl önce
21 yıl önce
21 yıl önce
21 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. //XXX no around on initialization yet
  68. if (kinds[j].equals("around") && cuts[i].equals("initialization_pc")) continue;
  69. TestContext.expectSignal(kinds[j] + "." + cuts[i] + "." + ifs[k]);
  70. }
  71. }
  72. }
  73. // ensure BaseApp method was called
  74. TestContext.expectSignal("call(int)");
  75. // Aspect namedIf delegate should have been called this many times
  76. // (todo: only checks for at-least, not for extra calls)
  77. // -1 for anonymous, 2* for two calls, -1 for around initialization
  78. final int namedIfCalls = 2*cuts.length * (ifs.length-1) - 1;
  79. for (int i = 0; i < namedIfCalls; i++) {
  80. TestContext.expectSignal("executedNamedIf:"+i);
  81. }
  82. }
  83. }
  84. /** Catch test failures using aspects - mainly to surface caller/join point easily */
  85. aspect TestSignals {
  86. /** identify methods that should never be called */
  87. pointcut errorIfCalled () : call(boolean *..executedNamedIfNever(..));
  88. /** signal failure if method that wasn't supposed to be called is in fact invoked */
  89. after () : errorIfCalled() {
  90. // todo: will StaticPart will always have null for source ?
  91. StringBuffer sb = new StringBuffer();
  92. sb.append("TestSignals.after() : errorIfCalled()");
  93. org.aspectj.lang.JoinPoint.StaticPart sp = thisJoinPointStaticPart;
  94. if (null == sp) {
  95. sb.append("null thisJoinPointStaticPart");
  96. } else {
  97. sb.append(" kind=" + sp.getKind());
  98. sb.append(" signature=" + sp.getSignature());
  99. sb.append(" source=" + sp.getSourceLocation());
  100. }
  101. TestContext.testFailed(sb.toString());
  102. }
  103. }
  104. /** named pointcuts including if(expr) - call, execution only */
  105. aspect Aspect {
  106. static int namedIfCounter;
  107. static int namedIfNeverCounter;
  108. static int i;
  109. static boolean executedNamedIf() {
  110. //System.err.println("named if + " + namedIfCounter);
  111. a("executedNamedIf:"+namedIfCounter++);
  112. return true;
  113. }
  114. static boolean executedNamedIfNever() {
  115. a("executedNamedIfNever:"+namedIfNeverCounter++);
  116. return true;
  117. }
  118. /**
  119. * should not short-circuit, but instead call executedNamedIf
  120. * @testTarget ifpcd.run.expr.java.group no short-circuit of false "or"
  121. * @testTarget ifpcd.run.expr.java.group method invocation in if-expression
  122. */
  123. pointcut namedIf ()
  124. : !within(Aspect) && if( ((1 == 0) || executedNamedIf()) ) ;
  125. /**
  126. * should short-circuit, never calling executedNamedIfNever
  127. * @testTarget ifpcd.run.expr.java.group short-circuit of false "and"
  128. * @testTarget ifpcd.run.expr.java.group assignment in if-expression
  129. */
  130. pointcut namedIfNever () // should short-circuit, never calling executedNamedIfNever
  131. : if( ((1 == 1) && executedNamedIfNever()) ) ;
  132. /**
  133. * @testTarget ifpcd.run.expr.literal literal operations not optimized away fyi
  134. */
  135. pointcut ifTrue () : if(true);
  136. /**
  137. * @testTarget ifpcd.run.expr.literal literal operations not optimized away fyi
  138. */
  139. pointcut ifFalse () : if(false);
  140. // ------------------------------------- pointcuts
  141. /** @testTarget ifpcd.compile.pcds.named.set */
  142. pointcut set_pc (): if (true) && set(int BaseApp.i) ;
  143. /** @testTarget ifpcd.compile.pcds.named.get */
  144. pointcut get_pc (): if (true) && get(int BaseApp.i) ;
  145. /** @testTarget ifpcd.compile.pcds.named.call */
  146. pointcut call_pc (): if (true) && call(void *.call(int)) && within(BaseApp);
  147. /** @testTarget ifpcd.compile.pcds.named.call with Type */
  148. pointcut callType_pc(): if(true) && call(void BaseApp.call(int));
  149. /** @testTarget ifpcd.compile.pcds.named.execution */
  150. pointcut execution_pc(): if(true) && within(BaseApp) && execution(void *(int));
  151. /** @testTarget ifpcd.compile.pcds.named.initialization */
  152. pointcut initialization_pc(): if(true) && initialization(BaseApp.new(..));
  153. // currently broken
  154. /** @testTarget ifpcd.compile.pcds.named.initialization */
  155. //pointcut staticinitialization_pc(): if(true) && staticinitialization(BaseApp);
  156. /** @testTarget ifpcd.compile.pcds.namedIf.set */
  157. pointcut named_set_pc (): namedIf() && set(int BaseApp.i) ;
  158. /** @testTarget ifpcd.compile.pcds.namedIf.get */
  159. pointcut named_get_pc (): namedIf() && get(int BaseApp.i) ;
  160. /** @testTarget ifpcd.compile.pcds.namedIf.call if pcd by name composition */
  161. pointcut named_call_pc() : namedIf() && call(void *.call(int)) && within(BaseApp);
  162. /** @testTarget ifpcd.compile.pcds.namedIf.call with Type, if pcd by name composition */
  163. pointcut named_callType_pc() : namedIf() && call(void BaseApp.call(int)) && within(BaseApp);;
  164. /** @testTarget ifpcd.compile.pcds.namedIf.execution if pcd by name composition */
  165. pointcut named_execution_pc(): namedIf() && execution(void *(int));
  166. /** @testTarget ifpcd.compile.pcds.namedIf.initialization */
  167. pointcut named_initialization_pc(): namedIf() && initialization(BaseApp.new(..));
  168. before(): set_pc () { a("before.set_pc.if(true)"); }
  169. before(): get_pc () { a("before.get_pc.if(true)"); }
  170. before(): call_pc () { a("before.call_pc.if(true)"); }
  171. before(): callType_pc() { a("before.callType_pc.if(true)"); }
  172. before(): execution_pc() { a("before.execution_pc.if(true)"); }
  173. before(): initialization_pc() { a("before.initialization_pc.if(true)"); }
  174. //before(): staticinitialization_pc() { a("before.staticinitialization_pc.if(true)"); }
  175. before(): named_set_pc () { a("before.set_pc.namedIf()"); }
  176. before(): named_get_pc () { a("before.get_pc.namedIf()"); }
  177. before(): named_call_pc () { a("before.call_pc.namedIf()"); }
  178. before(): named_callType_pc() { a("before.callType_pc.namedIf()"); }
  179. before(): named_execution_pc() { a("before.execution_pc.namedIf()"); }
  180. before(): named_initialization_pc() { a("before.initialization_pc.namedIf()"); }
  181. Object around() : set_pc () { a("around.set_pc.if(true)"); return proceed(); }
  182. int around() : get_pc () { a("around.get_pc.if(true)"); return proceed(); }
  183. void around() : call_pc () { a("around.call_pc.if(true)"); proceed(); }
  184. void around() : callType_pc() { a("around.callType_pc.if(true)"); proceed(); }
  185. void around() : execution_pc() { a("around.execution_pc.if(true)"); proceed(); }
  186. //XXXvoid around() : initialization_pc() { a("around.initialization_pc.if(true)"); proceed(); }
  187. Object around() : named_set_pc () { a("around.set_pc.namedIf()"); return proceed(); }
  188. int around() : named_get_pc () { a("around.get_pc.namedIf()"); return proceed(); }
  189. void around() : named_call_pc () { a("around.call_pc.namedIf()"); proceed(); }
  190. void around() : named_callType_pc() { a("around.callType_pc.namedIf()"); proceed(); }
  191. void around() : named_execution_pc() { a("around.execution_pc.namedIf()"); proceed(); }
  192. //XXXvoid around() : named_initialization_pc() { a("around.initialization_pc.namedIf()"); proceed(); }
  193. // ------------------------------------- after
  194. after(): set_pc () { a("after.set_pc.if(true)"); }
  195. after(): get_pc () { a("after.get_pc.if(true)"); }
  196. after(): call_pc () { a("after.call_pc.if(true)"); }
  197. after(): callType_pc() { a("after.callType_pc.if(true)"); }
  198. after(): execution_pc() { a("after.execution_pc.if(true)"); }
  199. after(): initialization_pc() { a("after.initialization_pc.if(true)"); }
  200. //after(): staticinitialization_pc() { a("after.staticinitialization_pc.if(true)"); }
  201. after(): named_set_pc () { a("after.set_pc.namedIf()"); }
  202. after(): named_get_pc () { a("after.get_pc.namedIf()"); }
  203. after(): named_call_pc () { a("after.call_pc.namedIf()"); }
  204. after(): named_callType_pc() { a("after.callType_pc.namedIf()"); }
  205. after(): named_execution_pc() { a("after.execution_pc.namedIf()"); }
  206. after(): named_initialization_pc() { a("after.initialization_pc.namedIf()"); }
  207. static void a(String msg) {
  208. TestContext.signal(msg);
  209. }
  210. }
  211. /** anonymous pointcuts including if(expr) - call, execution only */
  212. aspect Aspect2 {
  213. /** @testTarget ifpcd.compile.pcds.unnamed.set.before */
  214. before() : if(true) && set(int BaseApp.i) {
  215. a("before.set_pc.anonymous");
  216. }
  217. /** @testTarget ifpcd.compile.pcds.unnamed.get.before */
  218. before() : if(true) && get(int BaseApp.i) {
  219. a("before.get_pc.anonymous");
  220. }
  221. /** @testTarget ifpcd.compile.pcds.unnamed.call.before */
  222. before() : if(true) && call(void *.uncountedCall()) {
  223. a("before.call_pc.anonymous");
  224. }
  225. /** @testTarget ifpcd.compile.pcds.unnamed.call.before with Type */
  226. before() : if(true) && call(void BaseApp.uncountedCall()) {
  227. a("before.callType_pc.anonymous");
  228. }
  229. /** @testTarget ifpcd.compile.pcds.unnamed.execution.before */
  230. before() : if(true) && execution(void BaseApp.uncountedCall()) {
  231. a("before.execution_pc.anonymous");
  232. }
  233. /** @testTarget ifpcd.compile.pcds.unnamed.initialization.before */
  234. before() : if(true) && initialization(BaseApp.new(..)) {
  235. a("before.initialization_pc.anonymous");
  236. }
  237. /** @testTarget ifpcd.compile.pcds.unnamed.set.around */
  238. Object around() : if(true) && set(int BaseApp.i) {
  239. a("around.set_pc.anonymous");
  240. return proceed();
  241. }
  242. /** @testTarget ifpcd.compile.pcds.unnamed.get.around */
  243. int around() : if(true) && get(int BaseApp.i) {
  244. a("around.get_pc.anonymous");
  245. return proceed();
  246. }
  247. /** @testTarget ifpcd.compile.pcds.unnamed.call.around */
  248. void around() : if(true) && call(void *.uncountedCall()) {
  249. a("around.call_pc.anonymous");
  250. proceed();
  251. }
  252. /** @testTarget ifpcd.compile.pcds.unnamed.call.around with Type */
  253. void around() : if(true) && call(void BaseApp.uncountedCall()) {
  254. a("around.callType_pc.anonymous");
  255. proceed();
  256. }
  257. /** @testTarget ifpcd.compile.pcds.unnamed.execution.around */
  258. void around() : if(true) && execution(void BaseApp.uncountedCall()) {
  259. a("around.execution_pc.anonymous");
  260. proceed();
  261. }
  262. /** @testTarget ifpcd.compile.pcds.unnamed.initialization.around */
  263. //XXX
  264. // void around() : if(true) && initialization(BaseApp.new(..)) {
  265. // a("around.initialization_pc.anonymous");
  266. // proceed();
  267. // }
  268. /** @testTarget ifpcd.compile.pcds.unnamed.set.after */
  269. after() : if(true) && set(int BaseApp.i) {
  270. a("after.set_pc.anonymous");
  271. }
  272. /** @testTarget ifpcd.compile.pcds.unnamed.get.after */
  273. after() : if(true) && get(int BaseApp.i) {
  274. a("after.get_pc.anonymous");
  275. }
  276. /** @testTarget ifpcd.compile.pcds.unnamed.call.after */
  277. after() : if(true) && call(void *.uncountedCall()) {
  278. a("after.call_pc.anonymous");
  279. }
  280. /** @testTarget ifpcd.compile.pcds.unnamed.call.after with Type */
  281. after() : if(true) && call(void BaseApp.uncountedCall()) {
  282. a("after.callType_pc.anonymous");
  283. }
  284. /** @testTarget ifpcd.compile.pcds.unnamed.execution.after */
  285. after() : if(true) && execution(void BaseApp.uncountedCall()) {
  286. a("after.execution_pc.anonymous");
  287. }
  288. /** @testTarget ifpcd.compile.pcds.unnamed.initialization.after */
  289. after() : if(true) && initialization(BaseApp.new(..)) {
  290. a("after.initialization_pc.anonymous");
  291. }
  292. static void a(String msg) {
  293. TestContext.signal(msg);
  294. }
  295. }