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.

SynchronizationTests.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*******************************************************************************
  2. * Copyright (c) 2006 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v 2.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  7. *
  8. * Contributors:
  9. * Andy Clement - initial implementation
  10. *******************************************************************************/
  11. package org.aspectj.systemtest.ajc152;
  12. import java.net.URL;
  13. import org.aspectj.testing.XMLBasedAjcTestCase;
  14. import junit.framework.Test;
  15. /**
  16. * Work items, phase #1: lock()/unlock() x expose new joinpoints x parse new pcds x fix tjp string x preventing double unlock()
  17. * messages/markers in structure model x error messages appropriate for attempting to use around advice on synchronization join
  18. * points x making the use of lock/unlock conditional on an -Xjoinpoints:synchronization x activating the -Xjoinpoints options from
  19. * LTW configurations rather than through batch/AJDT x ensure the lock/unlock joinpoints only appear when
  20. * -Xjoinpoints:synchronization specified TAG: Completion of PHASE1
  21. *
  22. *
  23. * Work items, phase #2: transformation
  24. *
  25. * Design: transform all synchronized methods: public synchronized void m() { ... } => public void m() { synchronized (this) { ... }
  26. * }
  27. *
  28. * x transforming synchronized methods x matching execution(synchronized * *(..)) for transformed code x warning message for
  29. * execution() hitting a synchronized method x ensure verifier runs over generated code (done by just executing the code as part of
  30. * the test spec) - Ant task support for -Xjoinpoints TAG: Completion of PHASE2
  31. *
  32. *
  33. * TAG: Finished
  34. *
  35. * Future work items: - optimize matching for transformed methods since we *know* the type we are locking on - supporting type
  36. * pattern in lock() unlock() - this is not entirely trivial as kinded pointcuts do not usually have any residue - weaving messages
  37. * include 'unusual' strings for the join points, not the same as revealed by thisJoinPoint.getSignature() in code - handler is
  38. * probably similar - documentation - lazy translation of synchronized methods, rather than eager - applying execution(* *(..))
  39. * correctly to transformed methods (i.e. inside lock/unlock) - use knowledge of type containing synchronized methods to optimize
  40. * matching of (un)lock() - not always needing residue - line number table is incorrect for transformed code (lock joinpoint has no
  41. * line number)
  42. *
  43. * Notes: IllegalMonitorStateException Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify
  44. * other threads waiting on an object's monitor without owning the specified monitor.
  45. *
  46. * around advice won't work on SUN VMs (may be a bug that it does work on other VMs) since the monitor instructions are extracted to
  47. * a separate method for proceed() calls and yet the VM seems to want them paired inside a single method.
  48. *
  49. * Really we only need to restrict the use of around advice on synchronization join points if the advice uses proceed() - but
  50. * policing that is a little tough because (DOH) the AdviceAttribute field 'proceedCallSignatures' is never filled in (which records
  51. * how many proceeds occur in the advice) - see where it isnt filled in at AdviceDeclaration.resolveStatements() in the loop that
  52. * goes over the proceedCalls list.
  53. *
  54. *
  55. * Problems: - Can't run it on a 1.2.1 runtime - just not practical
  56. *
  57. *
  58. * Method transformation, example:
  59. *
  60. * public synchronized void m(); Code: Stack=2, Locals=1, Args_size=1 0: getstatic #2; //Field
  61. * java/lang/System.err:Ljava/io/PrintStream; 3: ldc #3; //String hello 5: invokevirtual #4; //Method
  62. * java/io/PrintStream.println:(Ljava/lang/String;)V 8: getstatic #2; //Field java/lang/System.err:Ljava/io/PrintStream; 11: ldc #5;
  63. * //String world 13: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 16: return LineNumberTable: line
  64. * 4: 0 line 5: 8 line 6: 16
  65. *
  66. * public void m2(); Code: Stack=2, Locals=3, Args_size=1 0: aload_0 1: dup 2: astore_1 3: monitorenter 4: getstatic #2; //Field
  67. * java/lang/System.err:Ljava/io/PrintStream; 7: ldc #3; //String hello 9: invokevirtual #4; //Method
  68. * java/io/PrintStream.println:(Ljava/lang/String;)V 12: getstatic #2; //Field java/lang/System.err:Ljava/io/PrintStream; 15: ldc
  69. * #5; //String world 17: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 20: aload_1 21: monitorexit
  70. * 22: goto 30 25: astore_2 26: aload_1 27: monitorexit 28: aload_2 29: athrow 30: return Exception table: from to target type 4 22
  71. * 25 any 25 28 25 any
  72. *
  73. * Factors affecting transformation: - LDC in Java5 supports referring to a class literal, e.g. Foo.class whereas before Java5, it
  74. * did not. This means if generating the synchronized() block for a static method from a preJava5 class then we have to generate a
  75. * lot of crap to build the class object for locking and unlocking. The object is also stored in a local field of the type (if we
  76. * follow the pattern of JDT/JAVAC)
  77. */
  78. public class SynchronizationTests extends XMLBasedAjcTestCase {
  79. // testing the new join points for monitorenter/monitorexit
  80. public void testTheBasics_1() {
  81. runTest("basic");
  82. }
  83. public void testTheBasics_2() {
  84. runTest("basic - within");
  85. }
  86. public void testTheBasics_3() {
  87. runTest("basic - within plus args");
  88. }
  89. public void testTheBasics_4() {
  90. runTest("basic - within plus this");
  91. } // this null in static context
  92. public void testTheBasics_5() {
  93. runTest("basic - within plus target");
  94. } // target null in static context?
  95. // testing parsing of the new PCDs lock/unlock
  96. public void testParsing_1() {
  97. runTest("parsing - lock");
  98. }
  99. public void testParsing_2() {
  100. runTest("parsing - unlock");
  101. }
  102. public void testParsing_errors_1() {
  103. runTest("parsing - error - lock");
  104. }
  105. public void testParsing_errors_2() {
  106. runTest("parsing - error - unlock");
  107. }
  108. // testing parsing and matching with the new PCDs
  109. public void testParsingAndMatching_1() {
  110. runTest("parsing and matching - lock and static context");
  111. }
  112. public void testParsingAndMatching_2() {
  113. runTest("parsing and matching - unlock and static context");
  114. }
  115. public void testParsingAndMatching_3() {
  116. runTest("parsing and matching - lock and non-static context");
  117. }
  118. public void testParsingAndMatching_4() {
  119. runTest("parsing and matching - unlock and non-static context");
  120. }
  121. public void testParsingAndMatching_5() {
  122. runTest("parsing and matching - lock and non-static context");
  123. }
  124. public void testParsingAndMatching_6() {
  125. runTest("parsing and matching - unlock and non-static context");
  126. }
  127. // using the new PCDs in a LTW environment
  128. public void testUsingWithLTW_MissingFlag_1() {
  129. runTest("using lock with LTW - missing flag");
  130. }
  131. public void testUsingWithLTW_MissingFlag_2() {
  132. runTest("using unlock with LTW - missing flag");
  133. }
  134. public void testUsingWithLTW_1() {
  135. runTest("using lock with LTW");
  136. }
  137. public void testUsingWithLTW_2() {
  138. runTest("using unlock with LTW");
  139. }
  140. // multiple PCDs
  141. public void testCombiningPCDs_1() {
  142. runTest("combining pcds - lock and this");
  143. }
  144. public void testCombiningPCDs_2() {
  145. runTest("combining pcds - unlock and this");
  146. }
  147. // useful examples
  148. public void testUseful_1() {
  149. runTest("a useful program");
  150. } // just uses within/args - matching the (un)lock jps
  151. public void testUseful_2() {
  152. runTest("a useful program - with lock");
  153. } // uses lock/args
  154. // all the methods of thisJoinPoint
  155. public void testThisJoinPoint_1() {
  156. runTest("thisjoinpoint - monitor entry");
  157. }
  158. public void testThisJoinPoint_2() {
  159. runTest("thisjoinpoint - monitor exit");
  160. }
  161. public void testDoubleMessagesOnUnlock() {
  162. // AsmManager.setReporting("c:/foo.txt",true,true,true,true);
  163. runTest("prevent double unlock weaving messages and model contents");
  164. // checkModel1();
  165. }
  166. // targetting 1.2 runtime - signature creation code in LazyClassGen.initializeTjp may not work
  167. // different advice kinds
  168. public void testBeforeAdvice_1() {
  169. runTest("before advice - lock");
  170. }
  171. public void testBeforeAdvice_2() {
  172. runTest("before advice - unlock");
  173. }
  174. public void testAfterAdvice_1() {
  175. runTest("after advice - lock");
  176. }
  177. public void testAfterAdvice_2() {
  178. runTest("after advice - unlock");
  179. }
  180. public void testAroundAdvice_1() {
  181. runTest("around advice - lock");
  182. }
  183. public void testAroundAdvice_2() {
  184. runTest("around advice - unlock");
  185. }
  186. public void testLockingTJP() {
  187. runTest("obtaining locked object through getArgs");
  188. }
  189. // binary weaving?
  190. // nested locking/unlocking
  191. // --- helpers
  192. // Half finished - could check there is only one relationship for unlock() rather than two - but
  193. // that seems to be the case anyway (peculiar...)
  194. // private void checkModel1() {
  195. // // Verifies only one unlock relationship, not two
  196. // IProgramElement unlockNode =
  197. // AsmManager.getDefault().getHierarchy().findElementForLabel(AsmManager.getDefault().getHierarchy().getRoot(),
  198. // IProgramElement.Kind.CODE,"unlock(void java.lang.Object.<unlock>(java.lang.Object))");
  199. // assertTrue("Couldn't find the unlock node",unlockNode!=null);
  200. // List l = AsmManager.getDefault().getRelationshipMap().get(unlockNode);
  201. // assertTrue("should be one entry :"+l,l!=null && l.size()==1);
  202. // IRelationship ir = (IRelationship)l.get(0);
  203. // System.err.println(ir);
  204. // List targs = ir.getTargets();
  205. // System.err.println(targs.size());
  206. // System.err.println(targs.get(0));
  207. // }
  208. // ---
  209. public static Test suite() {
  210. return XMLBasedAjcTestCase.loadSuite(SynchronizationTests.class);
  211. }
  212. protected URL getSpecFile() {
  213. return getClassResource("synchronization.xml");
  214. }
  215. }