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

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