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.

PatternParser.java 59KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  1. /* *******************************************************************
  2. * Copyright (c) 2002,2010
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * Adrian Colyer, IBM
  12. * Andy Clement, IBM, SpringSource
  13. * ******************************************************************/
  14. package org.aspectj.weaver.patterns;
  15. import java.util.ArrayList;
  16. import java.util.Collections;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import org.aspectj.weaver.ISourceContext;
  22. import org.aspectj.weaver.Member;
  23. import org.aspectj.weaver.MemberKind;
  24. import org.aspectj.weaver.Shadow;
  25. import org.aspectj.weaver.UnresolvedType;
  26. import org.aspectj.weaver.World;
  27. import org.aspectj.weaver.internal.tools.PointcutDesignatorHandlerBasedPointcut;
  28. import org.aspectj.weaver.tools.ContextBasedMatcher;
  29. import org.aspectj.weaver.tools.PointcutDesignatorHandler;
  30. /**
  31. * @author PARC
  32. * @author Adrian Colyer
  33. * @author Andy Clement
  34. */
  35. // XXX doesn't handle errors for extra tokens very well (sometimes ignores)
  36. public class PatternParser {
  37. private ITokenSource tokenSource;
  38. private ISourceContext sourceContext;
  39. /** not thread-safe, but this class is not intended to be... */
  40. private boolean allowHasTypePatterns = false;
  41. /** extension handlers used in weaver tools API only */
  42. private Set<PointcutDesignatorHandler> pointcutDesignatorHandlers = Collections.emptySet();
  43. private World world;
  44. /**
  45. * Constructor for PatternParser.
  46. */
  47. public PatternParser(ITokenSource tokenSource) {
  48. super();
  49. this.tokenSource = tokenSource;
  50. this.sourceContext = tokenSource.getSourceContext();
  51. }
  52. /** only used by weaver tools API */
  53. public void setPointcutDesignatorHandlers(Set<PointcutDesignatorHandler> handlers, World world) {
  54. this.pointcutDesignatorHandlers = handlers;
  55. this.world = world;
  56. }
  57. public PerClause maybeParsePerClause() {
  58. IToken tok = tokenSource.peek();
  59. if (tok == IToken.EOF) {
  60. return null;
  61. }
  62. if (tok.isIdentifier()) {
  63. String name = tok.getString();
  64. if (name.equals("issingleton")) {
  65. return parsePerSingleton();
  66. } else if (name.equals("perthis")) {
  67. return parsePerObject(true);
  68. } else if (name.equals("pertarget")) {
  69. return parsePerObject(false);
  70. } else if (name.equals("percflow")) {
  71. return parsePerCflow(false);
  72. } else if (name.equals("percflowbelow")) {
  73. return parsePerCflow(true);
  74. } else if (name.equals("pertypewithin")) { // PTWIMPL Parse the pertypewithin clause
  75. return parsePerTypeWithin();
  76. } else {
  77. return null;
  78. }
  79. }
  80. return null;
  81. }
  82. private PerClause parsePerCflow(boolean isBelow) {
  83. parseIdentifier();
  84. eat("(");
  85. Pointcut entry = parsePointcut();
  86. eat(")");
  87. return new PerCflow(entry, isBelow);
  88. }
  89. private PerClause parsePerObject(boolean isThis) {
  90. parseIdentifier();
  91. eat("(");
  92. Pointcut entry = parsePointcut();
  93. eat(")");
  94. return new PerObject(entry, isThis);
  95. }
  96. private PerClause parsePerTypeWithin() {
  97. parseIdentifier();
  98. eat("(");
  99. TypePattern withinTypePattern = parseTypePattern();
  100. eat(")");
  101. return new PerTypeWithin(withinTypePattern);
  102. }
  103. private PerClause parsePerSingleton() {
  104. parseIdentifier();
  105. eat("(");
  106. eat(")");
  107. return new PerSingleton();
  108. }
  109. public Declare parseDeclare() {
  110. int startPos = tokenSource.peek().getStart();
  111. eatIdentifier("declare");
  112. String kind = parseIdentifier();
  113. Declare ret;
  114. if (kind.equals("error")) {
  115. eat(":");
  116. ret = parseErrorOrWarning(true);
  117. } else if (kind.equals("warning")) {
  118. eat(":");
  119. ret = parseErrorOrWarning(false);
  120. } else if (kind.equals("precedence")) {
  121. eat(":");
  122. ret = parseDominates();
  123. } else if (kind.equals("dominates")) {
  124. throw new ParserException("name changed to declare precedence", tokenSource.peek(-2));
  125. } else if (kind.equals("parents")) {
  126. ret = parseParents();
  127. } else if (kind.equals("soft")) {
  128. eat(":");
  129. ret = parseSoft();
  130. } else {
  131. throw new ParserException(
  132. "expected one of error, warning, parents, soft, precedence, @type, @method, @constructor, @field",
  133. tokenSource.peek(-1));
  134. }
  135. int endPos = tokenSource.peek(-1).getEnd();
  136. ret.setLocation(sourceContext, startPos, endPos);
  137. return ret;
  138. }
  139. public Declare parseDeclareAnnotation() {
  140. int startPos = tokenSource.peek().getStart();
  141. eatIdentifier("declare");
  142. eat("@");
  143. String kind = parseIdentifier();
  144. eat(":");
  145. Declare ret;
  146. if (kind.equals("type")) {
  147. ret = parseDeclareAtType();
  148. } else if (kind.equals("method")) {
  149. ret = parseDeclareAtMethod(true);
  150. } else if (kind.equals("field")) {
  151. ret = parseDeclareAtField();
  152. } else if (kind.equals("constructor")) {
  153. ret = parseDeclareAtMethod(false);
  154. } else {
  155. throw new ParserException("one of type, method, field, constructor", tokenSource.peek(-1));
  156. }
  157. eat(";");
  158. int endPos = tokenSource.peek(-1).getEnd();
  159. ret.setLocation(sourceContext, startPos, endPos);
  160. return ret;
  161. }
  162. public DeclareAnnotation parseDeclareAtType() {
  163. allowHasTypePatterns = true;
  164. TypePattern p = parseTypePattern();
  165. allowHasTypePatterns = false;
  166. return new DeclareAnnotation(DeclareAnnotation.AT_TYPE, p);
  167. }
  168. public DeclareAnnotation parseDeclareAtMethod(boolean isMethod) {
  169. ISignaturePattern sp = parseCompoundMethodOrConstructorSignaturePattern(isMethod);// parseMethodOrConstructorSignaturePattern();
  170. if (!isMethod) {
  171. return new DeclareAnnotation(DeclareAnnotation.AT_CONSTRUCTOR, sp);
  172. } else {
  173. return new DeclareAnnotation(DeclareAnnotation.AT_METHOD, sp);
  174. }
  175. }
  176. public DeclareAnnotation parseDeclareAtField() {
  177. ISignaturePattern compoundFieldSignaturePattern = parseCompoundFieldSignaturePattern();
  178. DeclareAnnotation da = new DeclareAnnotation(DeclareAnnotation.AT_FIELD, compoundFieldSignaturePattern);
  179. return da;
  180. }
  181. public ISignaturePattern parseCompoundFieldSignaturePattern() {
  182. int index = tokenSource.getIndex();
  183. try {
  184. ISignaturePattern atomicFieldSignaturePattern = parseMaybeParenthesizedFieldSignaturePattern();
  185. while (isEitherAndOrOr()) {
  186. if (maybeEat("&&")) {
  187. atomicFieldSignaturePattern = new AndSignaturePattern(atomicFieldSignaturePattern,
  188. parseMaybeParenthesizedFieldSignaturePattern());
  189. }
  190. if (maybeEat("||")) {
  191. atomicFieldSignaturePattern = new OrSignaturePattern(atomicFieldSignaturePattern,
  192. parseMaybeParenthesizedFieldSignaturePattern());
  193. }
  194. }
  195. return atomicFieldSignaturePattern;
  196. } catch (ParserException e) {
  197. // fallback in the case of a regular single field signature pattern that just happened to start with '('
  198. int nowAt = tokenSource.getIndex();
  199. tokenSource.setIndex(index);
  200. try {
  201. ISignaturePattern fsp = parseFieldSignaturePattern();
  202. return fsp;
  203. } catch (Exception e2) {
  204. tokenSource.setIndex(nowAt);
  205. // throw the original
  206. throw e;
  207. }
  208. }
  209. }
  210. private boolean isEitherAndOrOr() {
  211. String tokenstring = tokenSource.peek().getString();
  212. return tokenstring.equals("&&") || tokenstring.equals("||");
  213. }
  214. public ISignaturePattern parseCompoundMethodOrConstructorSignaturePattern(boolean isMethod) {
  215. ISignaturePattern atomicMethodCtorSignaturePattern = parseMaybeParenthesizedMethodOrConstructorSignaturePattern(isMethod);
  216. while (isEitherAndOrOr()) {
  217. if (maybeEat("&&")) {
  218. atomicMethodCtorSignaturePattern = new AndSignaturePattern(atomicMethodCtorSignaturePattern,
  219. parseMaybeParenthesizedMethodOrConstructorSignaturePattern(isMethod));
  220. }
  221. if (maybeEat("||")) {
  222. atomicMethodCtorSignaturePattern = new OrSignaturePattern(atomicMethodCtorSignaturePattern,
  223. parseMaybeParenthesizedMethodOrConstructorSignaturePattern(isMethod));
  224. }
  225. }
  226. return atomicMethodCtorSignaturePattern;
  227. }
  228. public DeclarePrecedence parseDominates() {
  229. List<TypePattern> l = new ArrayList<TypePattern>();
  230. do {
  231. l.add(parseTypePattern());
  232. } while (maybeEat(","));
  233. return new DeclarePrecedence(l);
  234. }
  235. private Declare parseParents() {
  236. /*
  237. * simplified design requires use of raw types for declare parents, no generic spec. allowed String[] typeParameters =
  238. * maybeParseSimpleTypeVariableList();
  239. */
  240. eat(":");
  241. allowHasTypePatterns = true;
  242. TypePattern p = parseTypePattern(false, false);
  243. allowHasTypePatterns = false;
  244. IToken t = tokenSource.next();
  245. if (!(t.getString().equals("extends") || t.getString().equals("implements"))) {
  246. throw new ParserException("extends or implements", t);
  247. }
  248. boolean isExtends = t.getString().equals("extends");
  249. List<TypePattern> l = new ArrayList<TypePattern>();
  250. do {
  251. l.add(parseTypePattern());
  252. } while (maybeEat(","));
  253. // XXX somewhere in the chain we need to enforce that we have only ExactTypePatterns
  254. DeclareParents decp = new DeclareParents(p, l, isExtends);
  255. return decp;
  256. }
  257. private Declare parseSoft() {
  258. TypePattern p = parseTypePattern();
  259. eat(":");
  260. Pointcut pointcut = parsePointcut();
  261. return new DeclareSoft(p, pointcut);
  262. }
  263. /**
  264. * Attempt to parse a pointcut, if that fails then try again for a type pattern.
  265. *
  266. * @param isError true if it is declare error rather than declare warning
  267. * @return the new declare
  268. */
  269. private Declare parseErrorOrWarning(boolean isError) {
  270. Pointcut pointcut = null;
  271. int index = tokenSource.getIndex();
  272. try {
  273. pointcut = parsePointcut();
  274. } catch (ParserException pe) {
  275. try {
  276. tokenSource.setIndex(index);
  277. boolean oldValue = allowHasTypePatterns;
  278. TypePattern typePattern = null;
  279. try {
  280. allowHasTypePatterns = true;
  281. typePattern = parseTypePattern();
  282. } finally {
  283. allowHasTypePatterns = oldValue;
  284. }
  285. eat(":");
  286. String message = parsePossibleStringSequence(true);
  287. return new DeclareTypeErrorOrWarning(isError, typePattern, message);
  288. } catch (ParserException pe2) {
  289. // deliberately throw the original problem
  290. throw pe;
  291. }
  292. }
  293. eat(":");
  294. String message = parsePossibleStringSequence(true);
  295. return new DeclareErrorOrWarning(isError, pointcut, message);
  296. }
  297. public Pointcut parsePointcut() {
  298. Pointcut p = parseAtomicPointcut();
  299. if (maybeEat("&&")) {
  300. p = new AndPointcut(p, parseNotOrPointcut());
  301. }
  302. if (maybeEat("||")) {
  303. p = new OrPointcut(p, parsePointcut());
  304. }
  305. return p;
  306. }
  307. private Pointcut parseNotOrPointcut() {
  308. Pointcut p = parseAtomicPointcut();
  309. if (maybeEat("&&")) {
  310. p = new AndPointcut(p, parseNotOrPointcut());
  311. }
  312. return p;
  313. }
  314. private Pointcut parseAtomicPointcut() {
  315. if (maybeEat("!")) {
  316. int startPos = tokenSource.peek(-1).getStart();
  317. Pointcut p = new NotPointcut(parseAtomicPointcut(), startPos);
  318. return p;
  319. }
  320. if (maybeEat("(")) {
  321. Pointcut p = parsePointcut();
  322. eat(")");
  323. return p;
  324. }
  325. if (maybeEat("@")) {
  326. int startPos = tokenSource.peek().getStart();
  327. Pointcut p = parseAnnotationPointcut();
  328. int endPos = tokenSource.peek(-1).getEnd();
  329. p.setLocation(sourceContext, startPos, endPos);
  330. return p;
  331. }
  332. int startPos = tokenSource.peek().getStart();
  333. Pointcut p = parseSinglePointcut();
  334. int endPos = tokenSource.peek(-1).getEnd();
  335. p.setLocation(sourceContext, startPos, endPos);
  336. return p;
  337. }
  338. public Pointcut parseSinglePointcut() {
  339. int start = tokenSource.getIndex();
  340. IToken t = tokenSource.peek();
  341. Pointcut p = t.maybeGetParsedPointcut();
  342. if (p != null) {
  343. tokenSource.next();
  344. return p;
  345. }
  346. String kind = parseIdentifier();
  347. // IToken possibleTypeVariableToken = tokenSource.peek();
  348. // String[] typeVariables = maybeParseSimpleTypeVariableList();
  349. if (kind.equals("execution") || kind.equals("call") || kind.equals("get") || kind.equals("set")) {
  350. p = parseKindedPointcut(kind);
  351. } else if (kind.equals("args")) {
  352. p = parseArgsPointcut();
  353. } else if (kind.equals("this")) {
  354. p = parseThisOrTargetPointcut(kind);
  355. } else if (kind.equals("target")) {
  356. p = parseThisOrTargetPointcut(kind);
  357. } else if (kind.equals("within")) {
  358. p = parseWithinPointcut();
  359. } else if (kind.equals("withincode")) {
  360. p = parseWithinCodePointcut();
  361. } else if (kind.equals("cflow")) {
  362. p = parseCflowPointcut(false);
  363. } else if (kind.equals("cflowbelow")) {
  364. p = parseCflowPointcut(true);
  365. } else if (kind.equals("adviceexecution")) {
  366. eat("(");
  367. eat(")");
  368. p = new KindedPointcut(Shadow.AdviceExecution, new SignaturePattern(Member.ADVICE, ModifiersPattern.ANY,
  369. TypePattern.ANY, TypePattern.ANY, NamePattern.ANY, TypePatternList.ANY, ThrowsPattern.ANY,
  370. AnnotationTypePattern.ANY));
  371. } else if (kind.equals("handler")) {
  372. eat("(");
  373. TypePattern typePat = parseTypePattern(false, false);
  374. eat(")");
  375. p = new HandlerPointcut(typePat);
  376. } else if (kind.equals("lock") || kind.equals("unlock")) {
  377. p = parseMonitorPointcut(kind);
  378. } else if (kind.equals("initialization")) {
  379. eat("(");
  380. SignaturePattern sig = parseConstructorSignaturePattern();
  381. eat(")");
  382. p = new KindedPointcut(Shadow.Initialization, sig);
  383. } else if (kind.equals("staticinitialization")) {
  384. eat("(");
  385. TypePattern typePat = parseTypePattern(false, false);
  386. eat(")");
  387. p = new KindedPointcut(Shadow.StaticInitialization, new SignaturePattern(Member.STATIC_INITIALIZATION,
  388. ModifiersPattern.ANY, TypePattern.ANY, typePat, NamePattern.ANY, TypePatternList.EMPTY, ThrowsPattern.ANY,
  389. AnnotationTypePattern.ANY));
  390. } else if (kind.equals("preinitialization")) {
  391. eat("(");
  392. SignaturePattern sig = parseConstructorSignaturePattern();
  393. eat(")");
  394. p = new KindedPointcut(Shadow.PreInitialization, sig);
  395. } else if (kind.equals("if")) {
  396. // - annotation style only allows if(), if(true) or if(false)
  397. // - if() means the body of the annotated method represents the if expression
  398. // - anything else is an error because code cannot be put into the if()
  399. // - code style will already have been processed and the call to maybeGetParsedPointcut()
  400. // at the top of this method will have succeeded.
  401. eat("(");
  402. if (maybeEatIdentifier("true")) {
  403. eat(")");
  404. p = new IfPointcut.IfTruePointcut();
  405. } else if (maybeEatIdentifier("false")) {
  406. eat(")");
  407. p = new IfPointcut.IfFalsePointcut();
  408. } else {
  409. if (!maybeEat(")")) {
  410. throw new ParserException(
  411. "in annotation style, if(...) pointcuts cannot contain code. Use if() and put the code in the annotated method",
  412. t);
  413. }
  414. // TODO - Alex has some token stuff going on here to get a readable name in place of ""...
  415. p = new IfPointcut("");
  416. }
  417. } else {
  418. boolean matchedByExtensionDesignator = false;
  419. // see if a registered handler wants to parse it, otherwise
  420. // treat as a reference pointcut
  421. for (PointcutDesignatorHandler pcd : pointcutDesignatorHandlers) {
  422. if (pcd.getDesignatorName().equals(kind)) {
  423. p = parseDesignatorPointcut(pcd);
  424. matchedByExtensionDesignator = true;
  425. }
  426. }
  427. if (!matchedByExtensionDesignator) {
  428. tokenSource.setIndex(start);
  429. p = parseReferencePointcut();
  430. }
  431. }
  432. return p;
  433. }
  434. private void assertNoTypeVariables(String[] tvs, String errorMessage, IToken token) {
  435. if (tvs != null) {
  436. throw new ParserException(errorMessage, token);
  437. }
  438. }
  439. public Pointcut parseAnnotationPointcut() {
  440. int start = tokenSource.getIndex();
  441. IToken t = tokenSource.peek();
  442. String kind = parseIdentifier();
  443. IToken possibleTypeVariableToken = tokenSource.peek();
  444. String[] typeVariables = maybeParseSimpleTypeVariableList();
  445. if (typeVariables != null) {
  446. String message = "(";
  447. assertNoTypeVariables(typeVariables, message, possibleTypeVariableToken);
  448. }
  449. tokenSource.setIndex(start);
  450. if (kind.equals("annotation")) {
  451. return parseAtAnnotationPointcut();
  452. } else if (kind.equals("args")) {
  453. return parseArgsAnnotationPointcut();
  454. } else if (kind.equals("this") || kind.equals("target")) {
  455. return parseThisOrTargetAnnotationPointcut();
  456. } else if (kind.equals("within")) {
  457. return parseWithinAnnotationPointcut();
  458. } else if (kind.equals("withincode")) {
  459. return parseWithinCodeAnnotationPointcut();
  460. }
  461. throw new ParserException("pointcut name", t);
  462. }
  463. private Pointcut parseAtAnnotationPointcut() {
  464. parseIdentifier();
  465. eat("(");
  466. if (maybeEat(")")) {
  467. throw new ParserException("@AnnotationName or parameter", tokenSource.peek());
  468. }
  469. ExactAnnotationTypePattern type = parseAnnotationNameOrVarTypePattern();
  470. eat(")");
  471. return new AnnotationPointcut(type);
  472. }
  473. private SignaturePattern parseConstructorSignaturePattern() {
  474. SignaturePattern ret = parseMethodOrConstructorSignaturePattern();
  475. if (ret.getKind() == Member.CONSTRUCTOR) {
  476. return ret;
  477. }
  478. throw new ParserException("constructor pattern required, found method pattern", ret);
  479. }
  480. private Pointcut parseWithinCodePointcut() {
  481. // parseIdentifier();
  482. eat("(");
  483. SignaturePattern sig = parseMethodOrConstructorSignaturePattern();
  484. eat(")");
  485. return new WithincodePointcut(sig);
  486. }
  487. private Pointcut parseCflowPointcut(boolean isBelow) {
  488. // parseIdentifier();
  489. eat("(");
  490. Pointcut entry = parsePointcut();
  491. eat(")");
  492. return new CflowPointcut(entry, isBelow, null);
  493. }
  494. /**
  495. * Method parseWithinPointcut.
  496. *
  497. * @return Pointcut
  498. */
  499. private Pointcut parseWithinPointcut() {
  500. // parseIdentifier();
  501. eat("(");
  502. TypePattern type = parseTypePattern();
  503. eat(")");
  504. return new WithinPointcut(type);
  505. }
  506. /**
  507. * Method parseThisOrTargetPointcut.
  508. *
  509. * @return Pointcut
  510. */
  511. private Pointcut parseThisOrTargetPointcut(String kind) {
  512. eat("(");
  513. TypePattern type = parseTypePattern();
  514. eat(")");
  515. return new ThisOrTargetPointcut(kind.equals("this"), type);
  516. }
  517. private Pointcut parseThisOrTargetAnnotationPointcut() {
  518. String kind = parseIdentifier();
  519. eat("(");
  520. if (maybeEat(")")) {
  521. throw new ParserException("expecting @AnnotationName or parameter, but found ')'", tokenSource.peek());
  522. }
  523. ExactAnnotationTypePattern type = parseAnnotationNameOrVarTypePattern();
  524. eat(")");
  525. return new ThisOrTargetAnnotationPointcut(kind.equals("this"), type);
  526. }
  527. private Pointcut parseWithinAnnotationPointcut() {
  528. /* String kind = */parseIdentifier();
  529. eat("(");
  530. if (maybeEat(")")) {
  531. throw new ParserException("expecting @AnnotationName or parameter, but found ')'", tokenSource.peek());
  532. }
  533. AnnotationTypePattern type = parseAnnotationNameOrVarTypePattern();
  534. eat(")");
  535. return new WithinAnnotationPointcut(type);
  536. }
  537. private Pointcut parseWithinCodeAnnotationPointcut() {
  538. /* String kind = */parseIdentifier();
  539. eat("(");
  540. if (maybeEat(")")) {
  541. throw new ParserException("expecting @AnnotationName or parameter, but found ')'", tokenSource.peek());
  542. }
  543. ExactAnnotationTypePattern type = parseAnnotationNameOrVarTypePattern();
  544. eat(")");
  545. return new WithinCodeAnnotationPointcut(type);
  546. }
  547. /**
  548. * Method parseArgsPointcut.
  549. *
  550. * @return Pointcut
  551. */
  552. private Pointcut parseArgsPointcut() {
  553. // parseIdentifier();
  554. TypePatternList arguments = parseArgumentsPattern(false);
  555. return new ArgsPointcut(arguments);
  556. }
  557. private Pointcut parseArgsAnnotationPointcut() {
  558. parseIdentifier();
  559. AnnotationPatternList arguments = parseArgumentsAnnotationPattern();
  560. return new ArgsAnnotationPointcut(arguments);
  561. }
  562. private Pointcut parseReferencePointcut() {
  563. TypePattern onType = parseTypePattern();
  564. NamePattern name = null;
  565. if (onType.typeParameters.size() > 0) {
  566. eat(".");
  567. name = parseNamePattern();
  568. } else {
  569. name = tryToExtractName(onType);
  570. }
  571. if (name == null) {
  572. throw new ParserException("name pattern", tokenSource.peek());
  573. }
  574. if (onType.toString().equals("")) {
  575. onType = null;
  576. }
  577. String simpleName = name.maybeGetSimpleName();
  578. if (simpleName == null) {
  579. throw new ParserException("(", tokenSource.peek(-1));
  580. }
  581. TypePatternList arguments = parseArgumentsPattern(false);
  582. return new ReferencePointcut(onType, simpleName, arguments);
  583. }
  584. private Pointcut parseDesignatorPointcut(PointcutDesignatorHandler pcdHandler) {
  585. eat("(");
  586. int parenCount = 1;
  587. StringBuffer pointcutBody = new StringBuffer();
  588. while (parenCount > 0) {
  589. if (maybeEat("(")) {
  590. parenCount++;
  591. pointcutBody.append("(");
  592. } else if (maybeEat(")")) {
  593. parenCount--;
  594. if (parenCount > 0) {
  595. pointcutBody.append(")");
  596. }
  597. } else {
  598. pointcutBody.append(nextToken().getString());
  599. }
  600. }
  601. ContextBasedMatcher pcExpr = pcdHandler.parse(pointcutBody.toString());
  602. return new PointcutDesignatorHandlerBasedPointcut(pcExpr, world);
  603. }
  604. public List<String> parseDottedIdentifier() {
  605. List<String> ret = new ArrayList<String>();
  606. ret.add(parseIdentifier());
  607. while (maybeEat(".")) {
  608. ret.add(parseIdentifier());
  609. }
  610. return ret;
  611. }
  612. private KindedPointcut parseKindedPointcut(String kind) {
  613. eat("(");
  614. SignaturePattern sig;
  615. Shadow.Kind shadowKind = null;
  616. if (kind.equals("execution")) {
  617. sig = parseMethodOrConstructorSignaturePattern();
  618. if (sig.getKind() == Member.METHOD) {
  619. shadowKind = Shadow.MethodExecution;
  620. } else if (sig.getKind() == Member.CONSTRUCTOR) {
  621. shadowKind = Shadow.ConstructorExecution;
  622. }
  623. } else if (kind.equals("call")) {
  624. sig = parseMethodOrConstructorSignaturePattern();
  625. if (sig.getKind() == Member.METHOD) {
  626. shadowKind = Shadow.MethodCall;
  627. } else if (sig.getKind() == Member.CONSTRUCTOR) {
  628. shadowKind = Shadow.ConstructorCall;
  629. }
  630. } else if (kind.equals("get")) {
  631. sig = parseFieldSignaturePattern();
  632. shadowKind = Shadow.FieldGet;
  633. } else if (kind.equals("set")) {
  634. sig = parseFieldSignaturePattern();
  635. shadowKind = Shadow.FieldSet;
  636. } else {
  637. throw new ParserException("bad kind: " + kind, tokenSource.peek());
  638. }
  639. eat(")");
  640. return new KindedPointcut(shadowKind, sig);
  641. }
  642. /** Covers the 'lock()' and 'unlock()' pointcuts */
  643. private KindedPointcut parseMonitorPointcut(String kind) {
  644. eat("(");
  645. // TypePattern type = TypePattern.ANY;
  646. eat(")");
  647. if (kind.equals("lock")) {
  648. return new KindedPointcut(Shadow.SynchronizationLock, new SignaturePattern(Member.MONITORENTER, ModifiersPattern.ANY,
  649. TypePattern.ANY, TypePattern.ANY,
  650. // type,
  651. NamePattern.ANY, TypePatternList.ANY, ThrowsPattern.ANY, AnnotationTypePattern.ANY));
  652. } else {
  653. return new KindedPointcut(Shadow.SynchronizationUnlock, new SignaturePattern(Member.MONITORENTER, ModifiersPattern.ANY,
  654. TypePattern.ANY, TypePattern.ANY,
  655. // type,
  656. NamePattern.ANY, TypePatternList.ANY, ThrowsPattern.ANY, AnnotationTypePattern.ANY));
  657. }
  658. }
  659. public TypePattern parseTypePattern() {
  660. return parseTypePattern(false, false);
  661. }
  662. public TypePattern parseTypePattern(boolean insideTypeParameters, boolean parameterAnnotationsPossible) {
  663. TypePattern p = parseAtomicTypePattern(insideTypeParameters, parameterAnnotationsPossible);
  664. if (maybeEat("&&")) {
  665. p = new AndTypePattern(p, parseNotOrTypePattern(insideTypeParameters, parameterAnnotationsPossible));
  666. }
  667. if (maybeEat("||")) {
  668. p = new OrTypePattern(p, parseTypePattern(insideTypeParameters, parameterAnnotationsPossible));
  669. }
  670. return p;
  671. }
  672. private TypePattern parseNotOrTypePattern(boolean insideTypeParameters, boolean parameterAnnotationsPossible) {
  673. TypePattern p = parseAtomicTypePattern(insideTypeParameters, parameterAnnotationsPossible);
  674. if (maybeEat("&&")) {
  675. p = new AndTypePattern(p, parseTypePattern(insideTypeParameters, parameterAnnotationsPossible));
  676. }
  677. return p;
  678. }
  679. // Need to differentiate in here between two kinds of annotation pattern - depending on where the ( is
  680. private TypePattern parseAtomicTypePattern(boolean insideTypeParameters, boolean parameterAnnotationsPossible) {
  681. AnnotationTypePattern ap = maybeParseAnnotationPattern(); // might be parameter annotation pattern or type annotation
  682. // pattern
  683. if (maybeEat("!")) {
  684. // int startPos = tokenSource.peek(-1).getStart();
  685. // ??? we lose source location for true start of !type
  686. // An annotation, if processed, is outside of the Not - so here we have to build
  687. // an And pattern containing the annotation and the not as left and right children
  688. // *unless* the annotation pattern was just 'Any' then we can skip building the
  689. // And and just return the Not directly (pr228980)
  690. TypePattern p = null;
  691. TypePattern tp = parseAtomicTypePattern(insideTypeParameters, parameterAnnotationsPossible);
  692. if (!(ap instanceof AnyAnnotationTypePattern)) {
  693. p = new NotTypePattern(tp);
  694. p = new AndTypePattern(setAnnotationPatternForTypePattern(TypePattern.ANY, ap, false), p);
  695. } else {
  696. p = new NotTypePattern(tp);
  697. }
  698. return p;
  699. }
  700. if (maybeEat("(")) {
  701. int openParenPos = tokenSource.peek(-1).getStart();
  702. TypePattern p = parseTypePattern(insideTypeParameters, false);
  703. if ((p instanceof NotTypePattern) && !(ap instanceof AnyAnnotationTypePattern)) {
  704. // dont set the annotation on it, we don't want the annotation to be
  705. // considered as part of the not, it is outside the not (pr228980)
  706. TypePattern tp = setAnnotationPatternForTypePattern(TypePattern.ANY, ap, parameterAnnotationsPossible);
  707. p = new AndTypePattern(tp, p);
  708. } else {
  709. p = setAnnotationPatternForTypePattern(p, ap, parameterAnnotationsPossible);
  710. }
  711. eat(")");
  712. int closeParenPos = tokenSource.peek(-1).getStart();
  713. boolean isVarArgs = maybeEat("...");
  714. if (isVarArgs) {
  715. p.setIsVarArgs(isVarArgs);
  716. }
  717. boolean isIncludeSubtypes = maybeEat("+");
  718. if (isIncludeSubtypes) {
  719. p.includeSubtypes = true; // need the test because (A+) should not set subtypes to false!
  720. }
  721. p.start = openParenPos;
  722. p.end = closeParenPos;
  723. return p;
  724. }
  725. int startPos = tokenSource.peek().getStart();
  726. if (ap.start != -1) {
  727. startPos = ap.start;
  728. }
  729. TypePattern p = parseSingleTypePattern(insideTypeParameters);
  730. int endPos = tokenSource.peek(-1).getEnd();
  731. p = setAnnotationPatternForTypePattern(p, ap, false);
  732. p.setLocation(sourceContext, startPos, endPos);
  733. return p;
  734. }
  735. private TypePattern setAnnotationPatternForTypePattern(TypePattern t, AnnotationTypePattern ap,
  736. boolean parameterAnnotationsPattern) {
  737. TypePattern ret = t;
  738. if (parameterAnnotationsPattern) {
  739. ap.setForParameterAnnotationMatch();
  740. }
  741. if (ap != AnnotationTypePattern.ANY) {
  742. if (t == TypePattern.ANY) {
  743. if (t.annotationPattern == AnnotationTypePattern.ANY) {
  744. return new AnyWithAnnotationTypePattern(ap);
  745. } else {
  746. return new AnyWithAnnotationTypePattern(new AndAnnotationTypePattern(ap, t.annotationPattern));
  747. }
  748. // ret = new WildTypePattern(new NamePattern[] { NamePattern.ANY }, false, 0, false, null);
  749. }
  750. if (t.annotationPattern == AnnotationTypePattern.ANY) {
  751. ret.setAnnotationTypePattern(ap);
  752. } else {
  753. ret.setAnnotationTypePattern(new AndAnnotationTypePattern(ap, t.annotationPattern)); // ???
  754. }
  755. }
  756. return ret;
  757. }
  758. public AnnotationTypePattern maybeParseAnnotationPattern() {
  759. AnnotationTypePattern ret = AnnotationTypePattern.ANY;
  760. AnnotationTypePattern nextPattern = null;
  761. while ((nextPattern = maybeParseSingleAnnotationPattern()) != null) {
  762. if (ret == AnnotationTypePattern.ANY) {
  763. ret = nextPattern;
  764. } else {
  765. ret = new AndAnnotationTypePattern(ret, nextPattern);
  766. }
  767. }
  768. return ret;
  769. }
  770. // PVAL cope with annotation values at other places in this code
  771. public AnnotationTypePattern maybeParseSingleAnnotationPattern() {
  772. AnnotationTypePattern ret = null;
  773. Map<String, String> values = null;
  774. // LALR(2) - fix by making "!@" a single token
  775. int startIndex = tokenSource.getIndex();
  776. if (maybeEat("!")) {
  777. if (maybeEat("@")) {
  778. if (maybeEat("(")) {
  779. TypePattern p = parseTypePattern();
  780. ret = new NotAnnotationTypePattern(new WildAnnotationTypePattern(p));
  781. eat(")");
  782. return ret;
  783. } else {
  784. TypePattern p = parseSingleTypePattern();
  785. if (maybeEatAdjacent("(")) {
  786. values = parseAnnotationValues();
  787. eat(")");
  788. ret = new NotAnnotationTypePattern(new WildAnnotationTypePattern(p, values));
  789. } else {
  790. ret = new NotAnnotationTypePattern(new WildAnnotationTypePattern(p));
  791. }
  792. return ret;
  793. }
  794. } else {
  795. tokenSource.setIndex(startIndex); // not for us!
  796. return ret;
  797. }
  798. }
  799. if (maybeEat("@")) {
  800. if (maybeEat("(")) {
  801. TypePattern p = parseTypePattern();
  802. ret = new WildAnnotationTypePattern(p);
  803. eat(")");
  804. return ret;
  805. } else {
  806. int atPos = tokenSource.peek(-1).getStart();
  807. TypePattern p = parseSingleTypePattern();
  808. if (maybeEatAdjacent("(")) {
  809. values = parseAnnotationValues();
  810. eat(")");
  811. ret = new WildAnnotationTypePattern(p, values);
  812. } else {
  813. ret = new WildAnnotationTypePattern(p);
  814. }
  815. ret.start = atPos;
  816. return ret;
  817. }
  818. } else {
  819. tokenSource.setIndex(startIndex); // not for us!
  820. return ret;
  821. }
  822. }
  823. // Parse annotation values. In an expression in @A(a=b,c=d) this method will be
  824. // parsing the a=b,c=d.)
  825. public Map<String, String> parseAnnotationValues() {
  826. Map<String, String> values = new HashMap<String, String>();
  827. boolean seenDefaultValue = false;
  828. do {
  829. String possibleKeyString = parseAnnotationNameValuePattern();
  830. if (possibleKeyString == null) {
  831. throw new ParserException("expecting simple literal ", tokenSource.peek(-1));
  832. }
  833. // did they specify just a single entry 'v' or a keyvalue pair 'k=v'
  834. if (maybeEat("=")) {
  835. // it was a key!
  836. String valueString = parseAnnotationNameValuePattern();
  837. if (valueString == null) {
  838. throw new ParserException("expecting simple literal ", tokenSource.peek(-1));
  839. }
  840. values.put(possibleKeyString, valueString);
  841. } else if (maybeEat("!=")) {
  842. // it was a key, with a !=
  843. String valueString = parseAnnotationNameValuePattern();
  844. if (valueString == null) {
  845. throw new ParserException("expecting simple literal ", tokenSource.peek(-1));
  846. }
  847. // negation is captured by adding a trailing ! to the key name
  848. values.put(possibleKeyString + "!", valueString);
  849. } else {
  850. if (seenDefaultValue) {
  851. throw new ParserException("cannot specify two default values", tokenSource.peek(-1));
  852. }
  853. seenDefaultValue = true;
  854. values.put("value", possibleKeyString);
  855. }
  856. } while (maybeEat(",")); // keep going whilst there are ','
  857. return values;
  858. }
  859. public TypePattern parseSingleTypePattern() {
  860. return parseSingleTypePattern(false);
  861. }
  862. public TypePattern parseSingleTypePattern(boolean insideTypeParameters) {
  863. if (insideTypeParameters && maybeEat("?")) {
  864. return parseGenericsWildcardTypePattern();
  865. }
  866. if (allowHasTypePatterns) {
  867. if (maybeEatIdentifier("hasmethod")) {
  868. return parseHasMethodTypePattern();
  869. }
  870. if (maybeEatIdentifier("hasfield")) {
  871. return parseHasFieldTypePattern();
  872. }
  873. }
  874. // // Check for a type category
  875. // IToken token = tokenSource.peek();
  876. // if (token.isIdentifier()) {
  877. // String category = token.getString();
  878. // TypeCategoryTypePattern typeIsPattern = null;
  879. // if (category.equals("isClass")) {
  880. // typeIsPattern = new TypeCategoryTypePattern(TypeCategoryTypePattern.CLASS);
  881. // } else if (category.equals("isAspect")) {
  882. // typeIsPattern = new TypeCategoryTypePattern(TypeCategoryTypePattern.ASPECT);
  883. // } else if (category.equals("isInterface")) {
  884. // typeIsPattern = new TypeCategoryTypePattern(TypeCategoryTypePattern.INTERFACE);
  885. // } else if (category.equals("isInner")) {
  886. // typeIsPattern = new TypeCategoryTypePattern(TypeCategoryTypePattern.INNER);
  887. // } else if (category.equals("isAnonymous")) {
  888. // typeIsPattern = new TypeCategoryTypePattern(TypeCategoryTypePattern.ANONYMOUS);
  889. // } else if (category.equals("isEnum")) {
  890. // typeIsPattern = new TypeCategoryTypePattern(TypeCategoryTypePattern.ENUM);
  891. // } else if (category.equals("isAnnotation")) {
  892. // typeIsPattern = new TypeCategoryTypePattern(TypeCategoryTypePattern.ANNOTATION);
  893. // }
  894. // if (typeIsPattern != null) {
  895. // tokenSource.next();
  896. // typeIsPattern.setLocation(tokenSource.getSourceContext(), token.getStart(), token.getEnd());
  897. // return typeIsPattern;
  898. // }
  899. // }
  900. if (maybeEatIdentifier("is")) {
  901. int pos = tokenSource.getIndex() - 1;
  902. TypePattern typeIsPattern = parseIsTypePattern();
  903. if (typeIsPattern != null) {
  904. return typeIsPattern;
  905. }
  906. // rewind as if we never tried to parse it as a typeIs
  907. tokenSource.setIndex(pos);
  908. }
  909. List<NamePattern> names = parseDottedNamePattern();
  910. int dim = 0;
  911. while (maybeEat("[")) {
  912. eat("]");
  913. dim++;
  914. }
  915. TypePatternList typeParameters = maybeParseTypeParameterList();
  916. int endPos = tokenSource.peek(-1).getEnd();
  917. boolean includeSubtypes = maybeEat("+");
  918. // TODO do we need to associate the + with either the type or the array?
  919. while (maybeEat("[")) {
  920. eat("]");
  921. dim++;
  922. }
  923. boolean isVarArgs = maybeEat("...");
  924. // ??? what about the source location of any's????
  925. if (names.size() == 1 && names.get(0).isAny() && dim == 0 && !isVarArgs && typeParameters == null) {
  926. return TypePattern.ANY;
  927. }
  928. // Notice we increase the dimensions if varargs is set. this is to allow type matching to
  929. // succeed later: The actual signature at runtime of a method declared varargs is an array type of
  930. // the original declared type (so Integer... becomes Integer[] in the bytecode). So, here for the
  931. // pattern 'Integer...' we create a WildTypePattern 'Integer[]' with varargs set. If this matches
  932. // during shadow matching, we confirm that the varargs flags match up before calling it a successful
  933. // match.
  934. return new WildTypePattern(names, includeSubtypes, dim + (isVarArgs ? 1 : 0), endPos, isVarArgs, typeParameters);
  935. }
  936. public TypePattern parseHasMethodTypePattern() {
  937. int startPos = tokenSource.peek(-1).getStart();
  938. eat("(");
  939. SignaturePattern sp = parseMethodOrConstructorSignaturePattern();
  940. eat(")");
  941. int endPos = tokenSource.peek(-1).getEnd();
  942. HasMemberTypePattern ret = new HasMemberTypePattern(sp);
  943. ret.setLocation(sourceContext, startPos, endPos);
  944. return ret;
  945. }
  946. /**
  947. * Attempt to parse a typeIs(<category>) construct. If it cannot be parsed we just return null and that should cause the caller
  948. * to reset their position and attempt to consume it in another way. This means we won't have problems here: execution(*
  949. * typeIs(..)) because someone has decided to call a method the same as our construct.
  950. *
  951. * @return a TypeIsTypePattern or null if could not be parsed
  952. */
  953. public TypePattern parseIsTypePattern() {
  954. int startPos = tokenSource.peek(-1).getStart(); // that will be the start of the 'typeIs'
  955. if (!maybeEatAdjacent("(")) {
  956. return null;
  957. }
  958. IToken token = tokenSource.next();
  959. TypeCategoryTypePattern typeIsPattern = null;
  960. if (token.isIdentifier()) {
  961. String category = token.getString();
  962. if (category.equals("ClassType")) {
  963. typeIsPattern = new TypeCategoryTypePattern(TypeCategoryTypePattern.CLASS);
  964. } else if (category.equals("AspectType")) {
  965. typeIsPattern = new TypeCategoryTypePattern(TypeCategoryTypePattern.ASPECT);
  966. } else if (category.equals("InterfaceType")) {
  967. typeIsPattern = new TypeCategoryTypePattern(TypeCategoryTypePattern.INTERFACE);
  968. } else if (category.equals("InnerType")) {
  969. typeIsPattern = new TypeCategoryTypePattern(TypeCategoryTypePattern.INNER);
  970. } else if (category.equals("AnonymousType")) {
  971. typeIsPattern = new TypeCategoryTypePattern(TypeCategoryTypePattern.ANONYMOUS);
  972. } else if (category.equals("EnumType")) {
  973. typeIsPattern = new TypeCategoryTypePattern(TypeCategoryTypePattern.ENUM);
  974. } else if (category.equals("AnnotationType")) {
  975. typeIsPattern = new TypeCategoryTypePattern(TypeCategoryTypePattern.ANNOTATION);
  976. }
  977. }
  978. if (typeIsPattern == null) {
  979. return null;
  980. }
  981. if (!maybeEat(")")) {
  982. throw new ParserException(")", tokenSource.peek());
  983. }
  984. int endPos = tokenSource.peek(-1).getEnd();
  985. typeIsPattern.setLocation(tokenSource.getSourceContext(), startPos, endPos);
  986. return typeIsPattern;
  987. }
  988. // if (names.size() == 1 && !names.get(0).isAny()) {
  989. // if (maybeEatAdjacent("(")) {
  990. // if (maybeEat(")")) {
  991. // // likely to be one of isClass()/isInterface()/isInner()/isAnonymous()/isAspect()
  992. // if (names.size() == 1) {
  993. // NamePattern np = names.get(0);
  994. // String simpleName = np.maybeGetSimpleName();
  995. // if (simpleName != null) {
  996. // return new TypeCategoryTypePattern(TypeCategoryTypePattern.ANNOTATION, np);
  997. // } else {
  998. // throw new ParserException(
  999. // "not a supported type category, supported are isClass/isInterface/isEnum/isAnnotation/isInner/isAnonymous",
  1000. // tokenSource.peek(-3));
  1001. // }
  1002. // }
  1003. // int stop = 1;
  1004. // // return new WildTypePattern(names, includeSubtypes, dim + (isVarArgs ? 1 : 0), endPos, isVarArgs,
  1005. // // typeParameters);
  1006. // }
  1007. // } else {
  1008. // throw new ParserException("category type pattern is missing closing parentheses", tokenSource.peek(-2));
  1009. // }
  1010. // }
  1011. // }
  1012. public TypePattern parseHasFieldTypePattern() {
  1013. int startPos = tokenSource.peek(-1).getStart();
  1014. eat("(");
  1015. SignaturePattern sp = parseFieldSignaturePattern();
  1016. eat(")");
  1017. int endPos = tokenSource.peek(-1).getEnd();
  1018. HasMemberTypePattern ret = new HasMemberTypePattern(sp);
  1019. ret.setLocation(sourceContext, startPos, endPos);
  1020. return ret;
  1021. }
  1022. public TypePattern parseGenericsWildcardTypePattern() {
  1023. List<NamePattern> names = new ArrayList<NamePattern>();
  1024. names.add(new NamePattern("?"));
  1025. TypePattern upperBound = null;
  1026. TypePattern[] additionalInterfaceBounds = new TypePattern[0];
  1027. TypePattern lowerBound = null;
  1028. if (maybeEatIdentifier("extends")) {
  1029. upperBound = parseTypePattern(false, false);
  1030. additionalInterfaceBounds = maybeParseAdditionalInterfaceBounds();
  1031. }
  1032. if (maybeEatIdentifier("super")) {
  1033. lowerBound = parseTypePattern(false, false);
  1034. }
  1035. int endPos = tokenSource.peek(-1).getEnd();
  1036. return new WildTypePattern(names, false, 0, endPos, false, null, upperBound, additionalInterfaceBounds, lowerBound);
  1037. }
  1038. // private AnnotationTypePattern completeAnnotationPattern(AnnotationTypePattern p) {
  1039. // if (maybeEat("&&")) {
  1040. // return new AndAnnotationTypePattern(p,parseNotOrAnnotationPattern());
  1041. // }
  1042. // if (maybeEat("||")) {
  1043. // return new OrAnnotationTypePattern(p,parseAnnotationTypePattern());
  1044. // }
  1045. // return p;
  1046. // }
  1047. //
  1048. // protected AnnotationTypePattern parseAnnotationTypePattern() {
  1049. // AnnotationTypePattern ap = parseAtomicAnnotationPattern();
  1050. // if (maybeEat("&&")) {
  1051. // ap = new AndAnnotationTypePattern(ap, parseNotOrAnnotationPattern());
  1052. // }
  1053. //
  1054. // if (maybeEat("||")) {
  1055. // ap = new OrAnnotationTypePattern(ap, parseAnnotationTypePattern());
  1056. // }
  1057. // return ap;
  1058. // }
  1059. //
  1060. // private AnnotationTypePattern parseNotOrAnnotationPattern() {
  1061. // AnnotationTypePattern p = parseAtomicAnnotationPattern();
  1062. // if (maybeEat("&&")) {
  1063. // p = new AndAnnotationTypePattern(p,parseAnnotationTypePattern());
  1064. // }
  1065. // return p;
  1066. // }
  1067. protected ExactAnnotationTypePattern parseAnnotationNameOrVarTypePattern() {
  1068. ExactAnnotationTypePattern p = null;
  1069. int startPos = tokenSource.peek().getStart();
  1070. if (maybeEat("@")) {
  1071. throw new ParserException("@Foo form was deprecated in AspectJ 5 M2: annotation name or var ", tokenSource.peek(-1));
  1072. }
  1073. p = parseSimpleAnnotationName();
  1074. int endPos = tokenSource.peek(-1).getEnd();
  1075. p.setLocation(sourceContext, startPos, endPos);
  1076. // For optimized syntax that allows binding directly to annotation values (pr234943)
  1077. if (maybeEat("(")) {
  1078. String formalName = parseIdentifier();
  1079. p = new ExactAnnotationFieldTypePattern(p, formalName);
  1080. eat(")");
  1081. }
  1082. return p;
  1083. }
  1084. /**
  1085. * @return
  1086. */
  1087. private ExactAnnotationTypePattern parseSimpleAnnotationName() {
  1088. // the @ has already been eaten...
  1089. ExactAnnotationTypePattern p;
  1090. StringBuffer annotationName = new StringBuffer();
  1091. annotationName.append(parseIdentifier());
  1092. while (maybeEat(".")) {
  1093. annotationName.append('.');
  1094. annotationName.append(parseIdentifier());
  1095. }
  1096. UnresolvedType type = UnresolvedType.forName(annotationName.toString());
  1097. p = new ExactAnnotationTypePattern(type, null);
  1098. return p;
  1099. }
  1100. // private AnnotationTypePattern parseAtomicAnnotationPattern() {
  1101. // if (maybeEat("!")) {
  1102. // //int startPos = tokenSource.peek(-1).getStart();
  1103. // //??? we lose source location for true start of !type
  1104. // AnnotationTypePattern p = new NotAnnotationTypePattern(parseAtomicAnnotationPattern());
  1105. // return p;
  1106. // }
  1107. // if (maybeEat("(")) {
  1108. // AnnotationTypePattern p = parseAnnotationTypePattern();
  1109. // eat(")");
  1110. // return p;
  1111. // }
  1112. // int startPos = tokenSource.peek().getStart();
  1113. // eat("@");
  1114. // StringBuffer annotationName = new StringBuffer();
  1115. // annotationName.append(parseIdentifier());
  1116. // while (maybeEat(".")) {
  1117. // annotationName.append('.');
  1118. // annotationName.append(parseIdentifier());
  1119. // }
  1120. // UnresolvedType type = UnresolvedType.forName(annotationName.toString());
  1121. // AnnotationTypePattern p = new ExactAnnotationTypePattern(type);
  1122. // int endPos = tokenSource.peek(-1).getEnd();
  1123. // p.setLocation(sourceContext, startPos, endPos);
  1124. // return p;
  1125. // }
  1126. public List<NamePattern> parseDottedNamePattern() {
  1127. List<NamePattern> names = new ArrayList<NamePattern>();
  1128. StringBuffer buf = new StringBuffer();
  1129. IToken previous = null;
  1130. boolean justProcessedEllipsis = false; // Remember if we just dealt with an ellipsis (PR61536)
  1131. boolean justProcessedDot = false;
  1132. boolean onADot = false;
  1133. while (true) {
  1134. IToken tok = null;
  1135. int startPos = tokenSource.peek().getStart();
  1136. String afterDot = null;
  1137. while (true) {
  1138. if (previous != null && previous.getString().equals(".")) {
  1139. justProcessedDot = true;
  1140. }
  1141. tok = tokenSource.peek();
  1142. onADot = (tok.getString().equals("."));
  1143. if (previous != null) {
  1144. if (!isAdjacent(previous, tok)) {
  1145. break;
  1146. }
  1147. }
  1148. if (tok.getString() == "*" || (tok.isIdentifier() && tok.getString() != "...")) {
  1149. buf.append(tok.getString());
  1150. } else if (tok.getString() == "...") {
  1151. break;
  1152. } else if (tok.getLiteralKind() != null) {
  1153. // System.err.println("literal kind: " + tok.getString());
  1154. String s = tok.getString();
  1155. int dot = s.indexOf('.');
  1156. if (dot != -1) {
  1157. buf.append(s.substring(0, dot));
  1158. afterDot = s.substring(dot + 1);
  1159. previous = tokenSource.next();
  1160. break;
  1161. }
  1162. buf.append(s); // ??? so-so
  1163. } else {
  1164. break;
  1165. }
  1166. previous = tokenSource.next();
  1167. // XXX need to handle floats and other fun stuff
  1168. }
  1169. int endPos = tokenSource.peek(-1).getEnd();
  1170. if (buf.length() == 0 && names.isEmpty()) {
  1171. throw new ParserException("name pattern", tok);
  1172. }
  1173. if (buf.length() == 0 && justProcessedEllipsis) {
  1174. throw new ParserException("name pattern cannot finish with ..", tok);
  1175. }
  1176. if (buf.length() == 0 && justProcessedDot && !onADot) {
  1177. throw new ParserException("name pattern cannot finish with .", tok);
  1178. }
  1179. if (buf.length() == 0) {
  1180. names.add(NamePattern.ELLIPSIS);
  1181. justProcessedEllipsis = true;
  1182. } else {
  1183. checkLegalName(buf.toString(), previous);
  1184. NamePattern ret = new NamePattern(buf.toString());
  1185. ret.setLocation(sourceContext, startPos, endPos);
  1186. names.add(ret);
  1187. justProcessedEllipsis = false;
  1188. }
  1189. if (afterDot == null) {
  1190. buf.setLength(0);
  1191. // no elipsis or dotted name part
  1192. if (!maybeEat(".")) {
  1193. break;
  1194. // go on
  1195. } else {
  1196. previous = tokenSource.peek(-1);
  1197. }
  1198. } else {
  1199. buf.setLength(0);
  1200. buf.append(afterDot);
  1201. afterDot = null;
  1202. }
  1203. }
  1204. // System.err.println("parsed: " + names);
  1205. return names;
  1206. }
  1207. // supported form 'a.b.c.d' or just 'a'
  1208. public String parseAnnotationNameValuePattern() {
  1209. StringBuffer buf = new StringBuffer();
  1210. IToken tok;
  1211. // int startPos =
  1212. tokenSource.peek().getStart();
  1213. boolean dotOK = false;
  1214. int depth = 0;
  1215. while (true) {
  1216. tok = tokenSource.peek();
  1217. // keep going until we hit ')' or '=' or ','
  1218. if (tok.getString() == ")" && depth == 0) {
  1219. break;
  1220. }
  1221. if (tok.getString() == "!=" && depth == 0) {
  1222. break;
  1223. }
  1224. if (tok.getString() == "=" && depth == 0) {
  1225. break;
  1226. }
  1227. if (tok.getString() == "," && depth == 0) {
  1228. break;
  1229. }
  1230. if (tok == IToken.EOF) {
  1231. throw new ParserException("eof", tokenSource.peek());
  1232. }
  1233. // keep track of nested brackets
  1234. if (tok.getString() == "(") {
  1235. depth++;
  1236. }
  1237. if (tok.getString() == ")") {
  1238. depth--;
  1239. }
  1240. if (tok.getString() == "{") {
  1241. depth++;
  1242. }
  1243. if (tok.getString() == "}") {
  1244. depth--;
  1245. }
  1246. if (tok.getString() == "." && !dotOK) {
  1247. throw new ParserException("dot not expected", tok);
  1248. }
  1249. buf.append(tok.getString());
  1250. tokenSource.next();
  1251. dotOK = true;
  1252. }
  1253. return buf.toString();
  1254. }
  1255. public NamePattern parseNamePattern() {
  1256. StringBuffer buf = new StringBuffer();
  1257. IToken previous = null;
  1258. IToken tok;
  1259. int startPos = tokenSource.peek().getStart();
  1260. while (true) {
  1261. tok = tokenSource.peek();
  1262. if (previous != null) {
  1263. if (!isAdjacent(previous, tok)) {
  1264. break;
  1265. }
  1266. }
  1267. if (tok.getString() == "*" || tok.isIdentifier()) {
  1268. buf.append(tok.getString());
  1269. } else if (tok.getLiteralKind() != null) {
  1270. // System.err.println("literal kind: " + tok.getString());
  1271. String s = tok.getString();
  1272. if (s.indexOf('.') != -1) {
  1273. break;
  1274. }
  1275. buf.append(s); // ??? so-so
  1276. } else {
  1277. break;
  1278. }
  1279. previous = tokenSource.next();
  1280. // XXX need to handle floats and other fun stuff
  1281. }
  1282. int endPos = tokenSource.peek(-1).getEnd();
  1283. if (buf.length() == 0) {
  1284. throw new ParserException("name pattern", tok);
  1285. }
  1286. checkLegalName(buf.toString(), previous);
  1287. NamePattern ret = new NamePattern(buf.toString());
  1288. ret.setLocation(sourceContext, startPos, endPos);
  1289. return ret;
  1290. }
  1291. private void checkLegalName(String s, IToken tok) {
  1292. char ch = s.charAt(0);
  1293. if (!(ch == '*' || Character.isJavaIdentifierStart(ch))) {
  1294. throw new ParserException("illegal identifier start (" + ch + ")", tok);
  1295. }
  1296. for (int i = 1, len = s.length(); i < len; i++) {
  1297. ch = s.charAt(i);
  1298. if (!(ch == '*' || Character.isJavaIdentifierPart(ch))) {
  1299. throw new ParserException("illegal identifier character (" + ch + ")", tok);
  1300. }
  1301. }
  1302. }
  1303. private boolean isAdjacent(IToken first, IToken second) {
  1304. return first.getEnd() == second.getStart() - 1;
  1305. }
  1306. public ModifiersPattern parseModifiersPattern() {
  1307. int requiredFlags = 0;
  1308. int forbiddenFlags = 0;
  1309. int start;
  1310. while (true) {
  1311. start = tokenSource.getIndex();
  1312. boolean isForbidden = false;
  1313. isForbidden = maybeEat("!");
  1314. IToken t = tokenSource.next();
  1315. int flag = ModifiersPattern.getModifierFlag(t.getString());
  1316. if (flag == -1) {
  1317. break;
  1318. }
  1319. if (isForbidden) {
  1320. forbiddenFlags |= flag;
  1321. } else {
  1322. requiredFlags |= flag;
  1323. }
  1324. }
  1325. tokenSource.setIndex(start);
  1326. if (requiredFlags == 0 && forbiddenFlags == 0) {
  1327. return ModifiersPattern.ANY;
  1328. } else {
  1329. return new ModifiersPattern(requiredFlags, forbiddenFlags);
  1330. }
  1331. }
  1332. public TypePatternList parseArgumentsPattern(boolean parameterAnnotationsPossible) {
  1333. List<TypePattern> patterns = new ArrayList<TypePattern>();
  1334. eat("(");
  1335. // ()
  1336. if (maybeEat(")")) {
  1337. return new TypePatternList();
  1338. }
  1339. do {
  1340. if (maybeEat(".")) { // ..
  1341. eat(".");
  1342. patterns.add(TypePattern.ELLIPSIS);
  1343. } else {
  1344. patterns.add(parseTypePattern(false, parameterAnnotationsPossible));
  1345. }
  1346. } while (maybeEat(","));
  1347. eat(")");
  1348. return new TypePatternList(patterns);
  1349. }
  1350. public AnnotationPatternList parseArgumentsAnnotationPattern() {
  1351. List<AnnotationTypePattern> patterns = new ArrayList<AnnotationTypePattern>();
  1352. eat("(");
  1353. if (maybeEat(")")) {
  1354. return new AnnotationPatternList();
  1355. }
  1356. do {
  1357. if (maybeEat(".")) {
  1358. eat(".");
  1359. patterns.add(AnnotationTypePattern.ELLIPSIS);
  1360. } else if (maybeEat("*")) {
  1361. patterns.add(AnnotationTypePattern.ANY);
  1362. } else {
  1363. patterns.add(parseAnnotationNameOrVarTypePattern());
  1364. }
  1365. } while (maybeEat(","));
  1366. eat(")");
  1367. return new AnnotationPatternList(patterns);
  1368. }
  1369. public ThrowsPattern parseOptionalThrowsPattern() {
  1370. IToken t = tokenSource.peek();
  1371. if (t.isIdentifier() && t.getString().equals("throws")) {
  1372. tokenSource.next();
  1373. List<TypePattern> required = new ArrayList<TypePattern>();
  1374. List<TypePattern> forbidden = new ArrayList<TypePattern>();
  1375. do {
  1376. boolean isForbidden = maybeEat("!");
  1377. // ???might want an error for a second ! without a paren
  1378. TypePattern p = parseTypePattern();
  1379. if (isForbidden) {
  1380. forbidden.add(p);
  1381. } else {
  1382. required.add(p);
  1383. }
  1384. } while (maybeEat(","));
  1385. return new ThrowsPattern(new TypePatternList(required), new TypePatternList(forbidden));
  1386. }
  1387. return ThrowsPattern.ANY;
  1388. }
  1389. public SignaturePattern parseMethodOrConstructorSignaturePattern() {
  1390. int startPos = tokenSource.peek().getStart();
  1391. AnnotationTypePattern annotationPattern = maybeParseAnnotationPattern();
  1392. ModifiersPattern modifiers = parseModifiersPattern();
  1393. TypePattern returnType = parseTypePattern(false, false);
  1394. TypePattern declaringType;
  1395. NamePattern name = null;
  1396. MemberKind kind;
  1397. // here we can check for 'new'
  1398. if (maybeEatNew(returnType)) {
  1399. kind = Member.CONSTRUCTOR;
  1400. if (returnType.toString().length() == 0) {
  1401. declaringType = TypePattern.ANY;
  1402. } else {
  1403. declaringType = returnType;
  1404. }
  1405. returnType = TypePattern.ANY;
  1406. name = NamePattern.ANY;
  1407. } else {
  1408. kind = Member.METHOD;
  1409. IToken nameToken = tokenSource.peek();
  1410. declaringType = parseTypePattern(false, false);
  1411. if (maybeEat(".")) {
  1412. nameToken = tokenSource.peek();
  1413. name = parseNamePattern();
  1414. } else {
  1415. name = tryToExtractName(declaringType);
  1416. if (declaringType.toString().equals("")) {
  1417. declaringType = TypePattern.ANY;
  1418. }
  1419. }
  1420. if (name == null) {
  1421. throw new ParserException("name pattern", tokenSource.peek());
  1422. }
  1423. String simpleName = name.maybeGetSimpleName();
  1424. // XXX should add check for any Java keywords
  1425. if (simpleName != null && simpleName.equals("new")) {
  1426. throw new ParserException("method name (not constructor)", nameToken);
  1427. }
  1428. }
  1429. TypePatternList parameterTypes = parseArgumentsPattern(true);
  1430. ThrowsPattern throwsPattern = parseOptionalThrowsPattern();
  1431. SignaturePattern ret = new SignaturePattern(kind, modifiers, returnType, declaringType, name, parameterTypes,
  1432. throwsPattern, annotationPattern);
  1433. int endPos = tokenSource.peek(-1).getEnd();
  1434. ret.setLocation(sourceContext, startPos, endPos);
  1435. return ret;
  1436. }
  1437. private boolean maybeEatNew(TypePattern returnType) {
  1438. if (returnType instanceof WildTypePattern) {
  1439. WildTypePattern p = (WildTypePattern) returnType;
  1440. if (p.maybeExtractName("new")) {
  1441. return true;
  1442. }
  1443. }
  1444. int start = tokenSource.getIndex();
  1445. if (maybeEat(".")) {
  1446. String id = maybeEatIdentifier();
  1447. if (id != null && id.equals("new")) {
  1448. return true;
  1449. }
  1450. tokenSource.setIndex(start);
  1451. }
  1452. return false;
  1453. }
  1454. public ISignaturePattern parseMaybeParenthesizedFieldSignaturePattern() {
  1455. boolean negated = tokenSource.peek().getString().equals("!") && tokenSource.peek(1).getString().equals("(");
  1456. if (negated) {
  1457. eat("!");
  1458. }
  1459. ISignaturePattern result = null;
  1460. if (maybeEat("(")) {
  1461. result = parseCompoundFieldSignaturePattern();
  1462. eat(")", "missing ')' - unbalanced parentheses around field signature pattern in declare @field");
  1463. if (negated) {
  1464. result = new NotSignaturePattern(result);
  1465. }
  1466. } else {
  1467. result = parseFieldSignaturePattern();
  1468. }
  1469. return result;
  1470. }
  1471. public ISignaturePattern parseMaybeParenthesizedMethodOrConstructorSignaturePattern(boolean isMethod) {
  1472. boolean negated = tokenSource.peek().getString().equals("!") && tokenSource.peek(1).getString().equals("(");
  1473. if (negated) {
  1474. eat("!");
  1475. }
  1476. ISignaturePattern result = null;
  1477. if (maybeEat("(")) {
  1478. result = parseCompoundMethodOrConstructorSignaturePattern(isMethod);
  1479. eat(")", "missing ')' - unbalanced parentheses around method/ctor signature pattern in declare annotation");
  1480. if (negated) {
  1481. result = new NotSignaturePattern(result);
  1482. }
  1483. } else {
  1484. SignaturePattern sp = parseMethodOrConstructorSignaturePattern();
  1485. boolean isConstructorPattern = (sp.getKind() == Member.CONSTRUCTOR);
  1486. if (isMethod && isConstructorPattern) {
  1487. throw new ParserException("method signature pattern", tokenSource.peek(-1));
  1488. }
  1489. if (!isMethod && !isConstructorPattern) {
  1490. throw new ParserException("constructor signature pattern", tokenSource.peek(-1));
  1491. }
  1492. result = sp;
  1493. }
  1494. return result;
  1495. }
  1496. public SignaturePattern parseFieldSignaturePattern() {
  1497. int startPos = tokenSource.peek().getStart();
  1498. // TypePatternList followMe = TypePatternList.ANY;
  1499. AnnotationTypePattern annotationPattern = maybeParseAnnotationPattern();
  1500. ModifiersPattern modifiers = parseModifiersPattern();
  1501. TypePattern returnType = parseTypePattern();
  1502. TypePattern declaringType = parseTypePattern();
  1503. NamePattern name;
  1504. // System.err.println("parsed field: " + declaringType.toString());
  1505. if (maybeEat(".")) {
  1506. name = parseNamePattern();
  1507. } else {
  1508. name = tryToExtractName(declaringType);
  1509. if (name == null) {
  1510. throw new ParserException("name pattern", tokenSource.peek());
  1511. }
  1512. if (declaringType.toString().equals("")) {
  1513. declaringType = TypePattern.ANY;
  1514. }
  1515. }
  1516. SignaturePattern ret = new SignaturePattern(Member.FIELD, modifiers, returnType, declaringType, name, TypePatternList.ANY,
  1517. ThrowsPattern.ANY, annotationPattern);
  1518. int endPos = tokenSource.peek(-1).getEnd();
  1519. ret.setLocation(sourceContext, startPos, endPos);
  1520. return ret;
  1521. }
  1522. private NamePattern tryToExtractName(TypePattern nextType) {
  1523. if (nextType == TypePattern.ANY) {
  1524. return NamePattern.ANY;
  1525. } else if (nextType instanceof WildTypePattern) {
  1526. WildTypePattern p = (WildTypePattern) nextType;
  1527. return p.extractName();
  1528. } else {
  1529. return null;
  1530. }
  1531. }
  1532. /**
  1533. * Parse type variable declarations for a generic method or at the start of a signature pointcut to identify type variable names
  1534. * in a generic type.
  1535. *
  1536. * @param includeParameterizedTypes
  1537. * @return
  1538. */
  1539. public TypeVariablePatternList maybeParseTypeVariableList() {
  1540. if (!maybeEat("<")) {
  1541. return null;
  1542. }
  1543. List<TypeVariablePattern> typeVars = new ArrayList<TypeVariablePattern>();
  1544. TypeVariablePattern t = parseTypeVariable();
  1545. typeVars.add(t);
  1546. while (maybeEat(",")) {
  1547. TypeVariablePattern nextT = parseTypeVariable();
  1548. typeVars.add(nextT);
  1549. }
  1550. eat(">");
  1551. TypeVariablePattern[] tvs = new TypeVariablePattern[typeVars.size()];
  1552. typeVars.toArray(tvs);
  1553. return new TypeVariablePatternList(tvs);
  1554. }
  1555. // of the form execution<T,S,V> - allows identifiers only
  1556. public String[] maybeParseSimpleTypeVariableList() {
  1557. if (!maybeEat("<")) {
  1558. return null;
  1559. }
  1560. List<String> typeVarNames = new ArrayList<String>();
  1561. do {
  1562. typeVarNames.add(parseIdentifier());
  1563. } while (maybeEat(","));
  1564. eat(">", "',' or '>'");
  1565. String[] tvs = new String[typeVarNames.size()];
  1566. typeVarNames.toArray(tvs);
  1567. return tvs;
  1568. }
  1569. public TypePatternList maybeParseTypeParameterList() {
  1570. if (!maybeEat("<")) {
  1571. return null;
  1572. }
  1573. List<TypePattern> typePats = new ArrayList<TypePattern>();
  1574. do {
  1575. TypePattern tp = parseTypePattern(true, false);
  1576. typePats.add(tp);
  1577. } while (maybeEat(","));
  1578. eat(">");
  1579. TypePattern[] tps = new TypePattern[typePats.size()];
  1580. typePats.toArray(tps);
  1581. return new TypePatternList(tps);
  1582. }
  1583. public TypeVariablePattern parseTypeVariable() {
  1584. TypePattern upperBound = null;
  1585. TypePattern[] additionalInterfaceBounds = null;
  1586. TypePattern lowerBound = null;
  1587. String typeVariableName = parseIdentifier();
  1588. if (maybeEatIdentifier("extends")) {
  1589. upperBound = parseTypePattern();
  1590. additionalInterfaceBounds = maybeParseAdditionalInterfaceBounds();
  1591. } else if (maybeEatIdentifier("super")) {
  1592. lowerBound = parseTypePattern();
  1593. }
  1594. return new TypeVariablePattern(typeVariableName, upperBound, additionalInterfaceBounds, lowerBound);
  1595. }
  1596. private TypePattern[] maybeParseAdditionalInterfaceBounds() {
  1597. List<TypePattern> boundsList = new ArrayList<TypePattern>();
  1598. while (maybeEat("&")) {
  1599. TypePattern tp = parseTypePattern();
  1600. boundsList.add(tp);
  1601. }
  1602. if (boundsList.size() == 0) {
  1603. return null;
  1604. }
  1605. TypePattern[] ret = new TypePattern[boundsList.size()];
  1606. boundsList.toArray(ret);
  1607. return ret;
  1608. }
  1609. public String parsePossibleStringSequence(boolean shouldEnd) {
  1610. StringBuffer result = new StringBuffer();
  1611. IToken token = tokenSource.next();
  1612. if (token.getLiteralKind() == null) {
  1613. throw new ParserException("string", token);
  1614. }
  1615. while (token.getLiteralKind().equals("string")) {
  1616. result.append(token.getString());
  1617. boolean plus = maybeEat("+");
  1618. if (!plus) {
  1619. break;
  1620. }
  1621. token = tokenSource.next();
  1622. if (token.getLiteralKind() == null) {
  1623. throw new ParserException("string", token);
  1624. }
  1625. }
  1626. eatIdentifier(";");
  1627. IToken t = tokenSource.next();
  1628. if (shouldEnd && t != IToken.EOF) {
  1629. throw new ParserException("<string>;", token);
  1630. }
  1631. // bug 125027: since we've eaten the ";" we need to set the index
  1632. // to be one less otherwise the end position isn't set correctly.
  1633. int currentIndex = tokenSource.getIndex();
  1634. tokenSource.setIndex(currentIndex - 1);
  1635. return result.toString();
  1636. }
  1637. public String parseStringLiteral() {
  1638. IToken token = tokenSource.next();
  1639. String literalKind = token.getLiteralKind();
  1640. if (literalKind == "string") {
  1641. return token.getString();
  1642. }
  1643. throw new ParserException("string", token);
  1644. }
  1645. public String parseIdentifier() {
  1646. IToken token = tokenSource.next();
  1647. if (token.isIdentifier()) {
  1648. return token.getString();
  1649. }
  1650. throw new ParserException("identifier", token);
  1651. }
  1652. public void eatIdentifier(String expectedValue) {
  1653. IToken next = tokenSource.next();
  1654. if (!next.getString().equals(expectedValue)) {
  1655. throw new ParserException(expectedValue, next);
  1656. }
  1657. }
  1658. public boolean maybeEatIdentifier(String expectedValue) {
  1659. IToken next = tokenSource.peek();
  1660. if (next.getString().equals(expectedValue)) {
  1661. tokenSource.next();
  1662. return true;
  1663. } else {
  1664. return false;
  1665. }
  1666. }
  1667. public void eat(String expectedValue) {
  1668. eat(expectedValue, expectedValue);
  1669. }
  1670. private void eat(String expectedValue, String expectedMessage) {
  1671. IToken next = nextToken();
  1672. if (next.getString() != expectedValue) {
  1673. if (expectedValue.equals(">") && next.getString().startsWith(">")) {
  1674. // handle problem of >> and >>> being lexed as single tokens
  1675. pendingRightArrows = BasicToken.makeLiteral(next.getString().substring(1).intern(), "string", next.getStart() + 1,
  1676. next.getEnd());
  1677. return;
  1678. }
  1679. throw new ParserException(expectedMessage, next);
  1680. }
  1681. }
  1682. private IToken pendingRightArrows;
  1683. private IToken nextToken() {
  1684. if (pendingRightArrows != null) {
  1685. IToken ret = pendingRightArrows;
  1686. pendingRightArrows = null;
  1687. return ret;
  1688. } else {
  1689. return tokenSource.next();
  1690. }
  1691. }
  1692. public boolean maybeEatAdjacent(String token) {
  1693. IToken next = tokenSource.peek();
  1694. if (next.getString() == token) {
  1695. if (isAdjacent(tokenSource.peek(-1), next)) {
  1696. tokenSource.next();
  1697. return true;
  1698. }
  1699. }
  1700. return false;
  1701. }
  1702. public boolean maybeEat(String token) {
  1703. IToken next = tokenSource.peek();
  1704. if (next.getString() == token) {
  1705. tokenSource.next();
  1706. return true;
  1707. } else {
  1708. return false;
  1709. }
  1710. }
  1711. public String maybeEatIdentifier() {
  1712. IToken next = tokenSource.peek();
  1713. if (next.isIdentifier()) {
  1714. tokenSource.next();
  1715. return next.getString();
  1716. } else {
  1717. return null;
  1718. }
  1719. }
  1720. public boolean peek(String token) {
  1721. IToken next = tokenSource.peek();
  1722. return next.getString() == token;
  1723. }
  1724. public void checkEof() {
  1725. IToken last = tokenSource.next();
  1726. if (last != IToken.EOF) {
  1727. throw new ParserException("unexpected pointcut element: " + last.toString(), last);
  1728. }
  1729. }
  1730. public PatternParser(String data) {
  1731. this(BasicTokenSource.makeTokenSource(data, null));
  1732. }
  1733. public PatternParser(String data, ISourceContext context) {
  1734. this(BasicTokenSource.makeTokenSource(data, context));
  1735. }
  1736. }