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.

StandardPointcutParser.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*******************************************************************************
  2. * Copyright (c) 2004 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v 2.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.aspectj.weaver.tools;
  12. import java.io.File;
  13. import java.util.HashSet;
  14. import java.util.Properties;
  15. import java.util.Set;
  16. import org.aspectj.bridge.IMessageHandler;
  17. import org.aspectj.bridge.ISourceLocation;
  18. import org.aspectj.bridge.SourceLocation;
  19. import org.aspectj.weaver.BindingScope;
  20. import org.aspectj.weaver.IHasPosition;
  21. import org.aspectj.weaver.ISourceContext;
  22. import org.aspectj.weaver.IntMap;
  23. import org.aspectj.weaver.ResolvedType;
  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.StandardPointcutExpressionImpl;
  28. import org.aspectj.weaver.internal.tools.TypePatternMatcherImpl;
  29. import org.aspectj.weaver.patterns.AndPointcut;
  30. import org.aspectj.weaver.patterns.CflowPointcut;
  31. import org.aspectj.weaver.patterns.FormalBinding;
  32. import org.aspectj.weaver.patterns.IScope;
  33. import org.aspectj.weaver.patterns.KindedPointcut;
  34. import org.aspectj.weaver.patterns.NotPointcut;
  35. import org.aspectj.weaver.patterns.OrPointcut;
  36. import org.aspectj.weaver.patterns.ParserException;
  37. import org.aspectj.weaver.patterns.PatternParser;
  38. import org.aspectj.weaver.patterns.Pointcut;
  39. import org.aspectj.weaver.patterns.SimpleScope;
  40. import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
  41. import org.aspectj.weaver.patterns.ThisOrTargetPointcut;
  42. import org.aspectj.weaver.patterns.TypePattern;
  43. import org.aspectj.weaver.reflect.PointcutParameterImpl;
  44. import org.aspectj.weaver.reflect.ReflectionWorld;
  45. /**
  46. * A PointcutParser can be used to build PointcutExpressions for a user-defined subset of AspectJ's pointcut language
  47. */
  48. public class StandardPointcutParser {
  49. private World world;
  50. private final Set<PointcutPrimitive> supportedPrimitives;
  51. private final Set<PointcutDesignatorHandler> pointcutDesignators = new HashSet<>();
  52. /**
  53. * @return a Set containing every PointcutPrimitive except if, cflow, and cflowbelow (useful for passing to PointcutParser
  54. * constructor).
  55. */
  56. public static Set<PointcutPrimitive> getAllSupportedPointcutPrimitives() {
  57. Set<PointcutPrimitive> primitives = new HashSet<>();
  58. primitives.add(PointcutPrimitive.ADVICE_EXECUTION);
  59. primitives.add(PointcutPrimitive.ARGS);
  60. primitives.add(PointcutPrimitive.CALL);
  61. primitives.add(PointcutPrimitive.EXECUTION);
  62. primitives.add(PointcutPrimitive.GET);
  63. primitives.add(PointcutPrimitive.HANDLER);
  64. primitives.add(PointcutPrimitive.INITIALIZATION);
  65. primitives.add(PointcutPrimitive.PRE_INITIALIZATION);
  66. primitives.add(PointcutPrimitive.SET);
  67. primitives.add(PointcutPrimitive.STATIC_INITIALIZATION);
  68. primitives.add(PointcutPrimitive.TARGET);
  69. primitives.add(PointcutPrimitive.THIS);
  70. primitives.add(PointcutPrimitive.WITHIN);
  71. primitives.add(PointcutPrimitive.WITHIN_CODE);
  72. primitives.add(PointcutPrimitive.AT_ANNOTATION);
  73. primitives.add(PointcutPrimitive.AT_THIS);
  74. primitives.add(PointcutPrimitive.AT_TARGET);
  75. primitives.add(PointcutPrimitive.AT_ARGS);
  76. primitives.add(PointcutPrimitive.AT_WITHIN);
  77. primitives.add(PointcutPrimitive.AT_WITHINCODE);
  78. primitives.add(PointcutPrimitive.REFERENCE);
  79. return primitives;
  80. }
  81. /**
  82. * Returns a pointcut parser that can parse the full AspectJ pointcut language with the following exceptions:
  83. * <ul>
  84. * <li>The <code>if, cflow, and cflowbelow</code> pointcut designators are not supported
  85. * <li>Pointcut expressions must be self-contained :- they cannot contain references to other named pointcuts
  86. * <li>The pointcut expression must be anonymous with no formals allowed.
  87. * </ul>
  88. * <p>
  89. * When resolving types in pointcut expressions, the context classloader is used to find types.
  90. * </p>
  91. */
  92. public static StandardPointcutParser getPointcutParserSupportingAllPrimitives(World world) {
  93. StandardPointcutParser p = new StandardPointcutParser(world);
  94. return p;
  95. }
  96. /**
  97. * Returns a pointcut parser that can parse pointcut expressions built from a user-defined subset of AspectJ's supported
  98. * pointcut primitives. The following restrictions apply:
  99. * <ul>
  100. * <li>The <code>if, cflow, and cflowbelow</code> pointcut designators are not supported
  101. * <li>Pointcut expressions must be self-contained :- they cannot contain references to other named pointcuts
  102. * <li>The pointcut expression must be anonymous with no formals allowed.
  103. * </ul>
  104. * <p>
  105. * When resolving types in pointcut expressions, the given classloader is used to find types.
  106. * </p>
  107. *
  108. * @param supportedPointcutKinds a set of PointcutPrimitives this parser should support
  109. * @throws UnsupportedOperationException if the set contains if, cflow, or cflow below
  110. */
  111. public static StandardPointcutParser getPointcutParserSupportingSpecifiedPrimitives(Set supportedPointcutKinds, World world) {
  112. StandardPointcutParser p = new StandardPointcutParser(supportedPointcutKinds, world);
  113. return p;
  114. }
  115. /**
  116. * Create a pointcut parser that can parse the full AspectJ pointcut language with the following exceptions:
  117. * <ul>
  118. * <li>The <code>if, cflow, and cflowbelow</code> pointcut designators are not supported
  119. * <li>Pointcut expressions must be self-contained :- they cannot contain references to other named pointcuts
  120. * <li>The pointcut expression must be anonymous with no formals allowed.
  121. * </ul>
  122. */
  123. protected StandardPointcutParser(World world) {
  124. supportedPrimitives = getAllSupportedPointcutPrimitives();
  125. this.world = world;
  126. }
  127. /**
  128. * Create a pointcut parser that can parse pointcut expressions built from a user-defined subset of AspectJ's supported pointcut
  129. * primitives. The following restrictions apply:
  130. * <ul>
  131. * <li>The <code>if, cflow, and cflowbelow</code> pointcut designators are not supported
  132. * <li>Pointcut expressions must be self-contained :- they cannot contain references to other named pointcuts
  133. * <li>The pointcut expression must be anonymous with no formals allowed.
  134. * </ul>
  135. *
  136. * @param supportedPointcutKinds a set of PointcutPrimitives this parser should support
  137. * @throws UnsupportedOperationException if the set contains if, cflow, or cflow below
  138. */
  139. private StandardPointcutParser(Set<PointcutPrimitive> supportedPointcutKinds, World world) {
  140. supportedPrimitives = supportedPointcutKinds;
  141. for (PointcutPrimitive element : supportedPointcutKinds) {
  142. if ((element == PointcutPrimitive.IF) || (element == PointcutPrimitive.CFLOW)
  143. || (element == PointcutPrimitive.CFLOW_BELOW)) {
  144. throw new UnsupportedOperationException("Cannot handle if, cflow, and cflowbelow primitives");
  145. }
  146. }
  147. this.world = world;
  148. }
  149. // /**
  150. // * Set the lint properties for this parser from the given resource on the classpath.
  151. // *
  152. // * @param resourcePath path to a file containing aspectj lint properties
  153. // */
  154. // public void setLintProperties(String resourcePath) throws IOException {
  155. // URL url = this.classLoaderReference.getClassLoader().getResource(resourcePath);
  156. // InputStream is = url.openStream();
  157. // Properties p = new Properties();
  158. // p.load(is);
  159. // setLintProperties(p);
  160. // }
  161. /**
  162. * Set the lint properties for this parser from the given properties set.
  163. *
  164. * @param properties
  165. */
  166. public void setLintProperties(Properties properties) {
  167. getWorld().getLint().setFromProperties(properties);
  168. }
  169. /**
  170. * Register a new pointcut designator handler with this parser. This provides an extension mechansim for the integration of
  171. * domain-specific pointcut designators with the AspectJ pointcut language.
  172. *
  173. * @param designatorHandler
  174. */
  175. public void registerPointcutDesignatorHandler(PointcutDesignatorHandler designatorHandler) {
  176. this.pointcutDesignators.add(designatorHandler);
  177. if (world != null) {
  178. world.registerPointcutHandler(designatorHandler);
  179. }
  180. }
  181. /**
  182. * Create a pointcut parameter of the given name and type.
  183. *
  184. * @param name
  185. * @param type
  186. * @return
  187. */
  188. public PointcutParameter createPointcutParameter(String name, Class type) {
  189. return new PointcutParameterImpl(name, type);
  190. }
  191. /**
  192. * Parse the given pointcut expression. A global scope is assumed for resolving any type references, and the pointcut must
  193. * contain no formals (variables to be bound).
  194. *
  195. * @throws UnsupportedPointcutPrimitiveException if the parser encounters a primitive pointcut expression of a kind not
  196. * supported by this PointcutParser.
  197. * @throws IllegalArgumentException if the expression is not a well-formed pointcut expression
  198. */
  199. public StandardPointcutExpression parsePointcutExpression(String expression) throws UnsupportedPointcutPrimitiveException,
  200. IllegalArgumentException {
  201. return parsePointcutExpression(expression, null, new PointcutParameter[0]);
  202. }
  203. /**
  204. * Parse the given pointcut expression. The pointcut is resolved as if it had been declared inside the inScope class (this
  205. * allows the pointcut to contain unqualified references to other pointcuts declared in the same type for example). The pointcut
  206. * may contain zero or more formal parameters to be bound at matched join points.
  207. *
  208. * @throws UnsupportedPointcutPrimitiveException if the parser encounters a primitive pointcut expression of a kind not
  209. * supported by this PointcutParser.
  210. * @throws IllegalArgumentException if the expression is not a well-formed pointcut expression
  211. */
  212. public StandardPointcutExpression parsePointcutExpression(String expression, Class inScope, PointcutParameter[] formalParameters)
  213. throws UnsupportedPointcutPrimitiveException, IllegalArgumentException {
  214. StandardPointcutExpressionImpl pcExpr = null;
  215. try {
  216. Pointcut pc = resolvePointcutExpression(expression, inScope, formalParameters);
  217. pc = concretizePointcutExpression(pc, inScope, formalParameters);
  218. validateAgainstSupportedPrimitives(pc, expression); // again, because we have now followed any ref'd pcuts
  219. pcExpr = new StandardPointcutExpressionImpl(pc, expression, formalParameters, getWorld());
  220. } catch (ParserException pEx) {
  221. throw new IllegalArgumentException(buildUserMessageFromParserException(expression, pEx));
  222. } catch (ReflectionWorld.ReflectionWorldException rwEx) {
  223. rwEx.printStackTrace();
  224. throw new IllegalArgumentException(rwEx.getMessage());
  225. }
  226. return pcExpr;
  227. }
  228. protected Pointcut resolvePointcutExpression(String expression, Class<?> inScope, PointcutParameter[] formalParameters) {
  229. try {
  230. PatternParser parser = new PatternParser(expression);
  231. parser.setPointcutDesignatorHandlers(pointcutDesignators, world);
  232. Pointcut pc = parser.parsePointcut();
  233. validateAgainstSupportedPrimitives(pc, expression);
  234. IScope resolutionScope = buildResolutionScope((inScope == null ? Object.class : inScope), formalParameters);
  235. pc = pc.resolve(resolutionScope);
  236. return pc;
  237. } catch (ParserException pEx) {
  238. throw new IllegalArgumentException(buildUserMessageFromParserException(expression, pEx));
  239. }
  240. }
  241. protected Pointcut concretizePointcutExpression(Pointcut pc, Class<?> inScope, PointcutParameter[] formalParameters) {
  242. ResolvedType declaringTypeForResolution = null;
  243. if (inScope != null) {
  244. declaringTypeForResolution = getWorld().resolve(inScope.getName());
  245. } else {
  246. declaringTypeForResolution = ResolvedType.OBJECT.resolve(getWorld());
  247. }
  248. IntMap arity = new IntMap(formalParameters.length);
  249. for (int i = 0; i < formalParameters.length; i++) {
  250. arity.put(i, i);
  251. }
  252. return pc.concretize(declaringTypeForResolution, declaringTypeForResolution, arity);
  253. }
  254. /**
  255. * Parse the given aspectj type pattern, and return a matcher that can be used to match types using it.
  256. *
  257. * @param typePattern an aspectj type pattern
  258. * @return a type pattern matcher that matches using the given pattern
  259. * @throws IllegalArgumentException if the type pattern cannot be successfully parsed.
  260. */
  261. public TypePatternMatcher parseTypePattern(String typePattern) throws IllegalArgumentException {
  262. try {
  263. TypePattern tp = new PatternParser(typePattern).parseTypePattern();
  264. tp.resolve(world);
  265. return new TypePatternMatcherImpl(tp, world);
  266. } catch (ParserException pEx) {
  267. throw new IllegalArgumentException(buildUserMessageFromParserException(typePattern, pEx));
  268. } catch (ReflectionWorld.ReflectionWorldException rwEx) {
  269. throw new IllegalArgumentException(rwEx.getMessage());
  270. }
  271. }
  272. private World getWorld() {
  273. return world;
  274. }
  275. /* for testing */
  276. Set getSupportedPrimitives() {
  277. return supportedPrimitives;
  278. }
  279. /* for testing */
  280. IMessageHandler setCustomMessageHandler(IMessageHandler aHandler) {
  281. IMessageHandler current = getWorld().getMessageHandler();
  282. getWorld().setMessageHandler(aHandler);
  283. return current;
  284. }
  285. private IScope buildResolutionScope(Class<?> inScope, PointcutParameter[] formalParameters) {
  286. if (formalParameters == null) {
  287. formalParameters = new PointcutParameter[0];
  288. }
  289. FormalBinding[] formalBindings = new FormalBinding[formalParameters.length];
  290. for (int i = 0; i < formalBindings.length; i++) {
  291. formalBindings[i] = new FormalBinding(toUnresolvedType(formalParameters[i].getType()), formalParameters[i].getName(), i);
  292. }
  293. if (inScope == null) {
  294. SimpleScope ss = new SimpleScope(getWorld(), formalBindings);
  295. ss.setImportedPrefixes(new String[] { "java.lang.", "java.util." });
  296. return ss;
  297. } else {
  298. ResolvedType inType = getWorld().resolve(inScope.getName());
  299. ISourceContext sourceContext = new ISourceContext() {
  300. public ISourceLocation makeSourceLocation(IHasPosition position) {
  301. return new SourceLocation(new File(""), 0);
  302. }
  303. public ISourceLocation makeSourceLocation(int line, int offset) {
  304. return new SourceLocation(new File(""), line);
  305. }
  306. public int getOffset() {
  307. return 0;
  308. }
  309. public void tidy() {
  310. }
  311. };
  312. BindingScope bScope = new BindingScope(inType, sourceContext, formalBindings);
  313. bScope.setImportedPrefixes(new String[] { "java.lang.", "java.util." });
  314. return bScope;
  315. }
  316. }
  317. private UnresolvedType toUnresolvedType(Class<?> clazz) {
  318. if (clazz.isArray()) {
  319. return UnresolvedType.forSignature(clazz.getName().replace('.', '/'));
  320. } else {
  321. return UnresolvedType.forName(clazz.getName());
  322. }
  323. }
  324. private void validateAgainstSupportedPrimitives(Pointcut pc, String expression) {
  325. switch (pc.getPointcutKind()) {
  326. case Pointcut.AND:
  327. validateAgainstSupportedPrimitives(((AndPointcut) pc).getLeft(), expression);
  328. validateAgainstSupportedPrimitives(((AndPointcut) pc).getRight(), expression);
  329. break;
  330. case Pointcut.ARGS:
  331. if (!supportedPrimitives.contains(PointcutPrimitive.ARGS)) {
  332. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.ARGS);
  333. }
  334. break;
  335. case Pointcut.CFLOW:
  336. CflowPointcut cfp = (CflowPointcut) pc;
  337. if (cfp.isCflowBelow()) {
  338. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.CFLOW_BELOW);
  339. } else {
  340. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.CFLOW);
  341. }
  342. case Pointcut.HANDLER:
  343. if (!supportedPrimitives.contains(PointcutPrimitive.HANDLER)) {
  344. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.HANDLER);
  345. }
  346. break;
  347. case Pointcut.IF:
  348. case Pointcut.IF_FALSE:
  349. case Pointcut.IF_TRUE:
  350. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.IF);
  351. case Pointcut.KINDED:
  352. validateKindedPointcut(((KindedPointcut) pc), expression);
  353. break;
  354. case Pointcut.NOT:
  355. validateAgainstSupportedPrimitives(((NotPointcut) pc).getNegatedPointcut(), expression);
  356. break;
  357. case Pointcut.OR:
  358. validateAgainstSupportedPrimitives(((OrPointcut) pc).getLeft(), expression);
  359. validateAgainstSupportedPrimitives(((OrPointcut) pc).getRight(), expression);
  360. break;
  361. case Pointcut.THIS_OR_TARGET:
  362. boolean isThis = ((ThisOrTargetPointcut) pc).isThis();
  363. if (isThis && !supportedPrimitives.contains(PointcutPrimitive.THIS)) {
  364. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.THIS);
  365. } else if (!supportedPrimitives.contains(PointcutPrimitive.TARGET)) {
  366. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.TARGET);
  367. }
  368. break;
  369. case Pointcut.WITHIN:
  370. if (!supportedPrimitives.contains(PointcutPrimitive.WITHIN)) {
  371. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.WITHIN);
  372. }
  373. break;
  374. case Pointcut.WITHINCODE:
  375. if (!supportedPrimitives.contains(PointcutPrimitive.WITHIN_CODE)) {
  376. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.WITHIN_CODE);
  377. }
  378. break;
  379. case Pointcut.ATTHIS_OR_TARGET:
  380. isThis = ((ThisOrTargetAnnotationPointcut) pc).isThis();
  381. if (isThis && !supportedPrimitives.contains(PointcutPrimitive.AT_THIS)) {
  382. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.AT_THIS);
  383. } else if (!supportedPrimitives.contains(PointcutPrimitive.AT_TARGET)) {
  384. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.AT_TARGET);
  385. }
  386. break;
  387. case Pointcut.ATARGS:
  388. if (!supportedPrimitives.contains(PointcutPrimitive.AT_ARGS)) {
  389. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.AT_ARGS);
  390. }
  391. break;
  392. case Pointcut.ANNOTATION:
  393. if (!supportedPrimitives.contains(PointcutPrimitive.AT_ANNOTATION)) {
  394. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.AT_ANNOTATION);
  395. }
  396. break;
  397. case Pointcut.ATWITHIN:
  398. if (!supportedPrimitives.contains(PointcutPrimitive.AT_WITHIN)) {
  399. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.AT_WITHIN);
  400. }
  401. break;
  402. case Pointcut.ATWITHINCODE:
  403. if (!supportedPrimitives.contains(PointcutPrimitive.AT_WITHINCODE)) {
  404. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.AT_WITHINCODE);
  405. }
  406. break;
  407. case Pointcut.REFERENCE:
  408. if (!supportedPrimitives.contains(PointcutPrimitive.REFERENCE)) {
  409. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.REFERENCE);
  410. }
  411. break;
  412. case Pointcut.USER_EXTENSION:
  413. // always ok...
  414. break;
  415. case Pointcut.NONE: // deliberate fall-through
  416. default:
  417. throw new IllegalArgumentException("Unknown pointcut kind: " + pc.getPointcutKind());
  418. }
  419. }
  420. private void validateKindedPointcut(KindedPointcut pc, String expression) {
  421. Shadow.Kind kind = pc.getKind();
  422. if ((kind == Shadow.MethodCall) || (kind == Shadow.ConstructorCall)) {
  423. if (!supportedPrimitives.contains(PointcutPrimitive.CALL)) {
  424. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.CALL);
  425. }
  426. } else if ((kind == Shadow.MethodExecution) || (kind == Shadow.ConstructorExecution)) {
  427. if (!supportedPrimitives.contains(PointcutPrimitive.EXECUTION)) {
  428. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.EXECUTION);
  429. }
  430. } else if (kind == Shadow.AdviceExecution) {
  431. if (!supportedPrimitives.contains(PointcutPrimitive.ADVICE_EXECUTION)) {
  432. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.ADVICE_EXECUTION);
  433. }
  434. } else if (kind == Shadow.FieldGet) {
  435. if (!supportedPrimitives.contains(PointcutPrimitive.GET)) {
  436. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.GET);
  437. }
  438. } else if (kind == Shadow.FieldSet) {
  439. if (!supportedPrimitives.contains(PointcutPrimitive.SET)) {
  440. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.SET);
  441. }
  442. } else if (kind == Shadow.Initialization) {
  443. if (!supportedPrimitives.contains(PointcutPrimitive.INITIALIZATION)) {
  444. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.INITIALIZATION);
  445. }
  446. } else if (kind == Shadow.PreInitialization) {
  447. if (!supportedPrimitives.contains(PointcutPrimitive.PRE_INITIALIZATION)) {
  448. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.PRE_INITIALIZATION);
  449. }
  450. } else if (kind == Shadow.StaticInitialization) {
  451. if (!supportedPrimitives.contains(PointcutPrimitive.STATIC_INITIALIZATION)) {
  452. throw new UnsupportedPointcutPrimitiveException(expression, PointcutPrimitive.STATIC_INITIALIZATION);
  453. }
  454. }
  455. }
  456. private String buildUserMessageFromParserException(String pc, ParserException ex) {
  457. StringBuilder msg = new StringBuilder();
  458. msg.append("Pointcut is not well-formed: expecting '");
  459. msg.append(ex.getMessage());
  460. msg.append("'");
  461. IHasPosition location = ex.getLocation();
  462. msg.append(" at character position ");
  463. msg.append(location.getStart());
  464. msg.append("\n");
  465. msg.append(pc);
  466. msg.append("\n");
  467. for (int i = 0; i < location.getStart(); i++) {
  468. msg.append(" ");
  469. }
  470. for (int j = location.getStart(); j <= location.getEnd(); j++) {
  471. msg.append("^");
  472. }
  473. msg.append("\n");
  474. return msg.toString();
  475. }
  476. }