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.

AnnotationPatternMatchingTestCase.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. * Contributors:
  10. * Andy Clement initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import org.aspectj.bridge.AbortException;
  16. import org.aspectj.bridge.IMessage;
  17. import org.aspectj.bridge.IMessage.Kind;
  18. import org.aspectj.bridge.IMessageHandler;
  19. import org.aspectj.util.LangUtil;
  20. import org.aspectj.weaver.ResolvedMember;
  21. import org.aspectj.weaver.ResolvedType;
  22. import org.aspectj.weaver.WeaverTestCase;
  23. import org.aspectj.weaver.bcel.BcelWorld;
  24. import junit.framework.TestCase;
  25. /*
  26. * Sample types that this program uses are:
  27. import p.SimpleAnnotation;
  28. @SimpleAnnotation(id=2)
  29. public class AnnotatedClass {
  30. @SimpleAnnotation(id=3)
  31. public void m1() { }
  32. @SimpleAnnotation(id=4)
  33. int i;
  34. }
  35. * with SimpleAnnotation defined as:
  36. package p;
  37. import java.lang.annotation.*;
  38. @Retention(RetentionPolicy.RUNTIME)
  39. public @interface SimpleAnnotation {
  40. int id();
  41. String fruit() default "bananas";
  42. }
  43. *NOTE NOTE NOTE NOTE NOTE NOTE NOTE*
  44. If you need to rebuild the test data code, run 'ant -f build-15.xml' in the
  45. testdata directory.
  46. */
  47. public class AnnotationPatternMatchingTestCase extends TestCase {
  48. private BcelWorld world;
  49. private AnnotationTypePattern fooTP, simpleAnnotationTP;
  50. private ResolvedType loadType(String name) {
  51. if (world == null) {
  52. world = new BcelWorld(WeaverTestCase.TESTDATA_PATH + "/testcode.jar");
  53. world.setBehaveInJava5Way(true);
  54. }
  55. return world.resolve(name);
  56. }
  57. private void initAnnotationTypePatterns() {
  58. PatternParser p = new PatternParser("@Foo");
  59. fooTP = p.maybeParseAnnotationPattern();
  60. fooTP = fooTP.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  61. p = new PatternParser("@p.SimpleAnnotation");
  62. simpleAnnotationTP = p.maybeParseAnnotationPattern();
  63. simpleAnnotationTP = simpleAnnotationTP.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  64. }
  65. public void testAnnotationPatternMatchingOnTypes() {
  66. ResolvedType rtx = loadType("AnnotatedClass");
  67. initAnnotationTypePatterns();
  68. // One should match
  69. assertTrue("@Foo should not match on the AnnotatedClass", fooTP.matches(rtx).alwaysFalse());
  70. assertTrue("@SimpleAnnotation should match on the AnnotatedClass", simpleAnnotationTP.matches(rtx).alwaysTrue());
  71. }
  72. static class MyMessageHandler implements IMessageHandler {
  73. public List<IMessage> messages = new ArrayList<>();
  74. public boolean handleMessage(IMessage message) throws AbortException {
  75. messages.add(message);
  76. return false;
  77. }
  78. public boolean isIgnoring(Kind kind) {
  79. return false;
  80. }
  81. public void dontIgnore(IMessage.Kind kind) {
  82. }
  83. public void ignore(Kind kind) {
  84. }
  85. }
  86. public void testReferenceToNonAnnotationType() {
  87. // ResolvedType rtx =
  88. loadType("AnnotatedClass"); // inits the world
  89. PatternParser p = new PatternParser("@java.lang.String");
  90. MyMessageHandler mh = new MyMessageHandler();
  91. world.setMessageHandler(mh);
  92. AnnotationTypePattern atp = p.maybeParseAnnotationPattern();
  93. atp = atp.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  94. assertTrue("Expected 1 error message but got " + mh.messages.size(), mh.messages.size() == 1);
  95. String expected = "Type referred to is not an annotation type";
  96. String msg = ((IMessage) mh.messages.get(0)).toString();
  97. assertTrue("Expected: " + expected + " but got " + msg, msg.contains(expected));
  98. }
  99. public void testReferenceViaFormalToNonAnnotationType() {
  100. // ResolvedType rtx =
  101. loadType("AnnotatedClass"); // inits the world
  102. PatternParser p = new PatternParser("a");
  103. MyMessageHandler mh = new MyMessageHandler();
  104. world.setMessageHandler(mh);
  105. AnnotationTypePattern atp = p.parseAnnotationNameOrVarTypePattern();
  106. atp = atp.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  107. assertTrue("Expected 3 error messages but got " + mh.messages.size(), mh.messages.size() == 3);
  108. String expected = "Type referred to is not an annotation type";
  109. String msg = ((IMessage) mh.messages.get(0)).toString();
  110. assertTrue("Expected: " + expected + " but got " + msg, msg.contains(expected));
  111. // expected = "Binding not supported in @pcds (1.5.0 M1 limitation): null";
  112. // msg = ((IMessage)mh.messages.get(1)).toString();
  113. // assertTrue("Expected: "+expected+" but got "+msg,msg.indexOf(expected)!=-1);
  114. }
  115. public TestScope makeSimpleScope() {
  116. return new TestScope(new String[] { "int", "java.lang.String" }, new String[] { "a", "b" }, world);
  117. }
  118. public void testUnresolvedAnnotationTypes() {
  119. ResolvedType rtx = loadType("AnnotatedClass");
  120. PatternParser p = new PatternParser("@Foo");
  121. AnnotationTypePattern fooTP = p.maybeParseAnnotationPattern();
  122. try {
  123. fooTP.matches(rtx);
  124. fail("Should have failed with illegal state exception, fooTP is not resolved");
  125. } catch (IllegalStateException ise) {
  126. // Correct!
  127. }
  128. }
  129. public void testAnnotationPatternMatchingOnMethods() {
  130. ResolvedType rtx = loadType("AnnotatedClass");
  131. ResolvedMember aMethod = rtx.getDeclaredMethods()[1];
  132. assertTrue("Haven't got the right method, I'm looking for 'm1()': " + aMethod.getName(), aMethod.getName().equals("m1"));
  133. initAnnotationTypePatterns();
  134. // One should match
  135. assertTrue("@Foo should not match on the AnnotatedClass.m1() method", fooTP.matches(aMethod).alwaysFalse());
  136. assertTrue("@SimpleAnnotation should match on the AnnotatedClass.m1() method", simpleAnnotationTP.matches(aMethod)
  137. .alwaysTrue());
  138. }
  139. public void testAnnotationPatternMatchingOnFields() {
  140. ResolvedType rtx = loadType("AnnotatedClass");
  141. ResolvedMember aField = rtx.getDeclaredFields()[0];
  142. assertTrue("Haven't got the right field, I'm looking for 'i'" + aField.getName(), aField.getName().equals("i"));
  143. initAnnotationTypePatterns();
  144. // One should match
  145. assertTrue("@Foo should not match on the AnnotatedClass.i field", fooTP.matches(aField).alwaysFalse());
  146. assertTrue("@SimpleAnnotation should match on the AnnotatedClass.i field", simpleAnnotationTP.matches(aField)
  147. .alwaysTrue());
  148. }
  149. public void testAnnotationTypeResolutionOnTypes() {
  150. ResolvedType rtx = loadType("AnnotatedClass");
  151. ResolvedType[] types = rtx.getAnnotationTypes();
  152. assertTrue("Did not expect null", types != null);
  153. assertTrue("Expected 1 entry but got " + types.length, types.length == 1);
  154. assertTrue("Should be 'p.SimpleAnnotation' but is " + types[0], types[0].equals(world.resolve("p.SimpleAnnotation")));
  155. }
  156. public void testAnnotationTypeResolutionOnMethods() {
  157. ResolvedType rtx = loadType("AnnotatedClass");
  158. ResolvedMember aMethod = rtx.getDeclaredMethods()[1];
  159. assertTrue("Haven't got the right method, I'm looking for 'm1()': " + aMethod.getName(), aMethod.getName().equals("m1"));
  160. ResolvedType[] types = aMethod.getAnnotationTypes();
  161. assertTrue("Did not expect null", types != null);
  162. assertTrue("Expected 1 entry but got " + types.length, types.length == 1);
  163. assertTrue("Should be 'p.SimpleAnnotation' but is " + types[0], types[0].equals(world.resolve("p.SimpleAnnotation")));
  164. }
  165. public void testAnnotationTypeResolutionOnFields() {
  166. ResolvedType rtx = loadType("AnnotatedClass");
  167. ResolvedMember aField = rtx.getDeclaredFields()[0];
  168. assertTrue("Haven't got the right field, I'm looking for 'i'" + aField.getName(), aField.getName().equals("i"));
  169. ResolvedType[] types = aField.getAnnotationTypes();
  170. assertTrue("Did not expect null", types != null);
  171. assertTrue("Expected 1 entry but got " + types.length, types.length == 1);
  172. assertTrue("Should be 'p.SimpleAnnotation' but is " + types[0], types[0].equals(world.resolve("p.SimpleAnnotation")));
  173. }
  174. public void testWildPatternMatchingOnTypes() {
  175. ResolvedType rtx = loadType("AnnotatedClass");
  176. initAnnotationTypePatterns();
  177. // Let's create something wild
  178. PatternParser p = new PatternParser("@(Foo || Boo)");
  179. AnnotationTypePattern ap = p.maybeParseAnnotationPattern();
  180. ap = ap.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  181. assertTrue("shouldnt match the type AnnotatedClass", ap.matches(rtx).alwaysFalse());
  182. p = new PatternParser("@(p.SimpleAnnotation || Boo)");
  183. ap = p.maybeParseAnnotationPattern();
  184. ap = ap.resolveBindings(makeSimpleScope(), new Bindings(3), true);
  185. assertTrue("should match the type AnnotatedClass", ap.matches(rtx).alwaysTrue());
  186. }
  187. }