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.

AnnotationPatternTestCase.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * ******************************************************************/
  10. package org.aspectj.weaver.patterns;
  11. import org.aspectj.bridge.AbortException;
  12. import org.aspectj.util.LangUtil;
  13. import org.aspectj.weaver.AnnotatedElement;
  14. import org.aspectj.weaver.AnnotationAJ;
  15. import org.aspectj.weaver.ResolvedType;
  16. import org.aspectj.weaver.UnresolvedType;
  17. import org.aspectj.weaver.WeaverTestCase;
  18. import org.aspectj.weaver.bcel.BcelWorld;
  19. import junit.framework.TestCase;
  20. public class AnnotationPatternTestCase extends TestCase {
  21. public void testParseSimpleAnnotationPattern() {
  22. PatternParser p = new PatternParser("@Foo");
  23. AnnotationTypePattern foo = p.maybeParseAnnotationPattern();
  24. foo = foo.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  25. assertTrue("ExactAnnotationTypePattern", foo instanceof ExactAnnotationTypePattern);
  26. assertEquals("Foo", UnresolvedType.forSignature("LFoo;"), ((ExactAnnotationTypePattern) foo).annotationType);
  27. }
  28. public void testParseAndAnnotationPattern() {
  29. PatternParser p = new PatternParser("@Foo @Goo");
  30. AnnotationTypePattern fooAndGoo = p.maybeParseAnnotationPattern();
  31. assertTrue("AndAnnotationTypePattern", fooAndGoo instanceof AndAnnotationTypePattern);
  32. assertEquals("@(Foo) @(Goo)", fooAndGoo.toString());
  33. fooAndGoo = fooAndGoo.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  34. assertEquals("@Foo @Goo", fooAndGoo.toString());
  35. AnnotationTypePattern left = ((AndAnnotationTypePattern) fooAndGoo).getLeft();
  36. AnnotationTypePattern right = ((AndAnnotationTypePattern) fooAndGoo).getRight();
  37. assertEquals("Foo", UnresolvedType.forSignature("LFoo;"), ((ExactAnnotationTypePattern) left).annotationType);
  38. assertEquals("Goo", UnresolvedType.forSignature("LGoo;"), ((ExactAnnotationTypePattern) right).annotationType);
  39. }
  40. //
  41. // public void testParseOrAnnotationPattern() {
  42. // PatternParser p = new PatternParser("@Foo || @Goo");
  43. // AnnotationTypePattern fooOrGoo = p.parseAnnotationTypePattern();
  44. // assertTrue("OrAnnotationTypePattern",fooOrGoo instanceof
  45. // OrAnnotationTypePattern);
  46. // assertEquals("(@Foo || @Goo)",fooOrGoo.toString());
  47. // AnnotationTypePattern left =
  48. // ((OrAnnotationTypePattern)fooOrGoo).getLeft();
  49. // AnnotationTypePattern right =
  50. // ((OrAnnotationTypePattern)fooOrGoo).getRight();
  51. // assertEquals("Foo",UnresolvedType.forName("Foo"),((
  52. // ExactAnnotationTypePattern)left).annotationType);
  53. // assertEquals("Goo",UnresolvedType.forName("Goo"),((
  54. // ExactAnnotationTypePattern)right).annotationType);
  55. // }
  56. //
  57. public void testParseNotAnnotationPattern() {
  58. PatternParser p = new PatternParser("!@Foo");
  59. AnnotationTypePattern notFoo = p.maybeParseAnnotationPattern();
  60. assertTrue("NotAnnotationTypePattern", notFoo instanceof NotAnnotationTypePattern);
  61. notFoo = notFoo.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  62. assertEquals("!@Foo", notFoo.toString());
  63. AnnotationTypePattern body = ((NotAnnotationTypePattern) notFoo).getNegatedPattern();
  64. assertEquals("Foo", UnresolvedType.forName("Foo"), ((ExactAnnotationTypePattern) body).annotationType);
  65. }
  66. public void testParseBracketedAnnotationPattern() {
  67. PatternParser p = new PatternParser("(@Foo)");
  68. AnnotationTypePattern foo = p.maybeParseAnnotationPattern();
  69. // cannot start with ( so, we get ANY
  70. assertEquals("ANY", AnnotationTypePattern.ANY, foo);
  71. }
  72. public void testParseFQAnnPattern() {
  73. PatternParser p = new PatternParser("@org.aspectj.Foo");
  74. AnnotationTypePattern foo = p.maybeParseAnnotationPattern();
  75. assertEquals("@(org.aspectj.Foo)", foo.toString());
  76. }
  77. public void testParseComboPattern() {
  78. // PatternParser p = new PatternParser("!((@Foo || @Goo) && !@Boo)");
  79. PatternParser p = new PatternParser("@(Foo || Goo)!@Boo");
  80. AnnotationTypePattern ap = p.maybeParseAnnotationPattern();
  81. ap = ap.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  82. AndAnnotationTypePattern atp = (AndAnnotationTypePattern) ap;
  83. NotAnnotationTypePattern notBoo = (NotAnnotationTypePattern) atp.getRight();
  84. // ExactAnnotationTypePattern boo = (ExactAnnotationTypePattern)
  85. notBoo.getNegatedPattern();
  86. // AnnotationTypePattern fooOrGoo = (AnnotationTypePattern)
  87. atp.getLeft();
  88. assertEquals("@((Foo || Goo)) !@Boo", ap.toString());
  89. }
  90. // public void testParseAndOrPattern() {
  91. // PatternParser p = new PatternParser("@Foo && @Boo || @Goo");
  92. // AnnotationTypePattern andOr = p.parseAnnotationTypePattern();
  93. // assertTrue("Should be or pattern",andOr instanceof
  94. // OrAnnotationTypePattern);
  95. // }
  96. //
  97. public void testParseBadPattern() {
  98. PatternParser p = new PatternParser("@@Foo");
  99. try {
  100. p.maybeParseAnnotationPattern();
  101. fail("ParserException expected");
  102. } catch (ParserException pEx) {
  103. assertEquals("name pattern", pEx.getMessage());
  104. }
  105. }
  106. public void testParseBadPattern2() {
  107. PatternParser p = new PatternParser("Foo");
  108. AnnotationTypePattern bad = p.maybeParseAnnotationPattern();
  109. assertEquals("ANY", AnnotationTypePattern.ANY, bad);
  110. }
  111. public void testParseNameOrVarAnnotationPattern() {
  112. PatternParser p = new PatternParser("Foo");
  113. AnnotationTypePattern foo = p.parseAnnotationNameOrVarTypePattern();
  114. assertTrue("ExactAnnotationTypePattern expected", foo != null);
  115. assertEquals("Foo", UnresolvedType.forName("Foo"), ((ExactAnnotationTypePattern) foo).annotationType);
  116. }
  117. public void testParseNameOrVarAnnotationPatternWithNot() {
  118. PatternParser p = new PatternParser("!@Foo");
  119. try {
  120. // AnnotationTypePattern bad =
  121. p.parseAnnotationNameOrVarTypePattern();
  122. fail("ParserException expected");
  123. } catch (ParserException pEx) {
  124. assertEquals("identifier", pEx.getMessage());
  125. }
  126. }
  127. public void testParseNameOrVarAnnotationPatternWithOr() {
  128. PatternParser p = new PatternParser("Foo || Boo");
  129. AnnotationTypePattern foo = p.parseAnnotationNameOrVarTypePattern();
  130. // rest of pattern not consumed...
  131. assertTrue("ExactAnnotationTypePattern", foo instanceof ExactAnnotationTypePattern);
  132. assertEquals("Foo", UnresolvedType.forName("Foo"), ((ExactAnnotationTypePattern) foo).annotationType);
  133. }
  134. public void testParseNameOrVarAnnotationWithBinding() {
  135. PatternParser p = new PatternParser("foo");
  136. AnnotationTypePattern foo = p.parseAnnotationNameOrVarTypePattern();
  137. assertTrue("ExactAnnotationTypePattern", foo instanceof ExactAnnotationTypePattern);
  138. assertEquals("@foo", ((ExactAnnotationTypePattern) foo).toString());
  139. }
  140. public void testParseNameOrVarAnnotationPatternWithAnd() {
  141. PatternParser p = new PatternParser("Foo Boo");
  142. AnnotationTypePattern foo = p.parseAnnotationNameOrVarTypePattern();
  143. // rest of pattern not consumed...
  144. assertEquals("@Foo", foo.toString());
  145. }
  146. public void testMaybeParseAnnotationPattern() {
  147. PatternParser p = new PatternParser("@Foo");
  148. AnnotationTypePattern a = p.maybeParseAnnotationPattern();
  149. assertNotNull("Should find annotation pattern", a);
  150. p = new PatternParser("Foo && Boo");
  151. a = p.maybeParseAnnotationPattern();
  152. assertEquals("Should be ANY pattern for a non-match", AnnotationTypePattern.ANY, a);
  153. }
  154. public void testParseTypePatternsWithAnnotations() {
  155. PatternParser p = new PatternParser("@Foo *");
  156. TypePattern t = p.parseTypePattern();
  157. assertTrue("AnyWithAnnotationTypePattern", t instanceof AnyWithAnnotationTypePattern);
  158. AnnotationTypePattern atp = t.annotationPattern;
  159. assertEquals("@(Foo)", atp.toString());
  160. assertEquals("(@(Foo) *)", t.toString());
  161. }
  162. public void testParseTypePatternsWithAnnotationsComplex() {
  163. PatternParser p = new PatternParser("(@(Foo || Boo) (Foo || Boo))");
  164. TypePattern t = p.parseTypePattern();
  165. assertTrue("OrTypePattern", t instanceof OrTypePattern);
  166. assertEquals("((@((Foo || Boo)) Foo) || (@((Foo || Boo)) Boo))", t.toString());
  167. }
  168. public void testNotSyntax() {
  169. PatternParser p = new PatternParser("!@Foo (Foo || Boo))");
  170. TypePattern t = p.parseTypePattern();
  171. assertTrue("OrTypePattern", t instanceof OrTypePattern);
  172. assertEquals("((!@(Foo) Foo) || (!@(Foo) Boo))", t.toString());
  173. }
  174. public void testParseMethodOrConstructorSigNoAP() {
  175. PatternParser p = new PatternParser("* *.*(..)");
  176. SignaturePattern s = p.parseMethodOrConstructorSignaturePattern();
  177. assertEquals("Any annotation", AnnotationTypePattern.ANY, s.getAnnotationPattern());
  178. assertEquals("Any return", "*", s.getReturnType().toString());
  179. assertEquals("Any dec type", "*", s.getDeclaringType().toString());
  180. assertEquals("Any name", "*", s.getName().toString());
  181. assertEquals("* *.*(..)", s.toString());
  182. }
  183. public void testParseMethodOrConstructorSigSimpleAP() {
  184. PatternParser p = new PatternParser("@Foo * *.*(..)");
  185. SignaturePattern s = p.parseMethodOrConstructorSignaturePattern();
  186. assertEquals("@(Foo) annotation", "@(Foo)", s.getAnnotationPattern().toString());
  187. assertEquals("Any return", "*", s.getReturnType().toString());
  188. assertEquals("Any dec type", "*", s.getDeclaringType().toString());
  189. assertEquals("Any name", "*", s.getName().toString());
  190. assertEquals("@(Foo) * *.*(..)", s.toString());
  191. }
  192. public void testParseMethodOrConstructorSigComplexAP() {
  193. PatternParser p = new PatternParser("!@(Foo || Goo) * *.*(..)");
  194. SignaturePattern s = p.parseMethodOrConstructorSignaturePattern();
  195. assertEquals("complex annotation", "!@((Foo || Goo))", s.getAnnotationPattern().toString());
  196. assertEquals("Any return", "*", s.getReturnType().toString());
  197. assertEquals("Any dec type", "*", s.getDeclaringType().toString());
  198. assertEquals("Any name", "*", s.getName().toString());
  199. assertEquals("!@((Foo || Goo)) * *.*(..)", s.toString());
  200. }
  201. public void testParseMethodFieldSigNoAP() {
  202. PatternParser p = new PatternParser("* *.*");
  203. SignaturePattern s = p.parseFieldSignaturePattern();
  204. assertEquals("Any annotation", AnnotationTypePattern.ANY, s.getAnnotationPattern());
  205. assertEquals("Any field type", "*", s.getReturnType().toString());
  206. assertEquals("Any dec type", "*", s.getDeclaringType().toString());
  207. assertEquals("Any name", "*", s.getName().toString());
  208. assertEquals("* *.*", s.toString());
  209. }
  210. public void testParseFieldSigSimpleAP() {
  211. PatternParser p = new PatternParser("@Foo * *.*");
  212. SignaturePattern s = p.parseFieldSignaturePattern();
  213. assertEquals("@Foo annotation", "@(Foo)", s.getAnnotationPattern().toString());
  214. assertEquals("Any field type", "*", s.getReturnType().toString());
  215. assertEquals("Any dec type", "*", s.getDeclaringType().toString());
  216. assertEquals("Any name", "*", s.getName().toString());
  217. assertEquals("@(Foo) * *.*", s.toString());
  218. }
  219. public void testParseFieldSigComplexAP() {
  220. PatternParser p = new PatternParser("!@(Foo || Goo) * *.*");
  221. SignaturePattern s = p.parseFieldSignaturePattern();
  222. assertEquals("complex annotation", "!@((Foo || Goo))", s.getAnnotationPattern().toString());
  223. assertEquals("Any field type", "*", s.getReturnType().toString());
  224. assertEquals("Any dec type", "*", s.getDeclaringType().toString());
  225. assertEquals("Any name", "*", s.getName().toString());
  226. assertEquals("!@((Foo || Goo)) * *.*", s.toString());
  227. }
  228. public void testExactAnnotationPatternMatching() {
  229. if (LangUtil.is15VMOrGreater()) {
  230. PatternParser p = new PatternParser("@Foo");
  231. AnnotationTypePattern ap = p.maybeParseAnnotationPattern();
  232. ap = ap.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  233. AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[] { "Foo" });
  234. assertTrue("matches element with Foo", ap.matches(ae).alwaysTrue());
  235. AnnotatedElementImpl ae2 = new AnnotatedElementImpl(new String[] { "Boo" });
  236. assertTrue("does not match element with Boo", ap.matches(ae2).alwaysFalse());
  237. }
  238. }
  239. public void testBindingAnnotationPatternMatching() {
  240. if (LangUtil.is15VMOrGreater()) {
  241. PatternParser p = new PatternParser("foo");
  242. AnnotationTypePattern ap = p.parseAnnotationNameOrVarTypePattern();
  243. try {
  244. ap = ap.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  245. } catch (AbortException abEx) {
  246. assertEquals("Binding not supported in @pcds (1.5.0 M1 limitation): null", abEx.getMessage());
  247. }
  248. // uncomment these next lines once binding is supported
  249. // AnnotatedElementImpl ae = new AnnotatedElementImpl(new
  250. // String[]{"Foo"});
  251. // assertTrue("matches element with Foo",ap.matches(ae).alwaysTrue())
  252. // ;
  253. // AnnotatedElementImpl ae2 = new AnnotatedElementImpl(new
  254. // String[]{"Boo"});
  255. // assertTrue("does not match element with Boo",ap.matches(ae2).
  256. // alwaysFalse());
  257. }
  258. }
  259. public void testAndAnnotationPatternMatching() {
  260. if (LangUtil.is15VMOrGreater()) {
  261. PatternParser p = new PatternParser("@Foo @Boo");
  262. AnnotationTypePattern ap = p.maybeParseAnnotationPattern();
  263. ap = ap.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  264. AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[] { "Foo", "Boo" });
  265. assertTrue("matches foo and boo", ap.matches(ae).alwaysTrue());
  266. ae = new AnnotatedElementImpl(new String[] { "Foo" });
  267. assertTrue("does not match foo", ap.matches(ae).alwaysFalse());
  268. ae = new AnnotatedElementImpl(new String[] { "Boo" });
  269. assertTrue("does not match boo", ap.matches(ae).alwaysFalse());
  270. ae = new AnnotatedElementImpl(new String[] { "Goo" });
  271. assertTrue("does not match goo", ap.matches(ae).alwaysFalse());
  272. }
  273. }
  274. //
  275. // public void testOrAnnotationPatternMatching() {
  276. // PatternParser p = new PatternParser("@Foo || @Boo");
  277. // AnnotationTypePattern ap = p.parseAnnotationTypePattern();
  278. // ap = ap.resolveBindings(makeSimpleScope(),new Bindings(3),true);
  279. // AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[]
  280. // {"Foo","Boo"});
  281. // assertTrue("matches foo and boo",ap.matches(ae).alwaysTrue());
  282. // ae = new AnnotatedElementImpl(new String[] {"Foo"});
  283. // assertTrue("matches foo",ap.matches(ae).alwaysTrue());
  284. // ae = new AnnotatedElementImpl(new String[] {"Boo"});
  285. // assertTrue("matches boo",ap.matches(ae).alwaysTrue());
  286. // ae = new AnnotatedElementImpl(new String[] {"Goo"});
  287. // assertTrue("does not match goo",ap.matches(ae).alwaysFalse());
  288. // }
  289. //
  290. public void testNotAnnotationPatternMatching() {
  291. if (LangUtil.is15VMOrGreater()) {
  292. PatternParser p = new PatternParser("!@Foo");
  293. AnnotationTypePattern ap = p.maybeParseAnnotationPattern();
  294. ap = ap.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  295. AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[] { "Foo", "Boo" });
  296. assertTrue("does not match foo and boo", ap.matches(ae).alwaysFalse());
  297. ae = new AnnotatedElementImpl(new String[] { "Boo" });
  298. assertTrue("matches boo", ap.matches(ae).alwaysTrue());
  299. }
  300. }
  301. public void testAnyAnnotationPatternMatching() {
  302. AnnotatedElementImpl ae = new AnnotatedElementImpl(new String[] { "Foo", "Boo" });
  303. assertTrue("always matches", AnnotationTypePattern.ANY.matches(ae).alwaysTrue());
  304. ae = new AnnotatedElementImpl(new String[] {});
  305. assertTrue("always matches", AnnotationTypePattern.ANY.matches(ae).alwaysTrue());
  306. }
  307. public TestScope makeSimpleScope() {
  308. BcelWorld bWorld = new BcelWorld(WeaverTestCase.TESTDATA_PATH + "/testcode.jar"); // testcode contains Foo/Boo/Goo/etc
  309. bWorld.setBehaveInJava5Way(true);
  310. return new TestScope(new String[] { "int", "java.lang.String", "Foo", "Boo", "Goo" }, new String[] { "a", "b", "foo",
  311. "boo", "goo" }, bWorld);
  312. }
  313. // put test cases for AnnotationPatternList matching in separate test
  314. // class...
  315. static class AnnotatedElementImpl implements AnnotatedElement {
  316. private String[] annotationTypes;
  317. public AnnotatedElementImpl(String[] annotationTypes) {
  318. this.annotationTypes = annotationTypes;
  319. }
  320. public boolean hasAnnotation(UnresolvedType ofType) {
  321. for (int i = 0; i < annotationTypes.length; i++) {
  322. if (annotationTypes[i].equals(ofType.getName())) {
  323. return true;
  324. }
  325. }
  326. return false;
  327. }
  328. /*
  329. * (non-Javadoc)
  330. *
  331. * @see org.aspectj.weaver.AnnotatedElement#getAnnotationTypes()
  332. */
  333. public ResolvedType[] getAnnotationTypes() {
  334. // TODO Auto-generated method stub
  335. return null;
  336. }
  337. public AnnotationAJ getAnnotationOfType(UnresolvedType ofType) {
  338. // TODO Auto-generated method stub
  339. return null;
  340. }
  341. }
  342. }