選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AllRuntime.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. import org.aspectj.lang.*;
  2. import org.aspectj.lang.reflect.*;
  3. /**
  4. * Run via main or driveTest.
  5. * If you want the verbose output,
  6. * use "-p{rint}" to print when invoking via main,
  7. * or set resultCache with your result sink before running:
  8. * <pre>StringBuffer sb = new StringBuffer();
  9. * AllRuntime.resultCache(sb);
  10. * int errors = AllRuntime.driveTest();
  11. * System.err.println(sb.toString());
  12. * System.err.println("Errors: " + errors);</pre>
  13. * <p>
  14. * This was written to run in a 1.1 VM,
  15. * outside the Tester or Collections or...
  16. *
  17. * @testcase PR#474 rt.java uses 1.2-only variant of Class.forName
  18. */
  19. public class AllRuntime {
  20. public static void resultCache(StringBuffer cache) {
  21. A.resultCache(cache);
  22. }
  23. public static void main(String[] args) {
  24. StringBuffer result = null;
  25. if ((null != args) && (0 < args.length)
  26. && (args[0].startsWith("-p"))) {
  27. result = new StringBuffer();
  28. resultCache(result);
  29. }
  30. int errors = driveTest();
  31. A.log("Errors: " + errors);
  32. if (null != result) {
  33. System.err.println(result.toString());
  34. }
  35. }
  36. /** @return number of errors detected */
  37. public static int driveTest() {
  38. int result = 0;
  39. boolean ok = testNoAspectBoundException();
  40. if (!ok) result++;
  41. A.log("testNoAspectBoundException: " + ok);
  42. ok = testMultipleAspectsBoundException();
  43. if (!ok) result++;
  44. A.log("testMultipleAspectsBoundException: " + ok);
  45. TargetClass me = new TargetClass();
  46. ok = me.catchThrows();
  47. if (!ok) result++;
  48. int temp = me.publicIntMethod(2);
  49. if (temp != 12) result++;
  50. StringBuffer sb = new StringBuffer();
  51. sb.append("" + me); // callee-side join point
  52. if (sb.length() < 1) result++;
  53. A.log("Callee-side join point " + sb.toString());
  54. try {
  55. ok = false;
  56. me.throwException = true;
  57. me.run();
  58. } catch (SoftException e) {
  59. ok = true;
  60. }
  61. if (!ok) result++;
  62. A.log("SoftException: " + ok);
  63. A a = A.aspectOf();
  64. if (null != a) {
  65. ok = a.report();
  66. if (!ok) result++;
  67. A.log(" => all advice was run: " + ok);
  68. }
  69. return result;
  70. }
  71. /** todo: need test case for multiple aspects */
  72. public static boolean testMultipleAspectsBoundException() {
  73. return true;
  74. }
  75. public static boolean testNoAspectBoundException() {
  76. boolean result = false;
  77. try {
  78. B a = B.aspectOf(new Object());
  79. } catch (NoAspectBoundException e) {
  80. result = true;
  81. }
  82. return result;
  83. }
  84. }
  85. /** This has all relevant join points */
  86. class TargetClass {
  87. private static int INDEX;
  88. static {
  89. INDEX = 10;
  90. }
  91. private int index = INDEX;
  92. private int shadow = index;
  93. public int publicIntMethod(int input) {
  94. return privateIntMethod(input);
  95. }
  96. public boolean catchThrows() {
  97. try {
  98. throw new Exception("hello");
  99. } catch (Exception e) {
  100. if (null != e) return true;
  101. }
  102. return false;
  103. }
  104. /** print in VM-independent fashion */
  105. public String toString() {
  106. return "TargetClass " + shadow;
  107. }
  108. private int privateIntMethod(int input) {
  109. return shadow = index += input;
  110. }
  111. }
  112. /** used only for NoAspectBoundException test */
  113. aspect B perthis(target(TargetClass)) { }
  114. aspect A {
  115. /** log goes here if defined */
  116. private static StringBuffer CACHE;
  117. /** count number of join points hit */
  118. private static int jpIndex = 0;
  119. /** count number of A created */
  120. private static int INDEX = 0;
  121. /** index of this A */
  122. private int index;
  123. /** count for each advice of how many times invoked */
  124. private final int[] adviceHits;
  125. A() {
  126. index = INDEX++;
  127. adviceHits = new int[21];
  128. }
  129. public static void resultCache(StringBuffer cache) {
  130. if (CACHE != cache) CACHE = cache;
  131. }
  132. public static void log(String s) {
  133. StringBuffer cache = CACHE;
  134. if (null != cache) {
  135. cache.append(s);
  136. cache.append("\n");
  137. }
  138. }
  139. private void log(int i) { adviceHits[i]++; }
  140. /** report how many times each advice was run
  141. * logging report.
  142. * @return false if any advice was not hit
  143. */
  144. public boolean report() {
  145. StringBuffer sb = new StringBuffer();
  146. boolean result = report(this, sb);
  147. log(sb.toString());
  148. return result;
  149. }
  150. /** report how many times each advice was run
  151. * @return false if any advice was not hit
  152. */
  153. public static boolean report(A a, StringBuffer sb) {
  154. boolean result = true;
  155. if (null == a.adviceHits) {
  156. sb.append("[]");
  157. } else {
  158. sb.append("[");
  159. int[] adviceHits = a.adviceHits;
  160. for (int i = 0; i < adviceHits.length; i++) {
  161. if (i > 0) sb.append(", ");
  162. sb.append(i+"="+adviceHits[i]);
  163. if (result && (0 == adviceHits[i])) {
  164. result = false;
  165. }
  166. }
  167. sb.append("]");
  168. }
  169. return result;
  170. }
  171. public static void throwsException() throws Exception {
  172. throw new Exception("exception");
  173. }
  174. public String toString() { return "A " + index; }
  175. //-------------------------------------- pointcuts
  176. pointcut safety()
  177. : !within(A)
  178. && !cflow(execution(String TargetClass.toString()))
  179. && !call(String TargetClass.toString())
  180. ;
  181. pointcut intMethod() : call(int TargetClass.publicIntMethod(int));
  182. //-------------------------------------- declare, introductions
  183. declare parents : TargetClass implements Runnable;
  184. declare soft : Exception : execution(void TargetClass.run());
  185. /** unused - enable to throw exception from run() */
  186. public boolean TargetClass.throwException;
  187. public void TargetClass.run() {
  188. if (throwException) throwsException();
  189. }
  190. //-------------------------------------- advice
  191. /** was callee-side call join point, now is execution */ // todo: not being invoked, though TargetClass.toString is???
  192. before() : execution(public String toString())
  193. && target(TargetClass) {
  194. /* comment out test to avoid StackOverflow
  195. test(thisJoinPoint, thisJoinPointStaticPart, this,
  196. "before() : call(String TargetClass.toString())");
  197. */
  198. log(1);
  199. }
  200. /** caller-side call join point */
  201. before() : call(int TargetClass.privateIntMethod(int)) {
  202. test(thisJoinPoint, thisJoinPointStaticPart, this,
  203. "before() : call(int TargetClass.privateIntMethod()) ");
  204. log(2);
  205. }
  206. /** call join point */
  207. before() : intMethod() {
  208. test(thisJoinPoint, thisJoinPointStaticPart, this,
  209. "before() : pc() ");
  210. log(3);
  211. }
  212. /** execution join point */
  213. before() : execution(int TargetClass.privateIntMethod(int)) {
  214. test(thisJoinPoint, thisJoinPointStaticPart, this,
  215. "before() : execution(int TargetClass.privateIntMethod()) ");
  216. log(4);
  217. }
  218. /** execution join point for constructor */
  219. before() : execution(TargetClass.new(..)) {
  220. test(thisJoinPoint, thisJoinPointStaticPart, this,
  221. "before() : execution(TargetClass.new(..)) ");
  222. log(5);
  223. }
  224. /** initialization join point */
  225. before() : initialization(TargetClass+.new(..)) {
  226. test(thisJoinPoint, thisJoinPointStaticPart, this,
  227. "before() : initialization(TargetClass+.new(..)) ");
  228. log(6);
  229. }
  230. /** static initialization join point */
  231. before() : initialization(TargetClass+.new(..)) {
  232. test(thisJoinPoint, thisJoinPointStaticPart, this,
  233. "before() : initialization(TargetClass+.new(..)) ");
  234. log(7);
  235. }
  236. /** cflow join point */
  237. before() : cflow(execution(int TargetClass.publicIntMethod(int)))
  238. && safety() {
  239. test(thisJoinPoint, thisJoinPointStaticPart, this,
  240. "before() : cflow(execution(int TargetClass.publicIntMethod(int)))");
  241. log(8);
  242. }
  243. /** cflowbelow join point */
  244. before() : cflowbelow(execution(int TargetClass.publicIntMethod(int)))
  245. && safety() {
  246. test(thisJoinPoint, thisJoinPointStaticPart, this,
  247. "before() : cflowbelow(execution(int TargetClass.publicIntMethod(int)))");
  248. log(9);
  249. }
  250. /** initialization join point */
  251. before() : initialization(TargetClass+.new(..)) {
  252. test(thisJoinPoint, thisJoinPointStaticPart, this,
  253. "before() : initialization(TargetClass+.new(..)) ");
  254. log(10);
  255. }
  256. /** field set join point */
  257. before() : set(int TargetClass.index) && safety() {
  258. test(thisJoinPoint, thisJoinPointStaticPart, this,
  259. "before() : set(int TargetClass.index) ");
  260. log(11);
  261. }
  262. /** field get join point */
  263. before() : get(int TargetClass.index) && safety() {
  264. test(thisJoinPoint, thisJoinPointStaticPart, this,
  265. "before() : get(int TargetClass.index) ");
  266. log(12);
  267. }
  268. /** within join point (static) */
  269. before() : within(TargetClass+) && safety() {
  270. test(thisJoinPoint, thisJoinPointStaticPart, this,
  271. "before() : within(TargetClass+) ");
  272. log(13);
  273. }
  274. /** withincode join point (static) */
  275. before() : withincode(int TargetClass+.publicIntMethod(int)) && safety() {
  276. test(thisJoinPoint, thisJoinPointStaticPart, this,
  277. "before() : withincode(int TargetClass+.publicIntMethod(int)) ");
  278. log(14);
  279. }
  280. /** this join point */
  281. before(TargetClass t) : this(t) && safety() {
  282. test(thisJoinPoint, thisJoinPointStaticPart, this,
  283. "before(TargetClass t) : this(t) && safety() This t: " + t + " this: " + this);
  284. log(15);
  285. }
  286. /** target join point */
  287. before(TargetClass t) : target(t) && safety() {
  288. test(thisJoinPoint, thisJoinPointStaticPart, this,
  289. "before(TargetClass t) : target(t) && safety() target t: " + t + " this: " + this);
  290. log(16);
  291. }
  292. /** args join point */
  293. before(int i) : args(i) && safety() {
  294. test(thisJoinPoint, thisJoinPointStaticPart, this,
  295. "before(int i) : args(i) && safety() args i: " + i);
  296. log(17);
  297. }
  298. /** handler join point */
  299. before() : handler(Exception) { // && args(e) {
  300. test(thisJoinPoint, thisJoinPointStaticPart, this,
  301. "before(Throwable e) : handler(Throwable) && args(e) && within(TargetClass+) args e: " );
  302. log(18);
  303. }
  304. /** if pcd join point */
  305. before(int i) : args(i) && if(i > 0) && safety() {
  306. test(thisJoinPoint, thisJoinPointStaticPart, this,
  307. "before(int i) : args(i) && if(i > 0) && safety() args i: " + i);
  308. log(19);
  309. }
  310. /** call join point for constructor */
  311. before() : call(TargetClass.new(..)) {
  312. test(thisJoinPoint, thisJoinPointStaticPart, this,
  313. "before() : call(TargetClass.new(..)) ");
  314. log(20);
  315. }
  316. /** everything join point */
  317. before(TargetClass t)
  318. : (target(t) )
  319. && (call(int TargetClass.privateIntMethod(int))
  320. || execution(int TargetClass.privateIntMethod(int))
  321. || initialization(TargetClass.new())
  322. || (cflow(call(int TargetClass.privateIntMethod(int)))
  323. && !cflowbelow(call(int TargetClass.privateIntMethod(int))))
  324. )
  325. && (!cflow(call(void TargetClass.catchThrows())))
  326. && (!call(void TargetClass.run()))
  327. && (!set(int TargetClass.index))
  328. && (!get(int TargetClass.index))
  329. && safety()
  330. && if(null != t) {
  331. test(thisJoinPoint, thisJoinPointStaticPart, this,
  332. "everything"); // todo: add args
  333. log(0);
  334. }
  335. private void test(JoinPoint jp, JoinPoint.StaticPart jpsp, Object tis,
  336. String context) {
  337. StringBuffer sb = new StringBuffer();
  338. sb.append("\n join pt: " + jpIndex++);
  339. sb.append("\n jp: " + jp);
  340. render(jp, sb);
  341. sb.append("\n jpsp: " + jpsp);
  342. sb.append("\n tis: " + tis);
  343. sb.append("\n this: " + this);
  344. sb.append("\n context: " + context);
  345. log(sb.toString());
  346. }
  347. private void render(JoinPoint jp, StringBuffer sb) {
  348. if (null == jp) {
  349. sb.append("null");
  350. } else {
  351. //sb.append("\n args: " + jp.getArgs());
  352. sb.append("\n args: ");
  353. render(jp.getArgs(), sb);
  354. sb.append("\n kind: " + jp.getKind());
  355. sb.append("\n sig: " );
  356. render(jp.getSignature(), sb);
  357. sb.append("\n loc: " );
  358. render(jp.getSourceLocation(), sb);
  359. sb.append("\n targ: " + jp.getTarget());
  360. sb.append("\n this: " + jp.getThis());
  361. }
  362. }
  363. /** render to check subtype of Signature, print in VM-independent fashion */
  364. private void render(Signature sig, StringBuffer sb) {
  365. if (null == sig) {
  366. sb.append("null");
  367. } else {
  368. if (sig instanceof AdviceSignature) {
  369. sb.append("AdviceSignature ");
  370. sb.append(sig.getName() + " " );
  371. sb.append(""+((AdviceSignature ) sig).getReturnType());
  372. } else if (sig instanceof CatchClauseSignature) {
  373. sb.append("CatchClauseSignature ");
  374. sb.append(sig.getName() + " " );
  375. sb.append(""+((CatchClauseSignature ) sig).getParameterType());
  376. } else if (sig instanceof ConstructorSignature) {
  377. sb.append("ConstructorSignature ");
  378. sb.append(sig.getName() + " " );
  379. sb.append(""+((ConstructorSignature) sig).getName());
  380. } else if (sig instanceof FieldSignature) {
  381. sb.append("FieldSignature ");
  382. sb.append(sig.getName() + " " );
  383. sb.append(""+((FieldSignature ) sig).getFieldType());
  384. } else if (sig instanceof InitializerSignature) {
  385. sb.append("InitializerSignature ");
  386. sb.append(sig.getName() + " " );
  387. } else if (sig instanceof MethodSignature) {
  388. sb.append("MethodSignature ");
  389. sb.append(sig.getName() + " " );
  390. sb.append(""+((MethodSignature) sig).getReturnType());
  391. } else if (sig instanceof MemberSignature) {
  392. sb.append("MemberSignature?? ");
  393. sb.append(sig.getName() + " " );
  394. } else if (sig instanceof CodeSignature) {
  395. sb.append("CodeSignature ??");
  396. sb.append(sig.getName() + " " );
  397. } else {
  398. sb.append("Unknown ??");
  399. sb.append(sig.getName() + " " );
  400. }
  401. }
  402. }
  403. private void render(SourceLocation sl, StringBuffer sb) {
  404. if (null == sl) {
  405. sb.append("null");
  406. } else {
  407. String path = sl.getFileName();
  408. int loc = path.lastIndexOf("/");
  409. if (-1 != loc) {
  410. path = path.substring(loc+1);
  411. } else {
  412. // todo: not portable to other machines
  413. loc = path.lastIndexOf("\\");
  414. if (-1 != loc) {
  415. path = path.substring(loc+1);
  416. }
  417. }
  418. sb.append(path);
  419. sb.append(":" + sl.getLine());
  420. //sb.append(":" + sl.getColumn());
  421. }
  422. }
  423. private void render(Object[] args, StringBuffer sb) {
  424. if (null == args) {
  425. sb.append("null");
  426. } else {
  427. sb.append("Object[" + args.length + "] = {");
  428. for (int i = 0; i < args.length; i++) {
  429. if (i > 0) sb.append(", ");
  430. sb.append("" + args[i]);
  431. }
  432. sb.append("}");
  433. }
  434. }
  435. }