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.

TypePattern.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* *******************************************************************
  2. * Copyright (c) 2002, 2010 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * Nieraj Singh
  12. * ******************************************************************/
  13. package org.aspectj.weaver.patterns;
  14. import java.io.IOException;
  15. import java.util.Iterator;
  16. import java.util.Map;
  17. import org.aspectj.bridge.MessageUtil;
  18. import org.aspectj.util.FuzzyBoolean;
  19. import org.aspectj.weaver.BCException;
  20. import org.aspectj.weaver.ISourceContext;
  21. import org.aspectj.weaver.IntMap;
  22. import org.aspectj.weaver.ResolvedType;
  23. import org.aspectj.weaver.TypeVariableReference;
  24. import org.aspectj.weaver.UnresolvedType;
  25. import org.aspectj.weaver.VersionedDataInputStream;
  26. import org.aspectj.weaver.WeaverMessages;
  27. import org.aspectj.weaver.World;
  28. /**
  29. * On creation, type pattern only contains WildTypePattern nodes, not BindingType or ExactType.
  30. *
  31. * <p>
  32. * Then we call resolveBindings() during compilation During concretization of enclosing pointcuts, we call remapAdviceFormals
  33. *
  34. * @author Erik Hilsdale
  35. * @author Jim Hugunin
  36. */
  37. public abstract class TypePattern extends PatternNode {
  38. public static class MatchKind {
  39. private String name;
  40. public MatchKind(String name) {
  41. this.name = name;
  42. }
  43. @Override
  44. public String toString() {
  45. return name;
  46. }
  47. }
  48. public static final MatchKind STATIC = new MatchKind("STATIC");
  49. public static final MatchKind DYNAMIC = new MatchKind("DYNAMIC");
  50. public static final TypePattern ELLIPSIS = new EllipsisTypePattern();
  51. public static final TypePattern ANY = new AnyTypePattern();
  52. public static final TypePattern NO = new NoTypePattern();
  53. protected boolean includeSubtypes;
  54. protected boolean isVarArgs = false;
  55. protected AnnotationTypePattern annotationPattern = AnnotationTypePattern.ANY;
  56. protected TypePatternList typeParameters = TypePatternList.EMPTY;
  57. protected TypePattern(boolean includeSubtypes, boolean isVarArgs, TypePatternList typeParams) {
  58. this.includeSubtypes = includeSubtypes;
  59. this.isVarArgs = isVarArgs;
  60. this.typeParameters = (typeParams == null ? TypePatternList.EMPTY : typeParams);
  61. }
  62. protected TypePattern(boolean includeSubtypes, boolean isVarArgs) {
  63. this(includeSubtypes, isVarArgs, null);
  64. }
  65. public AnnotationTypePattern getAnnotationPattern() {
  66. return annotationPattern;
  67. }
  68. public boolean isVarArgs() {
  69. return isVarArgs;
  70. }
  71. public boolean isStarAnnotation() {
  72. return annotationPattern == AnnotationTypePattern.ANY;
  73. }
  74. public boolean isArray() {
  75. return false;
  76. }
  77. public int getDimensions() {
  78. return 0;
  79. }
  80. protected TypePattern(boolean includeSubtypes) {
  81. this(includeSubtypes, false);
  82. }
  83. public void setAnnotationTypePattern(AnnotationTypePattern annPatt) {
  84. this.annotationPattern = annPatt;
  85. }
  86. public void setTypeParameters(TypePatternList typeParams) {
  87. this.typeParameters = typeParams;
  88. }
  89. public TypePatternList getTypeParameters() {
  90. return this.typeParameters;
  91. }
  92. public void setIsVarArgs(boolean isVarArgs) {
  93. this.isVarArgs = isVarArgs;
  94. }
  95. // answer conservatively...
  96. protected boolean couldEverMatchSameTypesAs(TypePattern other) {
  97. if (this.includeSubtypes || other.includeSubtypes) {
  98. return true;
  99. }
  100. if (this.annotationPattern != AnnotationTypePattern.ANY) {
  101. return true;
  102. }
  103. if (other.annotationPattern != AnnotationTypePattern.ANY) {
  104. return true;
  105. }
  106. return false;
  107. }
  108. // XXX non-final for Not, && and ||
  109. public boolean matchesStatically(ResolvedType type) {
  110. if (includeSubtypes) {
  111. return matchesSubtypes(type);
  112. } else {
  113. return matchesExactly(type);
  114. }
  115. }
  116. public abstract FuzzyBoolean matchesInstanceof(ResolvedType type);
  117. public final FuzzyBoolean matches(ResolvedType type, MatchKind kind) {
  118. // FuzzyBoolean typeMatch = null;
  119. // ??? This is part of gracefully handling missing references
  120. if (type.isMissing()) {
  121. return FuzzyBoolean.NO;
  122. }
  123. if (kind == STATIC) {
  124. return FuzzyBoolean.fromBoolean(matchesStatically(type));
  125. } else if (kind == DYNAMIC) {
  126. // System.err.println("matching: " + this + " with " + type);
  127. // typeMatch = matchesInstanceof(type);
  128. // System.err.println(" got: " + ret);
  129. // return typeMatch.and(annotationPattern.matches(type));
  130. return matchesInstanceof(type);
  131. } else {
  132. throw new IllegalArgumentException("kind must be DYNAMIC or STATIC");
  133. }
  134. }
  135. protected abstract boolean matchesExactly(ResolvedType type);
  136. protected abstract boolean matchesExactly(ResolvedType type, ResolvedType annotatedType);
  137. protected abstract boolean matchesArray(UnresolvedType type);
  138. protected boolean matchesSubtypes(ResolvedType type) {
  139. // System.out.println("matching: " + this + " to " + type);
  140. if (matchesExactly(type)) {
  141. return true;
  142. }
  143. // pr124808
  144. Iterator<ResolvedType> typesIterator = null;
  145. if (type.isTypeVariableReference()) {
  146. typesIterator = ((TypeVariableReference) type).getTypeVariable().getFirstBound().resolve(type.getWorld())
  147. .getDirectSupertypes();
  148. } else {
  149. // pr223605
  150. if (type.isRawType()) {
  151. type = type.getGenericType();
  152. }
  153. typesIterator = type.getDirectSupertypes();
  154. }
  155. for (Iterator<ResolvedType> i = typesIterator; i.hasNext();) {
  156. ResolvedType superType = i.next();
  157. if (matchesSubtypes(superType, type)) {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. protected boolean matchesSubtypes(ResolvedType superType, ResolvedType annotatedType) {
  164. // System.out.println("matching2: " + this + " to " + superType);
  165. if (matchesExactly(superType, annotatedType)) {
  166. // System.out.println(" true");
  167. return true;
  168. }
  169. // If an ITD is applied, it will be put onto the generic type, not the parameterized or raw form
  170. if (superType.isParameterizedType() || superType.isRawType()) {
  171. superType = superType.getGenericType();
  172. }
  173. // FuzzyBoolean ret = FuzzyBoolean.NO; // ??? -eh
  174. for (Iterator<ResolvedType> i = superType.getDirectSupertypes(); i.hasNext();) {
  175. ResolvedType superSuperType = i.next();
  176. if (matchesSubtypes(superSuperType, annotatedType)) {
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182. public UnresolvedType resolveExactType(IScope scope, Bindings bindings) {
  183. TypePattern p = resolveBindings(scope, bindings, false, true);
  184. if (!(p instanceof ExactTypePattern)) {
  185. return ResolvedType.MISSING;
  186. }
  187. return ((ExactTypePattern) p).getType();
  188. }
  189. public UnresolvedType getExactType() {
  190. if (this instanceof ExactTypePattern) {
  191. return ((ExactTypePattern) this).getType();
  192. } else {
  193. return ResolvedType.MISSING;
  194. }
  195. }
  196. protected TypePattern notExactType(IScope s) {
  197. s.getMessageHandler().handleMessage(
  198. MessageUtil.error(WeaverMessages.format(WeaverMessages.EXACT_TYPE_PATTERN_REQD), getSourceLocation()));
  199. return NO;
  200. }
  201. // public boolean assertExactType(IMessageHandler m) {
  202. // if (this instanceof ExactTypePattern) return true;
  203. //
  204. // //XXX should try harder to avoid multiple errors for one problem
  205. // m.handleMessage(MessageUtil.error("exact type pattern required", getSourceLocation()));
  206. // return false;
  207. // }
  208. /**
  209. * This can modify in place, or return a new TypePattern if the type changes.
  210. */
  211. public TypePattern resolveBindings(IScope scope, Bindings bindings, boolean allowBinding, boolean requireExactType) {
  212. annotationPattern = annotationPattern.resolveBindings(scope, bindings, allowBinding);
  213. return this;
  214. }
  215. public void resolve(World world) {
  216. annotationPattern.resolve(world);
  217. }
  218. /**
  219. * return a version of this type pattern in which all type variable references have been replaced by their corresponding entry
  220. * in the map.
  221. */
  222. public abstract TypePattern parameterizeWith(Map<String, UnresolvedType> typeVariableMap, World w);
  223. public void postRead(ResolvedType enclosingType) {
  224. }
  225. public boolean isEllipsis() {
  226. return false;
  227. }
  228. public boolean isStar() {
  229. return false;
  230. }
  231. /**
  232. * This is called during concretization of pointcuts, it is used by BindingTypePattern to return a new BindingTypePattern with a
  233. * formal index appropriate for the advice, rather than for the lexical declaration, i.e. this handles transformations through
  234. * named pointcuts.
  235. *
  236. * <pre>
  237. * pointcut foo(String name): args(name);
  238. * --&gt; This makes a BindingTypePattern(0) pointing to the 0th formal
  239. *
  240. * before(Foo f, String n): this(f) &amp;&amp; foo(n) { ... }
  241. * --&gt; when resolveReferences is called on the args from the above, it
  242. * will return a BindingTypePattern(1)
  243. *
  244. * before(Foo f): this(f) &amp;&amp; foo(*) { ... }
  245. * --&gt; when resolveReferences is called on the args from the above, it
  246. * will return an ExactTypePattern(String)
  247. * </pre>
  248. */
  249. public TypePattern remapAdviceFormals(IntMap bindings) {
  250. return this;
  251. }
  252. public static final byte WILD = 1;
  253. public static final byte EXACT = 2;
  254. public static final byte BINDING = 3;
  255. public static final byte ELLIPSIS_KEY = 4;
  256. public static final byte ANY_KEY = 5;
  257. public static final byte NOT = 6;
  258. public static final byte OR = 7;
  259. public static final byte AND = 8;
  260. public static final byte NO_KEY = 9;
  261. public static final byte ANY_WITH_ANNO = 10;
  262. public static final byte HAS_MEMBER = 11;
  263. public static final byte TYPE_CATEGORY = 12;
  264. public static TypePattern read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  265. byte key = s.readByte();
  266. switch (key) {
  267. case WILD:
  268. return WildTypePattern.read(s, context);
  269. case EXACT:
  270. return ExactTypePattern.read(s, context);
  271. case BINDING:
  272. return BindingTypePattern.read(s, context);
  273. case ELLIPSIS_KEY:
  274. return ELLIPSIS;
  275. case ANY_KEY:
  276. return ANY;
  277. case NO_KEY:
  278. return NO;
  279. case NOT:
  280. return NotTypePattern.read(s, context);
  281. case OR:
  282. return OrTypePattern.read(s, context);
  283. case AND:
  284. return AndTypePattern.read(s, context);
  285. case ANY_WITH_ANNO:
  286. return AnyWithAnnotationTypePattern.read(s, context);
  287. case HAS_MEMBER:
  288. return HasMemberTypePattern.read(s, context);
  289. case TYPE_CATEGORY:
  290. return TypeCategoryTypePattern.read(s, context);
  291. }
  292. throw new BCException("unknown TypePattern kind: " + key);
  293. }
  294. public boolean isIncludeSubtypes() {
  295. return includeSubtypes;
  296. }
  297. /**
  298. * For quickly recognizing the pattern '!void'
  299. */
  300. public boolean isBangVoid() {
  301. return false;
  302. }
  303. /**
  304. * for quickly recognizing the pattern 'void'
  305. */
  306. public boolean isVoid() {
  307. return false;
  308. }
  309. public boolean hasFailedResolution() {
  310. return false;
  311. }
  312. @Override
  313. public Object traverse(PatternNodeVisitor visitor, Object data) {
  314. Object ret = accept(visitor, data);
  315. if (annotationPattern != null)
  316. annotationPattern.traverse(visitor, ret);
  317. if (typeParameters != null)
  318. typeParameters.traverse(visitor, ret);
  319. return ret;
  320. }
  321. }