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.

ExactTypePattern.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* *******************************************************************
  2. * Copyright (c) 2002 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. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.DataInputStream;
  14. import java.io.IOException;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. import org.aspectj.util.FuzzyBoolean;
  18. import org.aspectj.weaver.AjAttribute;
  19. import org.aspectj.weaver.BCException;
  20. import org.aspectj.weaver.CompressingDataOutputStream;
  21. import org.aspectj.weaver.ISourceContext;
  22. import org.aspectj.weaver.ResolvedType;
  23. import org.aspectj.weaver.TypeVariableReference;
  24. import org.aspectj.weaver.TypeVariableReferenceType;
  25. import org.aspectj.weaver.UnresolvedType;
  26. import org.aspectj.weaver.VersionedDataInputStream;
  27. import org.aspectj.weaver.World;
  28. public class ExactTypePattern extends TypePattern {
  29. protected UnresolvedType type;
  30. protected transient ResolvedType resolvedType;
  31. public boolean checked = false;
  32. public boolean isVoid = false;
  33. public static final Map<String, Class<?>> primitiveTypesMap;
  34. public static final Map<String, Class<?>> boxedPrimitivesMap;
  35. private static final Map<String, Class<?>> boxedTypesMap;
  36. static {
  37. primitiveTypesMap = new HashMap<>();
  38. primitiveTypesMap.put("int", int.class);
  39. primitiveTypesMap.put("short", short.class);
  40. primitiveTypesMap.put("long", long.class);
  41. primitiveTypesMap.put("byte", byte.class);
  42. primitiveTypesMap.put("char", char.class);
  43. primitiveTypesMap.put("float", float.class);
  44. primitiveTypesMap.put("double", double.class);
  45. boxedPrimitivesMap = new HashMap<>();
  46. boxedPrimitivesMap.put("java.lang.Integer", Integer.class);
  47. boxedPrimitivesMap.put("java.lang.Short", Short.class);
  48. boxedPrimitivesMap.put("java.lang.Long", Long.class);
  49. boxedPrimitivesMap.put("java.lang.Byte", Byte.class);
  50. boxedPrimitivesMap.put("java.lang.Character", Character.class);
  51. boxedPrimitivesMap.put("java.lang.Float", Float.class);
  52. boxedPrimitivesMap.put("java.lang.Double", Double.class);
  53. boxedTypesMap = new HashMap<>();
  54. boxedTypesMap.put("int", Integer.class);
  55. boxedTypesMap.put("short", Short.class);
  56. boxedTypesMap.put("long", Long.class);
  57. boxedTypesMap.put("byte", Byte.class);
  58. boxedTypesMap.put("char", Character.class);
  59. boxedTypesMap.put("float", Float.class);
  60. boxedTypesMap.put("double", Double.class);
  61. }
  62. @Override
  63. protected boolean matchesSubtypes(ResolvedType type) {
  64. boolean match = super.matchesSubtypes(type);
  65. if (match) {
  66. return match;
  67. }
  68. // are we dealing with array funkyness - pattern might be jlObject[]+ and type jlString[]
  69. if (type.isArray() && this.type.isArray()) {
  70. ResolvedType componentType = type.getComponentType().resolve(type.getWorld());
  71. UnresolvedType newPatternType = this.type.getComponentType();
  72. ExactTypePattern etp = new ExactTypePattern(newPatternType, includeSubtypes, false);
  73. return etp.matchesSubtypes(componentType, type);
  74. }
  75. return match;
  76. }
  77. public ExactTypePattern(UnresolvedType type, boolean includeSubtypes, boolean isVarArgs) {
  78. super(includeSubtypes, isVarArgs);
  79. this.type = type;
  80. }
  81. @Override
  82. public boolean isArray() {
  83. return type.isArray();
  84. }
  85. /*
  86. * (non-Javadoc)
  87. *
  88. * @see org.aspectj.weaver.patterns.TypePattern#couldEverMatchSameTypesAs(org.aspectj.weaver.patterns.TypePattern)
  89. */
  90. @Override
  91. protected boolean couldEverMatchSameTypesAs(TypePattern other) {
  92. if (super.couldEverMatchSameTypesAs(other)) {
  93. return true;
  94. }
  95. // false is necessary but not sufficient
  96. UnresolvedType otherType = other.getExactType();
  97. if (!ResolvedType.isMissing(otherType)) {
  98. return type.equals(otherType);
  99. }
  100. if (other instanceof WildTypePattern) {
  101. WildTypePattern owtp = (WildTypePattern) other;
  102. String yourSimpleNamePrefix = owtp.getNamePatterns()[0].maybeGetSimpleName();
  103. if (yourSimpleNamePrefix != null) {
  104. return (type.getName().startsWith(yourSimpleNamePrefix));
  105. }
  106. }
  107. return true;
  108. }
  109. @Override
  110. protected boolean matchesExactly(ResolvedType matchType) {
  111. boolean typeMatch = this.type.equals(matchType);
  112. if (!typeMatch && (matchType.isParameterizedType() || matchType.isGenericType())) {
  113. typeMatch = this.type.equals(matchType.getRawType());
  114. }
  115. if (!typeMatch && matchType.isTypeVariableReference()) {
  116. typeMatch = matchesTypeVariable((TypeVariableReferenceType) matchType);
  117. }
  118. if (!typeMatch) {
  119. return false;
  120. }
  121. annotationPattern.resolve(matchType.getWorld());
  122. boolean annMatch = false;
  123. if (matchType.temporaryAnnotationTypes != null) {
  124. annMatch = annotationPattern.matches(matchType, matchType.temporaryAnnotationTypes).alwaysTrue();
  125. } else {
  126. annMatch = annotationPattern.matches(matchType).alwaysTrue();
  127. }
  128. return (typeMatch && annMatch);
  129. }
  130. private boolean matchesTypeVariable(TypeVariableReferenceType matchType) {
  131. // was this method previously coded to return false *on purpose* ?? pr124808
  132. return this.type.equals(((TypeVariableReference) matchType).getTypeVariable().getFirstBound());
  133. // return false;
  134. }
  135. @Override
  136. protected boolean matchesExactly(ResolvedType matchType, ResolvedType annotatedType) {
  137. boolean typeMatch = this.type.equals(matchType);
  138. if (!typeMatch && (matchType.isParameterizedType() || matchType.isGenericType())) {
  139. typeMatch = this.type.equals(matchType.getRawType());
  140. }
  141. if (!typeMatch && matchType.isTypeVariableReference()) {
  142. typeMatch = matchesTypeVariable((TypeVariableReferenceType) matchType);
  143. }
  144. annotationPattern.resolve(matchType.getWorld());
  145. boolean annMatch = false;
  146. if (annotatedType.temporaryAnnotationTypes != null) {
  147. annMatch = annotationPattern.matches(annotatedType, annotatedType.temporaryAnnotationTypes).alwaysTrue();
  148. } else {
  149. annMatch = annotationPattern.matches(annotatedType).alwaysTrue();
  150. }
  151. return (typeMatch && annMatch);
  152. }
  153. public UnresolvedType getType() {
  154. return type;
  155. }
  156. public ResolvedType getResolvedExactType(World world) {
  157. if (resolvedType == null) {
  158. resolvedType = world.resolve(type);
  159. }
  160. return resolvedType;
  161. }
  162. @Override
  163. public boolean isVoid() {
  164. if (!checked) {
  165. isVoid = this.type.getSignature().equals("V");
  166. checked = true;
  167. }
  168. return isVoid;
  169. }
  170. // true if (matchType instanceof this.type)
  171. @Override
  172. public FuzzyBoolean matchesInstanceof(ResolvedType matchType) {
  173. // in our world, Object is assignable from anything
  174. annotationPattern.resolve(matchType.getWorld());
  175. if (type.equals(ResolvedType.OBJECT)) {
  176. return FuzzyBoolean.YES.and(annotationPattern.matches(matchType));
  177. }
  178. ResolvedType resolvedType = type.resolve(matchType.getWorld());
  179. if (resolvedType.isAssignableFrom(matchType)) {
  180. return FuzzyBoolean.YES.and(annotationPattern.matches(matchType));
  181. }
  182. // fix for PR 64262 - shouldn't try to coerce primitives
  183. if (type.isPrimitiveType()) {
  184. return FuzzyBoolean.NO;
  185. } else {
  186. return matchType.isCoerceableFrom(type.resolve(matchType.getWorld())) ? FuzzyBoolean.MAYBE : FuzzyBoolean.NO;
  187. }
  188. }
  189. @Override
  190. public boolean equals(Object other) {
  191. if (!(other instanceof ExactTypePattern)) {
  192. return false;
  193. }
  194. if (other instanceof BindingTypePattern) {
  195. return false;
  196. }
  197. ExactTypePattern o = (ExactTypePattern) other;
  198. if (includeSubtypes != o.includeSubtypes) {
  199. return false;
  200. }
  201. if (isVarArgs != o.isVarArgs) {
  202. return false;
  203. }
  204. if (!typeParameters.equals(o.typeParameters)) {
  205. return false;
  206. }
  207. return (o.type.equals(this.type) && o.annotationPattern.equals(this.annotationPattern));
  208. }
  209. @Override
  210. public int hashCode() {
  211. int result = 17;
  212. result = 37 * result + type.hashCode();
  213. result = 37 * result + Boolean.valueOf(includeSubtypes).hashCode();
  214. result = 37 * result + Boolean.valueOf(isVarArgs).hashCode();
  215. result = 37 * result + typeParameters.hashCode();
  216. result = 37 * result + annotationPattern.hashCode();
  217. return result;
  218. }
  219. private static final byte EXACT_VERSION = 1; // rev if changed
  220. @Override
  221. public void write(CompressingDataOutputStream out) throws IOException {
  222. out.writeByte(TypePattern.EXACT);
  223. out.writeByte(EXACT_VERSION);
  224. out.writeCompressedSignature(type.getSignature());
  225. out.writeBoolean(includeSubtypes);
  226. out.writeBoolean(isVarArgs);
  227. annotationPattern.write(out);
  228. typeParameters.write(out);
  229. writeLocation(out);
  230. }
  231. public static TypePattern read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  232. if (s.getMajorVersion() >= AjAttribute.WeaverVersionInfo.WEAVER_VERSION_MAJOR_AJ150) {
  233. return readTypePattern150(s, context);
  234. } else {
  235. return readTypePatternOldStyle(s, context);
  236. }
  237. }
  238. public static TypePattern readTypePattern150(VersionedDataInputStream s, ISourceContext context) throws IOException {
  239. byte version = s.readByte();
  240. if (version > EXACT_VERSION) {
  241. throw new BCException("ExactTypePattern was written by a more recent version of AspectJ");
  242. }
  243. TypePattern ret = new ExactTypePattern(s.isAtLeast169() ? s.readSignatureAsUnresolvedType() : UnresolvedType.read(s), s
  244. .readBoolean(), s.readBoolean());
  245. ret.setAnnotationTypePattern(AnnotationTypePattern.read(s, context));
  246. ret.setTypeParameters(TypePatternList.read(s, context));
  247. ret.readLocation(context, s);
  248. return ret;
  249. }
  250. public static TypePattern readTypePatternOldStyle(DataInputStream s, ISourceContext context) throws IOException {
  251. TypePattern ret = new ExactTypePattern(UnresolvedType.read(s), s.readBoolean(), false);
  252. ret.readLocation(context, s);
  253. return ret;
  254. }
  255. @Override
  256. public String toString() {
  257. StringBuilder buff = new StringBuilder();
  258. if (annotationPattern != AnnotationTypePattern.ANY) {
  259. buff.append('(');
  260. buff.append(annotationPattern.toString());
  261. buff.append(' ');
  262. }
  263. String typeString = type.toString();
  264. if (isVarArgs) {
  265. typeString = typeString.substring(0, typeString.lastIndexOf('['));
  266. }
  267. buff.append(typeString);
  268. if (includeSubtypes) {
  269. buff.append('+');
  270. }
  271. if (isVarArgs) {
  272. buff.append("...");
  273. }
  274. if (annotationPattern != AnnotationTypePattern.ANY) {
  275. buff.append(')');
  276. }
  277. return buff.toString();
  278. }
  279. @Override
  280. public TypePattern resolveBindings(IScope scope, Bindings bindings, boolean allowBinding, boolean requireExactType) {
  281. throw new BCException("trying to re-resolve");
  282. }
  283. /**
  284. * return a version of this type pattern with all type variables references replaced by the corresponding entry in the map.
  285. */
  286. @Override
  287. public TypePattern parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
  288. UnresolvedType newType = type;
  289. if (type.isTypeVariableReference()) {
  290. TypeVariableReference t = (TypeVariableReference) type;
  291. String key = t.getTypeVariable().getName();
  292. if (typeVariableMap.containsKey(key)) {
  293. newType = typeVariableMap.get(key);
  294. }
  295. } else if (type.isParameterizedType()) {
  296. newType = w.resolve(type).parameterize(typeVariableMap);
  297. }
  298. ExactTypePattern ret = new ExactTypePattern(newType, includeSubtypes, isVarArgs);
  299. ret.annotationPattern = annotationPattern.parameterizeWith(typeVariableMap, w);
  300. ret.copyLocationFrom(this);
  301. return ret;
  302. }
  303. @Override
  304. public Object accept(PatternNodeVisitor visitor, Object data) {
  305. return visitor.visit(this, data);
  306. }
  307. }