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.

DeclareParents.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* *******************************************************************
  2. * Copyright (c) 2002-2019 Contributors
  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. package org.aspectj.weaver.patterns;
  10. import java.io.IOException;
  11. import java.util.ArrayList;
  12. import java.util.Collections;
  13. import java.util.Iterator;
  14. import java.util.List;
  15. import java.util.Map;
  16. import org.aspectj.bridge.IMessage;
  17. import org.aspectj.bridge.ISourceLocation;
  18. import org.aspectj.bridge.Message;
  19. import org.aspectj.weaver.CompressingDataOutputStream;
  20. import org.aspectj.weaver.ISourceContext;
  21. import org.aspectj.weaver.ResolvedType;
  22. import org.aspectj.weaver.UnresolvedType;
  23. import org.aspectj.weaver.VersionedDataInputStream;
  24. import org.aspectj.weaver.WeaverMessages;
  25. import org.aspectj.weaver.World;
  26. /**
  27. * @author Thomas Kiviaho
  28. * @author Andy Clement
  29. * @author PARC
  30. */
  31. public class DeclareParents extends Declare {
  32. protected TypePattern child;
  33. protected TypePatternList parents;
  34. private boolean isWildChild = false;
  35. protected boolean isExtends = true;
  36. public DeclareParents(TypePattern child, List<TypePattern> parents, boolean isExtends) {
  37. this(child, new TypePatternList(parents), isExtends);
  38. }
  39. protected DeclareParents(TypePattern child, TypePatternList parents, boolean isExtends) {
  40. this.child = child;
  41. this.parents = parents;
  42. this.isExtends = isExtends;
  43. WildChildFinder wildChildFinder = new WildChildFinder();
  44. child.accept(wildChildFinder, null);
  45. isWildChild = wildChildFinder.containedWildChild();
  46. }
  47. public boolean match(ResolvedType typeX) {
  48. if (!child.matchesStatically(typeX)) {
  49. return false;
  50. }
  51. if (typeX.getWorld().getLint().typeNotExposedToWeaver.isEnabled() && !typeX.isExposedToWeaver()) {
  52. typeX.getWorld().getLint().typeNotExposedToWeaver.signal(typeX.getName(), getSourceLocation());
  53. }
  54. return true;
  55. }
  56. @Override
  57. public Object accept(PatternNodeVisitor visitor, Object data) {
  58. return visitor.visit(this, data);
  59. }
  60. public Object traverse(PatternNodeVisitor visitor, Object data) {
  61. Object ret = accept(visitor, data);
  62. if (this.child != null)
  63. this.child.traverse(visitor, ret);
  64. if (this.parents != null)
  65. this.parents.traverse(visitor, ret);
  66. return ret;
  67. }
  68. @Override
  69. public Declare parameterizeWith(Map<String,UnresolvedType> typeVariableBindingMap, World w) {
  70. DeclareParents ret = new DeclareParents(child.parameterizeWith(typeVariableBindingMap, w), parents.parameterizeWith(
  71. typeVariableBindingMap, w), isExtends);
  72. ret.copyLocationFrom(this);
  73. return ret;
  74. }
  75. @Override
  76. public String toString() {
  77. StringBuilder buf = new StringBuilder();
  78. buf.append("declare parents: ");
  79. buf.append(child);
  80. buf.append(isExtends ? " extends " : " implements "); // extends and implements are treated equivalently
  81. buf.append(parents);
  82. buf.append(";");
  83. return buf.toString();
  84. }
  85. @Override
  86. public boolean equals(Object other) {
  87. if (!(other instanceof DeclareParents)) {
  88. return false;
  89. }
  90. DeclareParents o = (DeclareParents) other;
  91. return o.child.equals(child) && o.parents.equals(parents);
  92. }
  93. // ??? cache this
  94. @Override
  95. public int hashCode() {
  96. int result = 23;
  97. result = 37 * result + child.hashCode();
  98. result = 37 * result + parents.hashCode();
  99. return result;
  100. }
  101. @Override
  102. public void write(CompressingDataOutputStream s) throws IOException {
  103. s.writeByte(Declare.PARENTS);
  104. child.write(s);
  105. parents.write(s);
  106. writeLocation(s);
  107. }
  108. public static Declare read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  109. DeclareParents ret = new DeclareParents(TypePattern.read(s, context), TypePatternList.read(s, context), true);
  110. ret.readLocation(context, s);
  111. return ret;
  112. }
  113. public boolean parentsIncludeInterface(World w) {
  114. for (int i = 0; i < parents.size(); i++) {
  115. if (parents.get(i).getExactType().resolve(w).isInterface()) {
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. public boolean parentsIncludeClass(World w) {
  122. for (int i = 0; i < parents.size(); i++) {
  123. if (parents.get(i).getExactType().resolve(w).isClass()) {
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. @Override
  130. public void resolve(IScope scope) {
  131. TypePattern resolvedChild = child.resolveBindings(scope, Bindings.NONE, false, false);
  132. if (!resolvedChild.equals(child)) {
  133. WildChildFinder wildChildFinder = new WildChildFinder();
  134. resolvedChild.accept(wildChildFinder, null);
  135. isWildChild = wildChildFinder.containedWildChild();
  136. this.child = resolvedChild;
  137. }
  138. parents = parents.resolveBindings(scope, Bindings.NONE, false, true);
  139. // Could assert this ...
  140. // for (int i=0; i < parents.size(); i++) {
  141. // parents.get(i).assertExactType(scope.getMessageHandler());
  142. // }
  143. }
  144. public TypePatternList getParents() {
  145. return parents;
  146. }
  147. public TypePattern getChild() {
  148. return child;
  149. }
  150. // note - will always return true after deserialization, this doesn't affect weaver
  151. public boolean isExtends() {
  152. return this.isExtends;
  153. }
  154. @Override
  155. public boolean isAdviceLike() {
  156. return false;
  157. }
  158. private ResolvedType maybeGetNewParent(ResolvedType targetType, TypePattern typePattern, World world, boolean reportErrors) {
  159. if (typePattern == TypePattern.NO) {
  160. return null; // already had an error here
  161. }
  162. // isWildChild = (child instanceof WildTypePattern);
  163. UnresolvedType iType = typePattern.getExactType();
  164. ResolvedType parentType = iType.resolve(world);
  165. if (targetType.equals(world.getCoreType(UnresolvedType.OBJECT))) {
  166. world.showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.DECP_OBJECT), this.getSourceLocation(), null);
  167. return null;
  168. }
  169. // Ensure the target doesn't already have an
  170. // alternate parameterization of the generic type on it
  171. if (parentType.isParameterizedType() || parentType.isRawType()) {
  172. // Let's take a look at the parents we already have
  173. boolean isOK = verifyNoInheritedAlternateParameterization(targetType, parentType, world);
  174. if (!isOK) {
  175. return null;
  176. }
  177. }
  178. if (parentType.isAssignableFrom(targetType)) {
  179. return null; // already a parent
  180. }
  181. // Enum types that are targetted for decp through a wild type pattern get linted
  182. if (reportErrors && isWildChild && targetType.isEnum()) {
  183. world.getLint().enumAsTargetForDecpIgnored.signal(targetType.toString(), getSourceLocation());
  184. }
  185. // Annotation types that are targetted for decp through a wild type pattern get linted
  186. if (reportErrors && isWildChild && targetType.isAnnotation()) {
  187. world.getLint().annotationAsTargetForDecpIgnored.signal(targetType.toString(), getSourceLocation());
  188. }
  189. // 1. Can't use decp to make an enum/annotation type implement an interface
  190. if (targetType.isEnum() && parentType.isInterface()) {
  191. if (reportErrors && !isWildChild) {
  192. world.showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.CANT_DECP_ON_ENUM_TO_IMPL_INTERFACE,
  193. targetType), getSourceLocation(), null);
  194. }
  195. return null;
  196. }
  197. if (targetType.isAnnotation() && parentType.isInterface()) {
  198. if (reportErrors && !isWildChild) {
  199. world.showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.CANT_DECP_ON_ANNOTATION_TO_IMPL_INTERFACE,
  200. targetType), getSourceLocation(), null);
  201. }
  202. return null;
  203. }
  204. // 2. Can't use decp to change supertype of an enum/annotation
  205. if (targetType.isEnum() && parentType.isClass()) {
  206. if (reportErrors && !isWildChild) {
  207. world.showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.CANT_DECP_ON_ENUM_TO_EXTEND_CLASS,
  208. targetType), getSourceLocation(), null);
  209. }
  210. return null;
  211. }
  212. if (targetType.isAnnotation() && parentType.isClass()) {
  213. if (reportErrors && !isWildChild) {
  214. world.showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.CANT_DECP_ON_ANNOTATION_TO_EXTEND_CLASS,
  215. targetType), getSourceLocation(), null);
  216. }
  217. return null;
  218. }
  219. // 3. Can't use decp to declare java.lang.Enum/java.lang.annotation.Annotation as the parent of a type
  220. if (parentType.getSignature().equals(UnresolvedType.ENUM.getSignature())) {
  221. if (reportErrors && !isWildChild) {
  222. world.showMessage(IMessage.ERROR, WeaverMessages
  223. .format(WeaverMessages.CANT_DECP_TO_MAKE_ENUM_SUPERTYPE, targetType), getSourceLocation(), null);
  224. }
  225. return null;
  226. }
  227. if (parentType.getSignature().equals(UnresolvedType.ANNOTATION.getSignature())) {
  228. if (reportErrors && !isWildChild) {
  229. world.showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.CANT_DECP_TO_MAKE_ANNOTATION_SUPERTYPE,
  230. targetType), getSourceLocation(), null);
  231. }
  232. return null;
  233. }
  234. if (parentType.isAssignableFrom(targetType)) {
  235. return null; // already a parent
  236. }
  237. if (targetType.isAssignableFrom(parentType)) {
  238. world.showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.CANT_EXTEND_SELF, targetType.getName()), this
  239. .getSourceLocation(), null);
  240. return null;
  241. }
  242. if (parentType.isClass()) {
  243. if (targetType.isInterface()) {
  244. world.showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.INTERFACE_CANT_EXTEND_CLASS), this
  245. .getSourceLocation(), null);
  246. return null;
  247. // how to handle xcutting errors???
  248. }
  249. if (!targetType.getSuperclass().isAssignableFrom(parentType)) {
  250. world.showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.DECP_HIERARCHY_ERROR, iType.getName(),
  251. targetType.getSuperclass().getName()), this.getSourceLocation(), null);
  252. return null;
  253. } else {
  254. return parentType;
  255. }
  256. } else {
  257. return parentType;
  258. }
  259. }
  260. /**
  261. * This method looks through the type hierarchy for some target type - it is attempting to find an existing parameterization
  262. * that clashes with the new parent that the user wants to apply to the type. If it finds an existing parameterization that
  263. * matches the new one, it silently completes, if it finds one that clashes (e.g. a type already has A<String> when the user
  264. * wants to add A<Number>) then it will produce an error.
  265. *
  266. * It uses recursion and exits recursion on hitting 'jlObject'
  267. *
  268. * Related bugzilla entries: pr110788
  269. */
  270. private boolean verifyNoInheritedAlternateParameterization(ResolvedType typeToVerify, ResolvedType newParent, World world) {
  271. if (typeToVerify.equals(ResolvedType.OBJECT)) {
  272. return true;
  273. }
  274. ResolvedType newParentGenericType = newParent.getGenericType();
  275. Iterator<ResolvedType> iter = typeToVerify.getDirectSupertypes();
  276. while (iter.hasNext()) {
  277. ResolvedType supertype = iter.next();
  278. if (((supertype.isRawType() && newParent.isParameterizedType()) || (supertype.isParameterizedType() && newParent
  279. .isRawType()))
  280. && newParentGenericType.equals(supertype.getGenericType())) {
  281. // new parent is a parameterized type, but this is a raw type
  282. world.getMessageHandler().handleMessage(
  283. new Message(WeaverMessages.format(WeaverMessages.CANT_DECP_MULTIPLE_PARAMETERIZATIONS, newParent.getName(),
  284. typeToVerify.getName(), supertype.getName()), getSourceLocation(), true,
  285. new ISourceLocation[] { typeToVerify.getSourceLocation() }));
  286. return false;
  287. }
  288. if (supertype.isParameterizedType()) {
  289. ResolvedType generictype = supertype.getGenericType();
  290. // If the generic types are compatible but the parameterizations aren't then we have a problem
  291. if (generictype.isAssignableFrom(newParentGenericType) && !supertype.isAssignableFrom(newParent)) {
  292. world.getMessageHandler().handleMessage(
  293. new Message(WeaverMessages.format(WeaverMessages.CANT_DECP_MULTIPLE_PARAMETERIZATIONS, newParent
  294. .getName(), typeToVerify.getName(), supertype.getName()), getSourceLocation(), true,
  295. new ISourceLocation[] { typeToVerify.getSourceLocation() }));
  296. return false;
  297. }
  298. }
  299. if (!verifyNoInheritedAlternateParameterization(supertype, newParent, world)) {
  300. return false;
  301. }
  302. }
  303. return true;
  304. }
  305. public List<ResolvedType> findMatchingNewParents(ResolvedType onType, boolean reportErrors) {
  306. if (onType.isRawType()) {
  307. onType = onType.getGenericType();
  308. }
  309. if (!match(onType)) {
  310. return Collections.emptyList();
  311. }
  312. List<ResolvedType> ret = new ArrayList<>();
  313. for (int i = 0; i < parents.size(); i++) {
  314. ResolvedType t = maybeGetNewParent(onType, parents.get(i), onType.getWorld(), reportErrors);
  315. if (t != null) {
  316. ret.add(t);
  317. }
  318. }
  319. return ret;
  320. }
  321. @Override
  322. public String getNameSuffix() {
  323. return "parents";
  324. }
  325. public boolean isMixin() {
  326. return false;
  327. }
  328. }