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.

AjASTMatcher.java 12KB

19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2010 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. * Nieraj Singh
  11. *******************************************************************************/
  12. package org.aspectj.org.eclipse.jdt.core.dom;
  13. public class AjASTMatcher extends ASTMatcher {
  14. /**
  15. * Creates a new AST matcher instance.
  16. * <p>
  17. * For backwards compatibility, the matcher ignores tag elements below doc comments by default. Use {@link #AjASTMatcher(boolean)
  18. * AjASTMatcher(true)} for a matcher that compares doc tags by default.
  19. * </p>
  20. */
  21. public AjASTMatcher() {
  22. this(false);
  23. }
  24. /**
  25. * Creates a new AST matcher instance.
  26. *
  27. * @param matchDocTags <code>true</code> if doc comment tags are to be compared by default, and <code>false</code> otherwise
  28. * @see #match(Javadoc,Object)
  29. * @since 3.0
  30. */
  31. public AjASTMatcher(boolean matchDocTags) {
  32. super(matchDocTags);
  33. }
  34. public boolean match(PointcutDeclaration node, Object other) {
  35. // ajh02: method added
  36. if (!(other instanceof PointcutDeclaration)) {
  37. return false;
  38. }
  39. PointcutDeclaration o = (PointcutDeclaration) other;
  40. int level = node.getAST().apiLevel;
  41. if (level == AST.JLS2_INTERNAL) {
  42. if (node.getModifiers() != o.getModifiers()) {
  43. return false;
  44. }
  45. }
  46. if (level >= AST.JLS3) {
  47. if (!safeSubtreeListMatch(node.modifiers(), o.modifiers())) {
  48. return false;
  49. }
  50. }
  51. return safeSubtreeMatch(node.getJavadoc(), o.getJavadoc()) && safeSubtreeMatch(node.getName(), o.getName())
  52. && safeSubtreeMatch(node.getDesignator(), o.getDesignator());
  53. }
  54. public boolean match(DefaultPointcut node, Object other) {
  55. if (!(other instanceof DefaultPointcut)) {
  56. return false;
  57. }
  58. return node.getDetail().equals(((DefaultPointcut) other).getDetail());
  59. }
  60. public boolean match(ReferencePointcut node, Object other) {
  61. if (!(other instanceof ReferencePointcut)) {
  62. return false;
  63. }
  64. ReferencePointcut o = (ReferencePointcut) other;
  65. // int level = node.getAST().apiLevel;
  66. return safeSubtreeMatch(node.getName(), o.getName());
  67. // ajh02: will have to add something here when ReferencePointcuts are given
  68. // a list of Types for parameters
  69. }
  70. public boolean match(NotPointcut node, Object other) {
  71. if (!(other instanceof NotPointcut)) {
  72. return false;
  73. }
  74. NotPointcut o = (NotPointcut) other;
  75. return safeSubtreeMatch(node.getBody(), o.getBody());
  76. }
  77. public boolean match(PerObject node, Object other) {
  78. if (!(other instanceof PerObject)) {
  79. return false;
  80. }
  81. PerObject o = (PerObject) other;
  82. return safeSubtreeMatch(node.getBody(), o.getBody()) && o.isThis() == node.isThis();
  83. }
  84. public boolean match(PerCflow node, Object other) {
  85. if (!(other instanceof PerCflow)) {
  86. return false;
  87. }
  88. PerCflow o = (PerCflow) other;
  89. return safeSubtreeMatch(node.getBody(), o.getBody()) && node.isBelow() == o.isBelow();
  90. }
  91. public boolean match(PerTypeWithin node, Object other) {
  92. if (!(other instanceof PerTypeWithin)) {
  93. return false;
  94. }
  95. // PerTypeWithin o = (PerTypeWithin) other;
  96. return true; // ajh02: stub, should look at the type pattern
  97. }
  98. public boolean match(CflowPointcut node, Object other) {
  99. if (!(other instanceof CflowPointcut)) {
  100. return false;
  101. }
  102. CflowPointcut o = (CflowPointcut) other;
  103. return safeSubtreeMatch(node.getBody(), o.getBody());
  104. }
  105. public boolean match(AndPointcut node, Object other) {
  106. if (!(other instanceof AndPointcut)) {
  107. return false;
  108. }
  109. AndPointcut o = (AndPointcut) other;
  110. return safeSubtreeMatch(node.getLeft(), o.getLeft()) && safeSubtreeMatch(node.getRight(), o.getRight());
  111. }
  112. public boolean match(OrPointcut node, Object other) {
  113. if (!(other instanceof OrPointcut)) {
  114. return false;
  115. }
  116. OrPointcut o = (OrPointcut) other;
  117. return safeSubtreeMatch(node.getLeft(), o.getLeft()) && safeSubtreeMatch(node.getRight(), o.getRight());
  118. }
  119. public boolean match(BeforeAdviceDeclaration node, Object other) {
  120. // ajh02: method added
  121. if (!(other instanceof BeforeAdviceDeclaration)) {
  122. return false;
  123. }
  124. BeforeAdviceDeclaration o = (BeforeAdviceDeclaration) other;
  125. return safeSubtreeMatch(node.getJavadoc(), o.getJavadoc()) && safeSubtreeListMatch(node.parameters(), o.parameters())
  126. && safeSubtreeMatch(node.getPointcut(), o.getPointcut())
  127. && safeSubtreeListMatch(node.thrownExceptions(), o.thrownExceptions())
  128. && safeSubtreeMatch(node.getBody(), o.getBody());
  129. }
  130. public boolean match(AfterAdviceDeclaration node, Object other) {
  131. // ajh02: todo: should have special methods to match
  132. // afterReturning and afterThrowing
  133. if (!(other instanceof AfterAdviceDeclaration)) {
  134. return false;
  135. }
  136. AfterAdviceDeclaration o = (AfterAdviceDeclaration) other;
  137. return safeSubtreeMatch(node.getJavadoc(), o.getJavadoc()) && safeSubtreeListMatch(node.parameters(), o.parameters())
  138. && safeSubtreeMatch(node.getPointcut(), o.getPointcut())
  139. && safeSubtreeListMatch(node.thrownExceptions(), o.thrownExceptions())
  140. && safeSubtreeMatch(node.getBody(), o.getBody());
  141. }
  142. public boolean match(AroundAdviceDeclaration node, Object other) {
  143. if (!(other instanceof AroundAdviceDeclaration)) {
  144. return false;
  145. }
  146. AroundAdviceDeclaration o = (AroundAdviceDeclaration) other;
  147. int level = node.getAST().apiLevel;
  148. if (level == AST.JLS2_INTERNAL) {
  149. if (!safeSubtreeMatch(node.internalGetReturnType(), o.internalGetReturnType())) {
  150. return false;
  151. }
  152. }
  153. if (level >= AST.JLS3) {
  154. if (!safeSubtreeMatch(node.getReturnType2(), o.getReturnType2())) {
  155. return false;
  156. }
  157. if (!safeSubtreeListMatch(node.typeParameters(), o.typeParameters())) {
  158. return false;
  159. }
  160. }
  161. return safeSubtreeMatch(node.getJavadoc(), o.getJavadoc()) && safeSubtreeListMatch(node.parameters(), o.parameters())
  162. && safeSubtreeMatch(node.getPointcut(), o.getPointcut())
  163. && safeSubtreeListMatch(node.thrownExceptions(), o.thrownExceptions())
  164. && safeSubtreeMatch(node.getBody(), o.getBody());
  165. }
  166. public boolean match(DeclareDeclaration node, Object other) {
  167. // ajh02: method added
  168. if (!(other instanceof DeclareDeclaration)) {
  169. return false;
  170. }
  171. DeclareDeclaration o = (DeclareDeclaration) other;
  172. // int level = node.getAST().apiLevel;
  173. return safeSubtreeMatch(node.getJavadoc(), o.getJavadoc());
  174. }
  175. public boolean match(InterTypeFieldDeclaration node, Object other) {
  176. // ajh02: method added
  177. if (!(other instanceof InterTypeFieldDeclaration)) {
  178. return false;
  179. }
  180. InterTypeFieldDeclaration o = (InterTypeFieldDeclaration) other;
  181. int level = node.getAST().apiLevel;
  182. if (level == AST.JLS2_INTERNAL) {
  183. if (node.getModifiers() != o.getModifiers()) {
  184. return false;
  185. }
  186. }
  187. if (level >= AST.JLS3) {
  188. if (!safeSubtreeListMatch(node.modifiers(), o.modifiers())) {
  189. return false;
  190. }
  191. }
  192. return safeSubtreeMatch(node.getJavadoc(), o.getJavadoc()) && safeSubtreeMatch(node.getType(), o.getType())
  193. && safeSubtreeListMatch(node.fragments(), o.fragments());
  194. }
  195. public boolean match(InterTypeMethodDeclaration node, Object other) {
  196. // ajh02: method added
  197. if (!(other instanceof InterTypeMethodDeclaration)) {
  198. return false;
  199. }
  200. InterTypeMethodDeclaration o = (InterTypeMethodDeclaration) other;
  201. int level = node.getAST().apiLevel;
  202. if (level == AST.JLS2_INTERNAL) {
  203. if (node.getModifiers() != o.getModifiers()) {
  204. return false;
  205. }
  206. if (!safeSubtreeMatch(node.internalGetReturnType(), o.internalGetReturnType())) {
  207. return false;
  208. }
  209. }
  210. if (level >= AST.JLS3) {
  211. if (!safeSubtreeListMatch(node.modifiers(), o.modifiers())) {
  212. return false;
  213. }
  214. if (!safeSubtreeMatch(node.getReturnType2(), o.getReturnType2())) {
  215. return false;
  216. }
  217. // n.b. compare type parameters even for constructors
  218. if (!safeSubtreeListMatch(node.typeParameters(), o.typeParameters())) {
  219. return false;
  220. }
  221. }
  222. return ((node.isConstructor() == o.isConstructor()) && safeSubtreeMatch(node.getJavadoc(), o.getJavadoc())
  223. && safeSubtreeMatch(node.getName(), o.getName())
  224. // n.b. compare return type even for constructors
  225. && safeSubtreeListMatch(node.parameters(), o.parameters()) && node.getExtraDimensions() == o.getExtraDimensions()
  226. && safeSubtreeListMatch(node.thrownExceptions(), o.thrownExceptions()) && safeSubtreeMatch(node.getBody(), o
  227. .getBody()));
  228. }
  229. public boolean match(DefaultTypePattern node, Object other) {
  230. if (!(other instanceof DefaultTypePattern)) {
  231. return false;
  232. }
  233. return node.getDetail().equals(((DefaultTypePattern) other).getDetail());
  234. }
  235. public boolean match(SignaturePattern node, Object other) {
  236. if (!(other instanceof SignaturePattern)) {
  237. return false;
  238. }
  239. return node.getDetail().equals(((SignaturePattern) other).getDetail());
  240. }
  241. public boolean match(AndTypePattern node, Object other) {
  242. if (node == other) {
  243. return true;
  244. }
  245. if (!(other instanceof AndTypePattern)) {
  246. return false;
  247. }
  248. AndTypePattern otherBoolean = (AndTypePattern) other;
  249. return safeSubtreeMatch(node.getLeft(), otherBoolean.getLeft())
  250. && safeSubtreeMatch(node.getRight(), otherBoolean.getRight());
  251. }
  252. public boolean match(OrTypePattern node, Object other) {
  253. if (node == other) {
  254. return true;
  255. }
  256. if (!(other instanceof OrTypePattern)) {
  257. return false;
  258. }
  259. OrTypePattern otherBoolean = (OrTypePattern) other;
  260. return safeSubtreeMatch(node.getLeft(), otherBoolean.getLeft())
  261. && safeSubtreeMatch(node.getRight(), otherBoolean.getRight());
  262. }
  263. public boolean match(AnyTypePattern node, Object other) {
  264. // AnyTypePattern nodes don't hold state aside from the AST, so just do a reference check
  265. if (node == other) {
  266. return true;
  267. }
  268. return false;
  269. }
  270. public boolean match(AnyWithAnnotationTypePattern node, Object other) {
  271. if (node == other) {
  272. return true;
  273. }
  274. if (!(other instanceof AnyWithAnnotationTypePattern)) {
  275. return false;
  276. }
  277. // For now only do an expression matching. In future versions, when
  278. // the node supports AnnotationTypes, this may have to be changed
  279. return node.getTypePatternExpression().equals(
  280. ((AnyWithAnnotationTypePattern) other)
  281. .getTypePatternExpression());
  282. }
  283. public boolean match(EllipsisTypePattern node, Object other) {
  284. // Ellipsis nodes don't hold state aside from the AST, so just do a reference check
  285. if (node == other) {
  286. return true;
  287. }
  288. return false;
  289. }
  290. public boolean match(NotTypePattern node, Object other) {
  291. if (node == other) {
  292. return true;
  293. }
  294. if (!(other instanceof NotTypePattern)) {
  295. return false;
  296. }
  297. return safeSubtreeMatch(node.getNegatedTypePattern(),
  298. ((NotTypePattern) other).getNegatedTypePattern());
  299. }
  300. public boolean match(NoTypePattern node, Object other) {
  301. // NoTypePattern nodes don't hold state aside from the AST, so just do a reference check
  302. if (node == other) {
  303. return true;
  304. }
  305. return false;
  306. }
  307. public boolean match(HasMemberTypePattern node, Object other) {
  308. if (node == other) {
  309. return true;
  310. }
  311. if (!(other instanceof HasMemberTypePattern)) {
  312. return false;
  313. }
  314. return safeSubtreeMatch(node.getSignaturePattern(),
  315. ((HasMemberTypePattern) other).getSignaturePattern());
  316. }
  317. public boolean match(IdentifierTypePattern node, Object other) {
  318. if (node == other) {
  319. return true;
  320. }
  321. if (!(other instanceof IdentifierTypePattern)) {
  322. return false;
  323. }
  324. return safeSubtreeMatch(node.getType(),
  325. ((IdentifierTypePattern) other).getType());
  326. }
  327. public boolean match(TypeCategoryTypePattern node, Object other) {
  328. if (node == other) {
  329. return true;
  330. }
  331. if (!(other instanceof TypeCategoryTypePattern)) {
  332. return false;
  333. }
  334. return node.getTypeCategory() == ((TypeCategoryTypePattern) other)
  335. .getTypeCategory();
  336. }
  337. public boolean match(Type type, Object other) {
  338. if (type == other) {
  339. return true;
  340. }
  341. // For now only support simple type/simple name matching. Support for
  342. // other types
  343. // may have to be added here
  344. if (type instanceof SimpleType && other instanceof SimpleType) {
  345. Name name = ((SimpleType) type).getName();
  346. Name otherName = ((SimpleType) other).getName();
  347. if (name instanceof SimpleName && otherName instanceof SimpleName) {
  348. return ((SimpleName) name).getIdentifier().equals(
  349. ((SimpleName) otherName).getIdentifier());
  350. }
  351. }
  352. return false;
  353. }
  354. }