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.

TypePatternList.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.DataOutputStream;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.Collection;
  17. import java.util.List;
  18. import org.aspectj.util.FuzzyBoolean;
  19. import org.aspectj.weaver.ISourceContext;
  20. import org.aspectj.weaver.IntMap;
  21. import org.aspectj.weaver.ResolvedTypeX;
  22. import org.aspectj.weaver.TypeX;
  23. import org.aspectj.weaver.VersionedDataInputStream;
  24. public class TypePatternList extends PatternNode {
  25. private TypePattern[] typePatterns;
  26. int ellipsisCount = 0;
  27. public static final TypePatternList EMPTY =
  28. new TypePatternList(new TypePattern[] {});
  29. public static final TypePatternList ANY =
  30. new TypePatternList(new TypePattern[] {TypePattern.ELLIPSIS});
  31. public TypePatternList() {
  32. typePatterns = new TypePattern[0];
  33. ellipsisCount = 0;
  34. }
  35. public TypePatternList(TypePattern[] arguments) {
  36. this.typePatterns = arguments;
  37. for (int i=0; i<arguments.length; i++) {
  38. if (arguments[i] == TypePattern.ELLIPSIS) ellipsisCount++;
  39. }
  40. }
  41. public TypePatternList(List l) {
  42. this((TypePattern[]) l.toArray(new TypePattern[l.size()]));
  43. }
  44. public int size() { return typePatterns.length; }
  45. public TypePattern get(int index) {
  46. return typePatterns[index];
  47. }
  48. public String toString() {
  49. StringBuffer buf = new StringBuffer();
  50. buf.append("(");
  51. for (int i=0, len=typePatterns.length; i < len; i++) {
  52. TypePattern type = typePatterns[i];
  53. if (i > 0) buf.append(", ");
  54. if (type == TypePattern.ELLIPSIS) {
  55. buf.append("..");
  56. } else {
  57. buf.append(type.toString());
  58. }
  59. }
  60. buf.append(")");
  61. return buf.toString();
  62. }
  63. /**
  64. * Used by reflection-based matching for args pcds.
  65. * Returns YES if types will always be matched by the pattern,
  66. * NO if types do not match the pattern,
  67. * MAYBE if types may match the pattern dependent on a runtime test
  68. */
  69. public FuzzyBoolean matchesArgsPatternSubset(Class[] types) {
  70. int argsLength = types.length;
  71. int patternLength = typePatterns.length;
  72. int argsIndex = 0;
  73. if ((argsLength < patternLength) && (ellipsisCount == 0)) return FuzzyBoolean.NO;
  74. if (argsLength < (patternLength -1)) return FuzzyBoolean.NO;
  75. int ellipsisMatchCount = argsLength - (patternLength - ellipsisCount);
  76. FuzzyBoolean ret = FuzzyBoolean.YES;
  77. for (int i = 0; i < typePatterns.length; i++) {
  78. if (typePatterns[i] == TypePattern.ELLIPSIS) {
  79. // match ellipsisMatchCount args
  80. argsIndex += ellipsisMatchCount;
  81. } else if (typePatterns[i] == TypePattern.ANY) {
  82. argsIndex++;
  83. } else {
  84. // match the argument type at argsIndex with the ExactTypePattern
  85. // we it is exact because nothing else is allowed in args
  86. ExactTypePattern tp = (ExactTypePattern)typePatterns[i];
  87. FuzzyBoolean matches = tp.willMatchDynamically(types[argsIndex]);
  88. if (matches == FuzzyBoolean.NO) {
  89. return FuzzyBoolean.NO;
  90. } else {
  91. argsIndex++;
  92. ret = ret.and(matches);
  93. }
  94. }
  95. }
  96. return ret;
  97. }
  98. //XXX shares much code with WildTypePattern and with NamePattern
  99. /**
  100. * When called with TypePattern.STATIC this will always return either
  101. * FuzzyBoolean.YES or FuzzyBoolean.NO.
  102. *
  103. * When called with TypePattern.DYNAMIC this could return MAYBE if
  104. * at runtime it would be possible for arguments of the given static
  105. * types to dynamically match this, but it is not known for certain.
  106. *
  107. * This method will never return FuzzyBoolean.NEVER
  108. */
  109. public FuzzyBoolean matches(ResolvedTypeX[] types, TypePattern.MatchKind kind) {
  110. int nameLength = types.length;
  111. int patternLength = typePatterns.length;
  112. int nameIndex = 0;
  113. int patternIndex = 0;
  114. if (ellipsisCount == 0) {
  115. if (nameLength != patternLength) return FuzzyBoolean.NO;
  116. FuzzyBoolean finalReturn = FuzzyBoolean.YES;
  117. while (patternIndex < patternLength) {
  118. FuzzyBoolean ret = typePatterns[patternIndex++].matches(types[nameIndex++], kind);
  119. if (ret == FuzzyBoolean.NO) return ret;
  120. if (ret == FuzzyBoolean.MAYBE) finalReturn = ret;
  121. }
  122. return finalReturn;
  123. } else if (ellipsisCount == 1) {
  124. if (nameLength < patternLength-1) return FuzzyBoolean.NO;
  125. FuzzyBoolean finalReturn = FuzzyBoolean.YES;
  126. while (patternIndex < patternLength) {
  127. TypePattern p = typePatterns[patternIndex++];
  128. if (p == TypePattern.ELLIPSIS) {
  129. nameIndex = nameLength - (patternLength-patternIndex);
  130. } else {
  131. FuzzyBoolean ret = p.matches(types[nameIndex++], kind);
  132. if (ret == FuzzyBoolean.NO) return ret;
  133. if (ret == FuzzyBoolean.MAYBE) finalReturn = ret;
  134. }
  135. }
  136. return finalReturn;
  137. } else {
  138. // System.err.print("match(" + arguments + ", " + types + ") -> ");
  139. FuzzyBoolean b = outOfStar(typePatterns, types, 0, 0, patternLength - ellipsisCount, nameLength, ellipsisCount, kind);
  140. // System.err.println(b);
  141. return b;
  142. }
  143. }
  144. // TODO Add TypePatternList.matches(Object[] objs)
  145. public FuzzyBoolean matches(Object[] objs, TypePattern.MatchKind kind) {
  146. int nameLength = objs.length;
  147. int patternLength = typePatterns.length;
  148. int nameIndex = 0;
  149. int patternIndex = 0;
  150. if (ellipsisCount == 0) {
  151. if (nameLength != patternLength) return FuzzyBoolean.NO;
  152. FuzzyBoolean finalReturn = FuzzyBoolean.YES;
  153. while (patternIndex < patternLength) {
  154. FuzzyBoolean ret = typePatterns[patternIndex++].matches(objs[nameIndex++],kind);
  155. if (ret == FuzzyBoolean.NO) return ret;
  156. if (ret == FuzzyBoolean.MAYBE) finalReturn = ret;
  157. }
  158. return finalReturn;
  159. } else if (ellipsisCount == 1) {
  160. if (nameLength < patternLength-1) return FuzzyBoolean.NO;
  161. FuzzyBoolean finalReturn = FuzzyBoolean.YES;
  162. while (patternIndex < patternLength) {
  163. TypePattern p = typePatterns[patternIndex++];
  164. if (p == TypePattern.ELLIPSIS) {
  165. nameIndex = nameLength - (patternLength-patternIndex);
  166. } else {
  167. FuzzyBoolean ret = p.matches(objs[nameIndex++],kind);
  168. if (ret == FuzzyBoolean.NO) return ret;
  169. if (ret == FuzzyBoolean.MAYBE) finalReturn = ret;
  170. }
  171. }
  172. return finalReturn;
  173. } else {
  174. // System.err.print("match(" + arguments + ", " + types + ") -> ");
  175. FuzzyBoolean b = outOfStar(typePatterns, objs, 0, 0, patternLength - ellipsisCount, nameLength, ellipsisCount, kind);
  176. // System.err.println(b);
  177. return b;
  178. }
  179. }
  180. // XXX run-time signature matching, too much duplicated code
  181. public FuzzyBoolean matches(Class[] types, TypePattern.MatchKind kind) {
  182. int nameLength = types.length;
  183. int patternLength = typePatterns.length;
  184. int nameIndex = 0;
  185. int patternIndex = 0;
  186. if (ellipsisCount == 0) {
  187. if (nameLength != patternLength) return FuzzyBoolean.NO;
  188. FuzzyBoolean finalReturn = FuzzyBoolean.YES;
  189. while (patternIndex < patternLength) {
  190. FuzzyBoolean ret = typePatterns[patternIndex++].matches(types[nameIndex++], kind);
  191. if (ret == FuzzyBoolean.NO) return ret;
  192. if (ret == FuzzyBoolean.MAYBE) finalReturn = ret;
  193. }
  194. return finalReturn;
  195. } else if (ellipsisCount == 1) {
  196. if (nameLength < patternLength-1) return FuzzyBoolean.NO;
  197. FuzzyBoolean finalReturn = FuzzyBoolean.YES;
  198. while (patternIndex < patternLength) {
  199. TypePattern p = typePatterns[patternIndex++];
  200. if (p == TypePattern.ELLIPSIS) {
  201. nameIndex = nameLength - (patternLength-patternIndex);
  202. } else {
  203. FuzzyBoolean ret = p.matches(types[nameIndex++], kind);
  204. if (ret == FuzzyBoolean.NO) return ret;
  205. if (ret == FuzzyBoolean.MAYBE) finalReturn = ret;
  206. }
  207. }
  208. return finalReturn;
  209. } else {
  210. // System.err.print("match(" + arguments + ", " + types + ") -> ");
  211. FuzzyBoolean b = outOfStar(typePatterns, types, 0, 0, patternLength - ellipsisCount, nameLength, ellipsisCount, kind);
  212. // System.err.println(b);
  213. return b;
  214. }
  215. }
  216. private static FuzzyBoolean outOfStar(final TypePattern[] pattern, final ResolvedTypeX[] target,
  217. int pi, int ti,
  218. int pLeft, int tLeft,
  219. final int starsLeft, TypePattern.MatchKind kind) {
  220. if (pLeft > tLeft) return FuzzyBoolean.NO;
  221. FuzzyBoolean finalReturn = FuzzyBoolean.YES;
  222. while (true) {
  223. // invariant: if (tLeft > 0) then (ti < target.length && pi < pattern.length)
  224. if (tLeft == 0) return finalReturn;
  225. if (pLeft == 0) {
  226. if (starsLeft > 0) {
  227. return finalReturn;
  228. } else {
  229. return FuzzyBoolean.NO;
  230. }
  231. }
  232. if (pattern[pi] == TypePattern.ELLIPSIS) {
  233. return inStar(pattern, target, pi+1, ti, pLeft, tLeft, starsLeft-1, kind);
  234. }
  235. FuzzyBoolean ret = pattern[pi].matches(target[ti], kind);
  236. if (ret == FuzzyBoolean.NO) return ret;
  237. if (ret == FuzzyBoolean.MAYBE) finalReturn = ret;
  238. pi++; ti++; pLeft--; tLeft--;
  239. }
  240. }
  241. private static FuzzyBoolean inStar(final TypePattern[] pattern, final ResolvedTypeX[] target,
  242. int pi, int ti,
  243. final int pLeft, int tLeft,
  244. int starsLeft, TypePattern.MatchKind kind) {
  245. // invariant: pLeft > 0, so we know we'll run out of stars and find a real char in pattern
  246. TypePattern patternChar = pattern[pi];
  247. while (patternChar == TypePattern.ELLIPSIS) {
  248. starsLeft--;
  249. patternChar = pattern[++pi];
  250. }
  251. while (true) {
  252. // invariant: if (tLeft > 0) then (ti < target.length)
  253. if (pLeft > tLeft) return FuzzyBoolean.NO;
  254. FuzzyBoolean ff = patternChar.matches(target[ti], kind);
  255. if (ff.maybeTrue()) {
  256. FuzzyBoolean xx = outOfStar(pattern, target, pi+1, ti+1, pLeft-1, tLeft-1, starsLeft, kind);
  257. if (xx.maybeTrue()) return ff.and(xx);
  258. }
  259. ti++; tLeft--;
  260. }
  261. }
  262. private static FuzzyBoolean outOfStar(final TypePattern[] pattern,
  263. final Class[] target, int pi, int ti, int pLeft, int tLeft,
  264. final int starsLeft, TypePattern.MatchKind kind) {
  265. if (pLeft > tLeft)
  266. return FuzzyBoolean.NO;
  267. FuzzyBoolean finalReturn = FuzzyBoolean.YES;
  268. while (true) {
  269. // invariant: if (tLeft > 0) then (ti < target.length && pi <
  270. // pattern.length)
  271. if (tLeft == 0)
  272. return finalReturn;
  273. if (pLeft == 0) {
  274. if (starsLeft > 0) {
  275. return finalReturn;
  276. } else {
  277. return FuzzyBoolean.NO;
  278. }
  279. }
  280. if (pattern[pi] == TypePattern.ELLIPSIS) {
  281. return inStar(pattern, target, pi + 1, ti, pLeft, tLeft,
  282. starsLeft - 1, kind);
  283. }
  284. FuzzyBoolean ret = pattern[pi].matches(target[ti], kind);
  285. if (ret == FuzzyBoolean.NO)
  286. return ret;
  287. if (ret == FuzzyBoolean.MAYBE)
  288. finalReturn = ret;
  289. pi++;
  290. ti++;
  291. pLeft--;
  292. tLeft--;
  293. }
  294. }
  295. private static FuzzyBoolean inStar(final TypePattern[] pattern,
  296. final Class[] target, int pi, int ti, final int pLeft,
  297. int tLeft, int starsLeft, TypePattern.MatchKind kind) {
  298. // invariant: pLeft > 0, so we know we'll run out of stars and find a
  299. // real char in pattern
  300. TypePattern patternChar = pattern[pi];
  301. while (patternChar == TypePattern.ELLIPSIS) {
  302. starsLeft--;
  303. patternChar = pattern[++pi];
  304. }
  305. while (true) {
  306. // invariant: if (tLeft > 0) then (ti < target.length)
  307. if (pLeft > tLeft)
  308. return FuzzyBoolean.NO;
  309. FuzzyBoolean ff = patternChar.matches(target[ti], kind);
  310. if (ff.maybeTrue()) {
  311. FuzzyBoolean xx = outOfStar(pattern, target, pi + 1, ti + 1,
  312. pLeft - 1, tLeft - 1, starsLeft, kind);
  313. if (xx.maybeTrue())
  314. return ff.and(xx);
  315. }
  316. ti++;
  317. tLeft--;
  318. }
  319. }
  320. private static FuzzyBoolean outOfStar(final TypePattern[] pattern,
  321. final Object[] target, int pi, int ti, int pLeft, int tLeft,
  322. final int starsLeft, TypePattern.MatchKind kind) {
  323. if (pLeft > tLeft)
  324. return FuzzyBoolean.NO;
  325. FuzzyBoolean finalReturn = FuzzyBoolean.YES;
  326. while (true) {
  327. // invariant: if (tLeft > 0) then (ti < target.length && pi <
  328. // pattern.length)
  329. if (tLeft == 0)
  330. return finalReturn;
  331. if (pLeft == 0) {
  332. if (starsLeft > 0) {
  333. return finalReturn;
  334. } else {
  335. return FuzzyBoolean.NO;
  336. }
  337. }
  338. if (pattern[pi] == TypePattern.ELLIPSIS) {
  339. return inStar(pattern, target, pi + 1, ti, pLeft, tLeft,
  340. starsLeft - 1,kind);
  341. }
  342. FuzzyBoolean ret = pattern[pi].matches(target[ti],kind);
  343. if (ret == FuzzyBoolean.NO)
  344. return ret;
  345. if (ret == FuzzyBoolean.MAYBE)
  346. finalReturn = ret;
  347. pi++;
  348. ti++;
  349. pLeft--;
  350. tLeft--;
  351. }
  352. }
  353. private static FuzzyBoolean inStar(final TypePattern[] pattern,
  354. final Object[] target, int pi, int ti, final int pLeft,
  355. int tLeft, int starsLeft, TypePattern.MatchKind kind) {
  356. // invariant: pLeft > 0, so we know we'll run out of stars and find a
  357. // real char in pattern
  358. TypePattern patternChar = pattern[pi];
  359. while (patternChar == TypePattern.ELLIPSIS) {
  360. starsLeft--;
  361. patternChar = pattern[++pi];
  362. }
  363. while (true) {
  364. // invariant: if (tLeft > 0) then (ti < target.length)
  365. if (pLeft > tLeft)
  366. return FuzzyBoolean.NO;
  367. FuzzyBoolean ff = patternChar.matches(target[ti],kind);
  368. if (ff.maybeTrue()) {
  369. FuzzyBoolean xx = outOfStar(pattern, target, pi + 1, ti + 1,
  370. pLeft - 1, tLeft - 1, starsLeft,kind);
  371. if (xx.maybeTrue())
  372. return ff.and(xx);
  373. }
  374. ti++;
  375. tLeft--;
  376. }
  377. }
  378. public TypePatternList resolveBindings(IScope scope, Bindings bindings, boolean allowBinding, boolean requireExactType) {
  379. for (int i=0; i<typePatterns.length; i++) {
  380. TypePattern p = typePatterns[i];
  381. if (p != null) {
  382. typePatterns[i] = typePatterns[i].resolveBindings(scope, bindings, allowBinding, requireExactType);
  383. }
  384. }
  385. return this;
  386. }
  387. public TypePatternList resolveBindingsFromRTTI(boolean allowBinding, boolean requireExactType) {
  388. for (int i=0; i<typePatterns.length; i++) {
  389. TypePattern p = typePatterns[i];
  390. if (p != null) {
  391. typePatterns[i] = typePatterns[i].resolveBindingsFromRTTI(allowBinding, requireExactType);
  392. }
  393. }
  394. return this;
  395. }
  396. public TypePatternList resolveReferences(IntMap bindings) {
  397. int len = typePatterns.length;
  398. TypePattern[] ret = new TypePattern[len];
  399. for (int i=0; i < len; i++) {
  400. ret[i] = typePatterns[i].remapAdviceFormals(bindings);
  401. }
  402. return new TypePatternList(ret);
  403. }
  404. public void postRead(ResolvedTypeX enclosingType) {
  405. for (int i=0; i<typePatterns.length; i++) {
  406. TypePattern p = typePatterns[i];
  407. p.postRead(enclosingType);
  408. }
  409. }
  410. public boolean equals(Object other) {
  411. if (!(other instanceof TypePatternList)) return false;
  412. TypePatternList o = (TypePatternList)other;
  413. int len = o.typePatterns.length;
  414. if (len != this.typePatterns.length) return false;
  415. for (int i=0; i<len; i++) {
  416. if (!this.typePatterns[i].equals(o.typePatterns[i])) return false;
  417. }
  418. return true;
  419. }
  420. public int hashCode() {
  421. int result = 41;
  422. for (int i = 0, len = typePatterns.length; i < len; i++) {
  423. result = 37*result + typePatterns[i].hashCode();
  424. }
  425. return result;
  426. }
  427. public static TypePatternList read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  428. short len = s.readShort();
  429. TypePattern[] arguments = new TypePattern[len];
  430. for (int i=0; i<len; i++) {
  431. arguments[i] = TypePattern.read(s, context);
  432. }
  433. TypePatternList ret = new TypePatternList(arguments);
  434. ret.readLocation(context, s);
  435. return ret;
  436. }
  437. public void write(DataOutputStream s) throws IOException {
  438. s.writeShort(typePatterns.length);
  439. for (int i=0; i<typePatterns.length; i++) {
  440. typePatterns[i].write(s);
  441. }
  442. writeLocation(s);
  443. }
  444. public TypePattern[] getTypePatterns() {
  445. return typePatterns;
  446. }
  447. public Collection getExactTypes() {
  448. ArrayList ret = new ArrayList();
  449. for (int i=0; i<typePatterns.length; i++) {
  450. TypeX t = typePatterns[i].getExactType();
  451. if (t != ResolvedTypeX.MISSING) ret.add(t);
  452. }
  453. return ret;
  454. }
  455. }