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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  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. * @return
  1553. */
  1554. public TypeVariablePatternList maybeParseTypeVariableList() {
  1555. if (!maybeEat("<")) {
  1556. return null;
  1557. }
  1558. List<TypeVariablePattern> typeVars = new ArrayList<>();
  1559. TypeVariablePattern t = parseTypeVariable();
  1560. typeVars.add(t);
  1561. while (maybeEat(",")) {
  1562. TypeVariablePattern nextT = parseTypeVariable();
  1563. typeVars.add(nextT);
  1564. }
  1565. eat(">");
  1566. TypeVariablePattern[] tvs = new TypeVariablePattern[typeVars.size()];
  1567. typeVars.toArray(tvs);
  1568. return new TypeVariablePatternList(tvs);
  1569. }
  1570. // of the form execution<T,S,V> - allows identifiers only
  1571. public String[] maybeParseSimpleTypeVariableList() {
  1572. if (!maybeEat("<")) {
  1573. return null;
  1574. }
  1575. List<String> typeVarNames = new ArrayList<>();
  1576. do {
  1577. typeVarNames.add(parseIdentifier());
  1578. } while (maybeEat(","));
  1579. eat(">", "',' or '>'");
  1580. String[] tvs = new String[typeVarNames.size()];
  1581. typeVarNames.toArray(tvs);
  1582. return tvs;
  1583. }
  1584. public TypePatternList maybeParseTypeParameterList() {
  1585. if (!maybeEat("<")) {
  1586. return null;
  1587. }
  1588. List<TypePattern> typePats = new ArrayList<>();
  1589. do {
  1590. TypePattern tp = parseTypePattern(true, false);
  1591. typePats.add(tp);
  1592. } while (maybeEat(","));
  1593. eat(">");
  1594. TypePattern[] tps = new TypePattern[typePats.size()];
  1595. typePats.toArray(tps);
  1596. return new TypePatternList(tps);
  1597. }
  1598. public TypeVariablePattern parseTypeVariable() {
  1599. TypePattern upperBound = null;
  1600. TypePattern[] additionalInterfaceBounds = null;
  1601. TypePattern lowerBound = null;
  1602. String typeVariableName = parseIdentifier();
  1603. if (maybeEatIdentifier("extends")) {
  1604. upperBound = parseTypePattern();
  1605. additionalInterfaceBounds = maybeParseAdditionalInterfaceBounds();
  1606. } else if (maybeEatIdentifier("super")) {
  1607. lowerBound = parseTypePattern();
  1608. }
  1609. return new TypeVariablePattern(typeVariableName, upperBound, additionalInterfaceBounds, lowerBound);
  1610. }
  1611. private TypePattern[] maybeParseAdditionalInterfaceBounds() {
  1612. List<TypePattern> boundsList = new ArrayList<>();
  1613. while (maybeEat("&")) {
  1614. TypePattern tp = parseTypePattern();
  1615. boundsList.add(tp);
  1616. }
  1617. if (boundsList.size() == 0) {
  1618. return null;
  1619. }
  1620. TypePattern[] ret = new TypePattern[boundsList.size()];
  1621. boundsList.toArray(ret);
  1622. return ret;
  1623. }
  1624. public String parsePossibleStringSequence(boolean shouldEnd) {
  1625. StringBuffer result = new StringBuffer();
  1626. IToken token = tokenSource.next();
  1627. if (token.getLiteralKind() == null) {
  1628. throw new ParserException("string", token);
  1629. }
  1630. while (token.getLiteralKind().equals("string")) {
  1631. result.append(token.getString());
  1632. boolean plus = maybeEat("+");
  1633. if (!plus) {
  1634. break;
  1635. }
  1636. token = tokenSource.next();
  1637. if (token.getLiteralKind() == null) {
  1638. throw new ParserException("string", token);
  1639. }
  1640. }
  1641. eatIdentifier(";");
  1642. IToken t = tokenSource.next();
  1643. if (shouldEnd && t != IToken.EOF) {
  1644. throw new ParserException("<string>;", token);
  1645. }
  1646. // bug 125027: since we've eaten the ";" we need to set the index
  1647. // to be one less otherwise the end position isn't set correctly.
  1648. int currentIndex = tokenSource.getIndex();
  1649. tokenSource.setIndex(currentIndex - 1);
  1650. return result.toString();
  1651. }
  1652. public String parseStringLiteral() {
  1653. IToken token = tokenSource.next();
  1654. String literalKind = token.getLiteralKind();
  1655. if (literalKind == "string") {
  1656. return token.getString();
  1657. }
  1658. throw new ParserException("string", token);
  1659. }
  1660. public String parseIdentifier() {
  1661. IToken token = tokenSource.next();
  1662. if (token.isIdentifier()) {
  1663. return token.getString();
  1664. }
  1665. throw new ParserException("identifier", token);
  1666. }
  1667. public void eatIdentifier(String expectedValue) {
  1668. IToken next = tokenSource.next();
  1669. if (!next.getString().equals(expectedValue)) {
  1670. throw new ParserException(expectedValue, next);
  1671. }
  1672. }
  1673. public boolean maybeEatIdentifier(String expectedValue) {
  1674. IToken next = tokenSource.peek();
  1675. if (next.getString().equals(expectedValue)) {
  1676. tokenSource.next();
  1677. return true;
  1678. } else {
  1679. return false;
  1680. }
  1681. }
  1682. public void eat(String expectedValue) {
  1683. eat(expectedValue, expectedValue);
  1684. }
  1685. private void eat(String expectedValue, String expectedMessage) {
  1686. IToken next = nextToken();
  1687. if (next.getString() != expectedValue) {
  1688. if (expectedValue.equals(">") && next.getString().startsWith(">")) {
  1689. // handle problem of >> and >>> being lexed as single tokens
  1690. pendingRightArrows = BasicToken.makeLiteral(next.getString().substring(1).intern(), "string", next.getStart() + 1,
  1691. next.getEnd());
  1692. return;
  1693. }
  1694. throw new ParserException(expectedMessage, next);
  1695. }
  1696. }
  1697. private IToken pendingRightArrows;
  1698. private IToken nextToken() {
  1699. if (pendingRightArrows != null) {
  1700. IToken ret = pendingRightArrows;
  1701. pendingRightArrows = null;
  1702. return ret;
  1703. } else {
  1704. return tokenSource.next();
  1705. }
  1706. }
  1707. public boolean maybeEatAdjacent(String token) {
  1708. IToken next = tokenSource.peek();
  1709. if (next.getString() == token) {
  1710. if (isAdjacent(tokenSource.peek(-1), next)) {
  1711. tokenSource.next();
  1712. return true;
  1713. }
  1714. }
  1715. return false;
  1716. }
  1717. public boolean maybeEat(String token) {
  1718. IToken next = tokenSource.peek();
  1719. if (next.getString() == token) {
  1720. tokenSource.next();
  1721. return true;
  1722. } else {
  1723. return false;
  1724. }
  1725. }
  1726. public String maybeEatIdentifier() {
  1727. IToken next = tokenSource.peek();
  1728. if (next.isIdentifier()) {
  1729. tokenSource.next();
  1730. return next.getString();
  1731. } else {
  1732. return null;
  1733. }
  1734. }
  1735. public boolean peek(String token) {
  1736. IToken next = tokenSource.peek();
  1737. return next.getString() == token;
  1738. }
  1739. public void checkEof() {
  1740. IToken last = tokenSource.next();
  1741. if (last != IToken.EOF) {
  1742. throw new ParserException("unexpected pointcut element: " + last.toString(), last);
  1743. }
  1744. }
  1745. public PatternParser(String data) {
  1746. this(BasicTokenSource.makeTokenSource(data, null));
  1747. }
  1748. public PatternParser(String data, ISourceContext context) {
  1749. this(BasicTokenSource.makeTokenSource(data, context));
  1750. }
  1751. }