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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. protected TypePattern(boolean includeSubtypes) {
  78. this(includeSubtypes, false);
  79. }
  80. public void setAnnotationTypePattern(AnnotationTypePattern annPatt) {
  81. this.annotationPattern = annPatt;
  82. }
  83. public void setTypeParameters(TypePatternList typeParams) {
  84. this.typeParameters = typeParams;
  85. }
  86. public TypePatternList getTypeParameters() {
  87. return this.typeParameters;
  88. }
  89. public void setIsVarArgs(boolean isVarArgs) {
  90. this.isVarArgs = isVarArgs;
  91. }
  92. // answer conservatively...
  93. protected boolean couldEverMatchSameTypesAs(TypePattern other) {
  94. if (this.includeSubtypes || other.includeSubtypes) {
  95. return true;
  96. }
  97. if (this.annotationPattern != AnnotationTypePattern.ANY) {
  98. return true;
  99. }
  100. if (other.annotationPattern != AnnotationTypePattern.ANY) {
  101. return true;
  102. }
  103. return false;
  104. }
  105. // XXX non-final for Not, && and ||
  106. public boolean matchesStatically(ResolvedType type) {
  107. if (includeSubtypes) {
  108. return matchesSubtypes(type);
  109. } else {
  110. return matchesExactly(type);
  111. }
  112. }
  113. public abstract FuzzyBoolean matchesInstanceof(ResolvedType type);
  114. public final FuzzyBoolean matches(ResolvedType type, MatchKind kind) {
  115. // FuzzyBoolean typeMatch = null;
  116. // ??? This is part of gracefully handling missing references
  117. if (type.isMissing()) {
  118. return FuzzyBoolean.NO;
  119. }
  120. if (kind == STATIC) {
  121. return FuzzyBoolean.fromBoolean(matchesStatically(type));
  122. } else if (kind == DYNAMIC) {
  123. // System.err.println("matching: " + this + " with " + type);
  124. // typeMatch = matchesInstanceof(type);
  125. // System.err.println(" got: " + ret);
  126. // return typeMatch.and(annotationPattern.matches(type));
  127. return matchesInstanceof(type);
  128. } else {
  129. throw new IllegalArgumentException("kind must be DYNAMIC or STATIC");
  130. }
  131. }
  132. protected abstract boolean matchesExactly(ResolvedType type);
  133. protected abstract boolean matchesExactly(ResolvedType type, ResolvedType annotatedType);
  134. protected boolean matchesSubtypes(ResolvedType type) {
  135. // System.out.println("matching: " + this + " to " + type);
  136. if (matchesExactly(type)) {
  137. return true;
  138. }
  139. // pr124808
  140. Iterator<ResolvedType> typesIterator = null;
  141. if (type.isTypeVariableReference()) {
  142. typesIterator = ((TypeVariableReference) type).getTypeVariable().getFirstBound().resolve(type.getWorld())
  143. .getDirectSupertypes();
  144. } else {
  145. // pr223605
  146. if (type.isRawType()) {
  147. type = type.getGenericType();
  148. }
  149. typesIterator = type.getDirectSupertypes();
  150. }
  151. for (Iterator<ResolvedType> i = typesIterator; i.hasNext();) {
  152. ResolvedType superType = i.next();
  153. if (matchesSubtypes(superType, type)) {
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159. protected boolean matchesSubtypes(ResolvedType superType, ResolvedType annotatedType) {
  160. // System.out.println("matching2: " + this + " to " + superType);
  161. if (matchesExactly(superType, annotatedType)) {
  162. // System.out.println(" true");
  163. return true;
  164. }
  165. // If an ITD is applied, it will be put onto the generic type, not the parameterized or raw form
  166. if (superType.isParameterizedType() || superType.isRawType()) {
  167. superType = superType.getGenericType();
  168. }
  169. // FuzzyBoolean ret = FuzzyBoolean.NO; // ??? -eh
  170. for (Iterator<ResolvedType> i = superType.getDirectSupertypes(); i.hasNext();) {
  171. ResolvedType superSuperType = i.next();
  172. if (matchesSubtypes(superSuperType, annotatedType)) {
  173. return true;
  174. }
  175. }
  176. return false;
  177. }
  178. public UnresolvedType resolveExactType(IScope scope, Bindings bindings) {
  179. TypePattern p = resolveBindings(scope, bindings, false, true);
  180. if (!(p instanceof ExactTypePattern)) {
  181. return ResolvedType.MISSING;
  182. }
  183. return ((ExactTypePattern) p).getType();
  184. }
  185. public UnresolvedType getExactType() {
  186. if (this instanceof ExactTypePattern) {
  187. return ((ExactTypePattern) this).getType();
  188. } else {
  189. return ResolvedType.MISSING;
  190. }
  191. }
  192. protected TypePattern notExactType(IScope s) {
  193. s.getMessageHandler().handleMessage(
  194. MessageUtil.error(WeaverMessages.format(WeaverMessages.EXACT_TYPE_PATTERN_REQD), getSourceLocation()));
  195. return NO;
  196. }
  197. // public boolean assertExactType(IMessageHandler m) {
  198. // if (this instanceof ExactTypePattern) return true;
  199. //
  200. // //XXX should try harder to avoid multiple errors for one problem
  201. // m.handleMessage(MessageUtil.error("exact type pattern required", getSourceLocation()));
  202. // return false;
  203. // }
  204. /**
  205. * This can modify in place, or return a new TypePattern if the type changes.
  206. */
  207. public TypePattern resolveBindings(IScope scope, Bindings bindings, boolean allowBinding, boolean requireExactType) {
  208. annotationPattern = annotationPattern.resolveBindings(scope, bindings, allowBinding);
  209. return this;
  210. }
  211. public void resolve(World world) {
  212. annotationPattern.resolve(world);
  213. }
  214. /**
  215. * return a version of this type pattern in which all type variable references have been replaced by their corresponding entry
  216. * in the map.
  217. */
  218. public abstract TypePattern parameterizeWith(Map<String, UnresolvedType> typeVariableMap, World w);
  219. public void postRead(ResolvedType enclosingType) {
  220. }
  221. public boolean isEllipsis() {
  222. return false;
  223. }
  224. public boolean isStar() {
  225. return false;
  226. }
  227. /**
  228. * This is called during concretization of pointcuts, it is used by BindingTypePattern to return a new BindingTypePattern with a
  229. * formal index appropriate for the advice, rather than for the lexical declaration, i.e. this handles transformations through
  230. * named pointcuts.
  231. *
  232. * <pre>
  233. * pointcut foo(String name): args(name);
  234. * --&gt; This makes a BindingTypePattern(0) pointing to the 0th formal
  235. *
  236. * before(Foo f, String n): this(f) &amp;&amp; foo(n) { ... }
  237. * --&gt; when resolveReferences is called on the args from the above, it
  238. * will return a BindingTypePattern(1)
  239. *
  240. * before(Foo f): this(f) &amp;&amp; foo(*) { ... }
  241. * --&gt; when resolveReferences is called on the args from the above, it
  242. * will return an ExactTypePattern(String)
  243. * </pre>
  244. */
  245. public TypePattern remapAdviceFormals(IntMap bindings) {
  246. return this;
  247. }
  248. public static final byte WILD = 1;
  249. public static final byte EXACT = 2;
  250. public static final byte BINDING = 3;
  251. public static final byte ELLIPSIS_KEY = 4;
  252. public static final byte ANY_KEY = 5;
  253. public static final byte NOT = 6;
  254. public static final byte OR = 7;
  255. public static final byte AND = 8;
  256. public static final byte NO_KEY = 9;
  257. public static final byte ANY_WITH_ANNO = 10;
  258. public static final byte HAS_MEMBER = 11;
  259. public static final byte TYPE_CATEGORY = 12;
  260. public static TypePattern read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  261. byte key = s.readByte();
  262. switch (key) {
  263. case WILD:
  264. return WildTypePattern.read(s, context);
  265. case EXACT:
  266. return ExactTypePattern.read(s, context);
  267. case BINDING:
  268. return BindingTypePattern.read(s, context);
  269. case ELLIPSIS_KEY:
  270. return ELLIPSIS;
  271. case ANY_KEY:
  272. return ANY;
  273. case NO_KEY:
  274. return NO;
  275. case NOT:
  276. return NotTypePattern.read(s, context);
  277. case OR:
  278. return OrTypePattern.read(s, context);
  279. case AND:
  280. return AndTypePattern.read(s, context);
  281. case ANY_WITH_ANNO:
  282. return AnyWithAnnotationTypePattern.read(s, context);
  283. case HAS_MEMBER:
  284. return HasMemberTypePattern.read(s, context);
  285. case TYPE_CATEGORY:
  286. return TypeCategoryTypePattern.read(s, context);
  287. }
  288. throw new BCException("unknown TypePattern kind: " + key);
  289. }
  290. public boolean isIncludeSubtypes() {
  291. return includeSubtypes;
  292. }
  293. /**
  294. * For quickly recognizing the pattern '!void'
  295. */
  296. public boolean isBangVoid() {
  297. return false;
  298. }
  299. /**
  300. * for quickly recognizing the pattern 'void'
  301. */
  302. public boolean isVoid() {
  303. return false;
  304. }
  305. public boolean hasFailedResolution() {
  306. return false;
  307. }
  308. }