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.

IfPointcut.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * Alexandre Vasseur if() implementation for @AJ style
  12. * ******************************************************************/
  13. package org.aspectj.weaver.patterns;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.Iterator;
  17. import java.util.List;
  18. import java.util.Map;
  19. import org.aspectj.bridge.IMessage;
  20. import org.aspectj.bridge.MessageUtil;
  21. import org.aspectj.util.FuzzyBoolean;
  22. import org.aspectj.weaver.Advice;
  23. import org.aspectj.weaver.AjcMemberMaker;
  24. import org.aspectj.weaver.BCException;
  25. import org.aspectj.weaver.CompressingDataOutputStream;
  26. import org.aspectj.weaver.ISourceContext;
  27. import org.aspectj.weaver.IntMap;
  28. import org.aspectj.weaver.ResolvedMember;
  29. import org.aspectj.weaver.ResolvedMemberImpl;
  30. import org.aspectj.weaver.ResolvedPointcutDefinition;
  31. import org.aspectj.weaver.ResolvedType;
  32. import org.aspectj.weaver.Shadow;
  33. import org.aspectj.weaver.ShadowMunger;
  34. import org.aspectj.weaver.UnresolvedType;
  35. import org.aspectj.weaver.VersionedDataInputStream;
  36. import org.aspectj.weaver.WeaverMessages;
  37. import org.aspectj.weaver.World;
  38. import org.aspectj.weaver.ast.Expr;
  39. import org.aspectj.weaver.ast.Literal;
  40. import org.aspectj.weaver.ast.Test;
  41. import org.aspectj.weaver.ast.Var;
  42. public class IfPointcut extends Pointcut {
  43. public ResolvedMember testMethod;
  44. public int extraParameterFlags;
  45. /**
  46. * A token source dump that looks like a pointcut (but is NOT parseable at all - just here to help debugging etc)
  47. */
  48. private final String enclosingPointcutHint;
  49. public Pointcut residueSource;
  50. int baseArgsCount;
  51. // XXX some way to compute args
  52. public IfPointcut(ResolvedMember testMethod, int extraParameterFlags) {
  53. this.testMethod = testMethod;
  54. this.extraParameterFlags = extraParameterFlags;
  55. this.pointcutKind = IF;
  56. this.enclosingPointcutHint = null;
  57. }
  58. /**
  59. * No-arg constructor for @AJ style, where the if() body is actually the @Pointcut annotated method
  60. */
  61. public IfPointcut(String enclosingPointcutHint) {
  62. this.pointcutKind = IF;
  63. this.enclosingPointcutHint = enclosingPointcutHint;
  64. this.testMethod = null;// resolved during concretize
  65. this.extraParameterFlags = -1;// allows to keep track of the @Aj style
  66. }
  67. @Override
  68. public int couldMatchKinds() {
  69. return Shadow.ALL_SHADOW_KINDS_BITS;
  70. }
  71. @Override
  72. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  73. return FuzzyBoolean.MAYBE;
  74. }
  75. @Override
  76. protected FuzzyBoolean matchInternal(Shadow shadow) {
  77. if (extraParameterFlags != -1) { // make sure it isn't annotation style if relying on these values
  78. if ((extraParameterFlags & Advice.ConstantReference) != 0) {
  79. if ((extraParameterFlags & Advice.ConstantValue) != 0) {
  80. return FuzzyBoolean.YES;
  81. } else {
  82. return FuzzyBoolean.NO;
  83. }
  84. }
  85. }
  86. // ??? this is not maximally efficient
  87. return FuzzyBoolean.MAYBE;
  88. }
  89. public boolean alwaysFalse() {
  90. return false;
  91. }
  92. public boolean alwaysTrue() {
  93. return false;
  94. }
  95. // enh 76055
  96. public Pointcut getResidueSource() {
  97. return residueSource;
  98. }
  99. @Override
  100. public void write(CompressingDataOutputStream s) throws IOException {
  101. s.writeByte(Pointcut.IF);
  102. s.writeBoolean(testMethod != null); // do we have a test method?
  103. if (testMethod != null) {
  104. testMethod.write(s);
  105. }
  106. s.writeByte(extraParameterFlags);
  107. writeLocation(s);
  108. }
  109. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  110. boolean hasTestMethod = s.readBoolean();
  111. ResolvedMember resolvedTestMethod = null;
  112. if (hasTestMethod) { // should always have a test method unless @AJ style
  113. resolvedTestMethod = ResolvedMemberImpl.readResolvedMember(s, context);
  114. }
  115. IfPointcut ret = new IfPointcut(resolvedTestMethod, s.readByte());
  116. ret.readLocation(context, s);
  117. return ret;
  118. }
  119. @Override
  120. public void resolveBindings(IScope scope, Bindings bindings) {
  121. // ??? all we need is good error messages in here in cflow contexts
  122. }
  123. @Override
  124. public boolean equals(Object other) {
  125. if (!(other instanceof IfPointcut)) {
  126. return false;
  127. }
  128. IfPointcut o = (IfPointcut) other;
  129. if (o.testMethod == null) {
  130. return (this.testMethod == null);
  131. }
  132. return o.testMethod.equals(this.testMethod);
  133. }
  134. @Override
  135. public int hashCode() {
  136. int result = 17;
  137. result = 37 * result + testMethod.hashCode();
  138. return result;
  139. }
  140. @Override
  141. public String toString() {
  142. if (extraParameterFlags < 0) {
  143. // @AJ style
  144. return "if()";
  145. } else {
  146. return "if(" + testMethod + ")";// FIXME AV - bad, this makes it unparsable. Perhaps we can use if() for code style
  147. // behind the scene!
  148. }
  149. }
  150. // ??? The implementation of name binding and type checking in if PCDs is very convoluted
  151. // There has to be a better way...
  152. private boolean findingResidue = false;
  153. // Similar to lastMatchedShadowId - but only for if PCDs.
  154. private int ifLastMatchedShadowId;
  155. private Test ifLastMatchedShadowResidue;
  156. /**
  157. * At each shadow that matched, the residue can be different.
  158. */
  159. @Override
  160. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  161. if (findingResidue) {
  162. return Literal.TRUE;
  163. }
  164. findingResidue = true;
  165. try {
  166. // Have we already been asked this question?
  167. if (shadow.shadowId == ifLastMatchedShadowId) {
  168. return ifLastMatchedShadowResidue;
  169. }
  170. Test ret = Literal.TRUE;
  171. List<Var> args = new ArrayList<>();
  172. // code style
  173. if (extraParameterFlags >= 0) {
  174. if ((extraParameterFlags & Advice.ConstantReference) != 0) {
  175. // it is either always true or always false, no need for test
  176. if ((extraParameterFlags & Advice.ConstantValue) != 0) {
  177. ret = Literal.TRUE;
  178. ifLastMatchedShadowId = shadow.shadowId;
  179. ifLastMatchedShadowResidue = ret;
  180. return ret;
  181. } else {
  182. // Dont think we should be in here as the match cannot have succeeded...
  183. ret = Literal.FALSE;
  184. ifLastMatchedShadowId = shadow.shadowId;
  185. ifLastMatchedShadowResidue = ret;
  186. return ret;
  187. }
  188. }
  189. // If there are no args to sort out, don't bother with the recursive call
  190. if (baseArgsCount > 0) {
  191. ExposedState myState = new ExposedState(baseArgsCount);
  192. myState.setConcreteAspect(state.getConcreteAspect());
  193. // ??? we throw out the test that comes from this walk. All we want here
  194. // is bindings for the arguments
  195. residueSource.findResidue(shadow, myState);
  196. UnresolvedType[] pTypes = (testMethod == null ? null : testMethod.getParameterTypes());
  197. if (pTypes != null && baseArgsCount > pTypes.length) { // pr155347
  198. throw new BCException("Unexpected problem with testMethod " + testMethod + ": expecting " + baseArgsCount
  199. + " arguments");
  200. }
  201. // pr118149
  202. // It is possible for vars in myState (which would normally be set
  203. // in the call to residueSource.findResidue) to not be set (be null)
  204. // in an Or pointcut with if expressions in both branches, and where
  205. // one branch is known statically to not match. In this situation we
  206. // simply return Test.
  207. for (int i = 0; i < baseArgsCount; i++) {
  208. Var v = myState.get(i);
  209. if (v == null) {
  210. continue; // pr118149
  211. }
  212. args.add(v);
  213. ret = Test.makeAnd(ret, Test.makeInstanceof(v, pTypes[i].resolve(shadow.getIWorld())));
  214. }
  215. }
  216. // handle thisJoinPoint parameters
  217. if ((extraParameterFlags & Advice.ThisJoinPoint) != 0) {
  218. args.add(shadow.getThisJoinPointVar());
  219. }
  220. if ((extraParameterFlags & Advice.ThisJoinPointStaticPart) != 0) {
  221. args.add(shadow.getThisJoinPointStaticPartVar());
  222. }
  223. if ((extraParameterFlags & Advice.ThisEnclosingJoinPointStaticPart) != 0) {
  224. args.add(shadow.getThisEnclosingJoinPointStaticPartVar());
  225. }
  226. if ((extraParameterFlags & Advice.ThisAspectInstance) != 0) {
  227. args.add(shadow.getThisAspectInstanceVar(state.getConcreteAspect()));
  228. }
  229. } else {
  230. // @style is slightly different
  231. int currentStateIndex = 0;
  232. // FIXME AV - "args(jp)" test(jp, thejp) will fail here
  233. for (int i = 0; i < testMethod.getParameterTypes().length; i++) {
  234. String argSignature = testMethod.getParameterTypes()[i].getSignature();
  235. if (AjcMemberMaker.TYPEX_JOINPOINT.getSignature().equals(argSignature)) {
  236. args.add(shadow.getThisJoinPointVar());
  237. } else if (AjcMemberMaker.TYPEX_PROCEEDINGJOINPOINT.getSignature().equals(argSignature)) {
  238. args.add(shadow.getThisJoinPointVar());
  239. } else if (AjcMemberMaker.TYPEX_STATICJOINPOINT.getSignature().equals(argSignature)) {
  240. args.add(shadow.getThisJoinPointStaticPartVar());
  241. } else if (AjcMemberMaker.TYPEX_ENCLOSINGSTATICJOINPOINT.getSignature().equals(argSignature)) {
  242. args.add(shadow.getThisEnclosingJoinPointStaticPartVar());
  243. } else {
  244. if (state.size() == 0 || currentStateIndex > state.size()) { // if 'we have nothing else to bind from in the state object'
  245. String[] paramNames = testMethod.getParameterNames();
  246. StringBuilder errorParameter = new StringBuilder();
  247. // Support a single special situation: where the if() pointcut takes a parameter bound elsewhere
  248. // in the pointcut but the advice does not bind it. For example:
  249. //
  250. // @Pointcut("this(o) && if()") public static boolean method(Foo f) { return f.isTrue();}
  251. // @Before("method(*)") public void beforeAdvice() {}
  252. // The condition above is effectively saying 'if we have nothing to bind'
  253. if ( (i+1) == testMethod.getParameterTypes().length) { // If there is just one more to bind
  254. // As with code style, recurse just to get the variable binding information
  255. ExposedState myState = new ExposedState(baseArgsCount);
  256. myState.setConcreteAspect(state.getConcreteAspect());
  257. residueSource.findResidue(shadow, myState);
  258. if (myState.size()==1) {
  259. // Treat that as the parameter value the if pointcut needs
  260. Var v = myState.get(0);
  261. args.add(v);
  262. ret = Test.makeAnd(ret,
  263. Test.makeInstanceof(v, testMethod.getParameterTypes()[i].resolve(shadow.getIWorld())));
  264. continue;
  265. }
  266. }
  267. if (paramNames != null) {
  268. errorParameter.append(testMethod.getParameterTypes()[i].getName()).append(" ");
  269. errorParameter.append(paramNames[i]);
  270. shadow.getIWorld()
  271. .getMessageHandler()
  272. .handleMessage(
  273. MessageUtil.error("Missing binding for if() pointcut method. Parameter " + (i + 1)
  274. + "(" + errorParameter.toString()
  275. + ") must be bound - even in reference pointcuts (compiler limitation)",
  276. testMethod.getSourceLocation()));
  277. } else {
  278. shadow.getIWorld()
  279. .getMessageHandler()
  280. .handleMessage(
  281. MessageUtil.error("Missing binding for if() pointcut method. Parameter " + (i + 1)
  282. + " must be bound - even in reference pointcuts (compiler limitation)",
  283. testMethod.getSourceLocation()));
  284. }
  285. return Literal.TRUE; // exit quickly
  286. }
  287. // we don't use i as JoinPoint.* can be anywhere in the signature in @style
  288. Var v = state.get(currentStateIndex++);
  289. while (v == null && currentStateIndex < state.size()) { // pr162135
  290. v = state.get(currentStateIndex++);
  291. }
  292. args.add(v);
  293. ret = Test.makeAnd(ret,
  294. Test.makeInstanceof(v, testMethod.getParameterTypes()[i].resolve(shadow.getIWorld())));
  295. }
  296. }
  297. }
  298. ret = Test.makeAnd(ret, Test.makeCall(testMethod, args.toArray(Expr.NONE)));
  299. // Remember...
  300. ifLastMatchedShadowId = shadow.shadowId;
  301. ifLastMatchedShadowResidue = ret;
  302. return ret;
  303. } finally {
  304. findingResidue = false;
  305. }
  306. }
  307. // amc - the only reason this override seems to be here is to stop the copy, but
  308. // that can be prevented by overriding shouldCopyLocationForConcretization,
  309. // allowing me to make the method final in Pointcut.
  310. // public Pointcut concretize(ResolvedType inAspect, IntMap bindings) {
  311. // return this.concretize1(inAspect, bindings);
  312. // }
  313. @Override
  314. protected boolean shouldCopyLocationForConcretize() {
  315. return false;
  316. }
  317. private IfPointcut partiallyConcretized = null;
  318. @Override
  319. public Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
  320. // System.err.println("concretize: " + this + " already: " + partiallyConcretized);
  321. if (isDeclare(bindings.getEnclosingAdvice())) {
  322. // Enforce rule about which designators are supported in declare
  323. inAspect.getWorld().showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.IF_IN_DECLARE),
  324. bindings.getEnclosingAdvice().getSourceLocation(), null);
  325. return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
  326. }
  327. if (partiallyConcretized != null) {
  328. return partiallyConcretized;
  329. }
  330. final IfPointcut ret;
  331. if (extraParameterFlags < 0 && testMethod == null) {
  332. // @AJ style, we need to find the testMethod in the aspect defining the "if()" enclosing pointcut
  333. ResolvedPointcutDefinition def = bindings.peekEnclosingDefinition();
  334. if (def != null) {
  335. ResolvedType aspect = inAspect.getWorld().resolve(def.getDeclaringType());
  336. for (Iterator<ResolvedMember> memberIter = aspect.getMethods(true, true); memberIter.hasNext();) {
  337. ResolvedMember method = memberIter.next();
  338. if (def.getName().equals(method.getName())
  339. && def.getParameterTypes().length == method.getParameterTypes().length) {
  340. boolean sameSig = true;
  341. for (int j = 0; j < method.getParameterTypes().length; j++) {
  342. UnresolvedType argJ = method.getParameterTypes()[j];
  343. if (!argJ.equals(def.getParameterTypes()[j])) {
  344. sameSig = false;
  345. break;
  346. }
  347. }
  348. if (sameSig) {
  349. testMethod = method;
  350. break;
  351. }
  352. }
  353. }
  354. if (testMethod == null) {
  355. inAspect.getWorld().showMessage(IMessage.ERROR,
  356. "Cannot find if() body from '" + def.toString() + "' for '" + enclosingPointcutHint + "'",
  357. this.getSourceLocation(), null);
  358. return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
  359. }
  360. } else {
  361. testMethod = inAspect.getWorld().resolve(bindings.getAdviceSignature());
  362. }
  363. ret = new IfPointcut(enclosingPointcutHint);
  364. ret.testMethod = testMethod;
  365. } else {
  366. ret = new IfPointcut(testMethod, extraParameterFlags);
  367. }
  368. ret.copyLocationFrom(this);
  369. partiallyConcretized = ret;
  370. // It is possible to directly code your pointcut expression in a per clause
  371. // rather than defining a pointcut declaration and referencing it in your
  372. // per clause. If you do this, we have problems (bug #62458). For now,
  373. // let's police that you are trying to code a pointcut in a per clause and
  374. // put out a compiler error.
  375. if (bindings.directlyInAdvice() && bindings.getEnclosingAdvice() == null) {
  376. // Assumption: if() is in a per clause if we say we are directly in advice
  377. // but we have no enclosing advice.
  378. inAspect.getWorld().showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.IF_IN_PERCLAUSE),
  379. this.getSourceLocation(), null);
  380. return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
  381. }
  382. if (bindings.directlyInAdvice()) {
  383. ShadowMunger advice = bindings.getEnclosingAdvice();
  384. if (advice instanceof Advice) {
  385. ret.baseArgsCount = ((Advice) advice).getBaseParameterCount();
  386. } else {
  387. ret.baseArgsCount = 0;
  388. }
  389. ret.residueSource = advice.getPointcut().concretize(inAspect, inAspect, ret.baseArgsCount, advice);
  390. } else {
  391. ResolvedPointcutDefinition def = bindings.peekEnclosingDefinition();
  392. if (def == CflowPointcut.CFLOW_MARKER) {
  393. inAspect.getWorld().showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.IF_LEXICALLY_IN_CFLOW),
  394. getSourceLocation(), null);
  395. return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
  396. }
  397. ret.baseArgsCount = def.getParameterTypes().length;
  398. // for @style, we have implicit binding for JoinPoint.* things
  399. // FIXME AV - will lead to failure for "args(jp)" test(jp, thejp) / see args() implementation
  400. if (ret.extraParameterFlags < 0) {
  401. ret.baseArgsCount = 0;
  402. for (int i = 0; i < testMethod.getParameterTypes().length; i++) {
  403. String argSignature = testMethod.getParameterTypes()[i].getSignature();
  404. if (AjcMemberMaker.TYPEX_JOINPOINT.getSignature().equals(argSignature)
  405. || AjcMemberMaker.TYPEX_PROCEEDINGJOINPOINT.getSignature().equals(argSignature)
  406. || AjcMemberMaker.TYPEX_STATICJOINPOINT.getSignature().equals(argSignature)
  407. || AjcMemberMaker.TYPEX_ENCLOSINGSTATICJOINPOINT.getSignature().equals(argSignature)) {
  408. } else {
  409. ret.baseArgsCount++;
  410. }
  411. }
  412. }
  413. IntMap newBindings = IntMap.idMap(ret.baseArgsCount);
  414. newBindings.copyContext(bindings);
  415. ret.residueSource = def.getPointcut().concretize(inAspect, declaringType, newBindings);
  416. }
  417. return ret;
  418. }
  419. // we can't touch "if" methods
  420. @Override
  421. public Pointcut parameterizeWith(Map typeVariableMap, World w) {
  422. return this;
  423. }
  424. // public static Pointcut MatchesNothing = new MatchesNothingPointcut();
  425. // ??? there could possibly be some good optimizations to be done at this point
  426. public static IfPointcut makeIfFalsePointcut(State state) {
  427. IfPointcut ret = new IfFalsePointcut();
  428. ret.state = state;
  429. return ret;
  430. }
  431. @Override
  432. public Object accept(PatternNodeVisitor visitor, Object data) {
  433. return visitor.visit(this, data);
  434. }
  435. @Override
  436. public Object traverse(PatternNodeVisitor visitor, Object data) {
  437. Object ret = accept(visitor, data);
  438. if (this.partiallyConcretized != null)
  439. this.partiallyConcretized.traverse(visitor, ret);
  440. if (this.residueSource != null)
  441. this.residueSource.traverse(visitor, ret);
  442. return ret;
  443. }
  444. public static class IfFalsePointcut extends IfPointcut {
  445. public IfFalsePointcut() {
  446. super(null, 0);
  447. this.pointcutKind = Pointcut.IF_FALSE;
  448. }
  449. @Override
  450. public int couldMatchKinds() {
  451. return Shadow.NO_SHADOW_KINDS_BITS;
  452. }
  453. @Override
  454. public boolean alwaysFalse() {
  455. return true;
  456. }
  457. @Override
  458. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  459. return Literal.FALSE; // can only get here if an earlier error occurred
  460. }
  461. @Override
  462. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  463. return FuzzyBoolean.NO;
  464. }
  465. @Override
  466. protected FuzzyBoolean matchInternal(Shadow shadow) {
  467. return FuzzyBoolean.NO;
  468. }
  469. @Override
  470. public void resolveBindings(IScope scope, Bindings bindings) {
  471. }
  472. @Override
  473. public void postRead(ResolvedType enclosingType) {
  474. }
  475. @Override
  476. public Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
  477. if (isDeclare(bindings.getEnclosingAdvice())) {
  478. // Enforce rule about which designators are supported in declare
  479. inAspect.getWorld().showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.IF_IN_DECLARE),
  480. bindings.getEnclosingAdvice().getSourceLocation(), null);
  481. return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
  482. }
  483. return makeIfFalsePointcut(state);
  484. }
  485. @Override
  486. public void write(CompressingDataOutputStream s) throws IOException {
  487. s.writeByte(Pointcut.IF_FALSE);
  488. }
  489. @Override
  490. public int hashCode() {
  491. int result = 17;
  492. return result;
  493. }
  494. @Override
  495. public String toString() {
  496. return "if(false)";
  497. }
  498. }
  499. public static IfPointcut makeIfTruePointcut(State state) {
  500. IfPointcut ret = new IfTruePointcut();
  501. ret.state = state;
  502. return ret;
  503. }
  504. public static class IfTruePointcut extends IfPointcut {
  505. public IfTruePointcut() {
  506. super(null, 0);
  507. this.pointcutKind = Pointcut.IF_TRUE;
  508. }
  509. @Override
  510. public boolean alwaysTrue() {
  511. return true;
  512. }
  513. @Override
  514. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  515. return Literal.TRUE; // can only get here if an earlier error occurred
  516. }
  517. @Override
  518. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  519. return FuzzyBoolean.YES;
  520. }
  521. @Override
  522. protected FuzzyBoolean matchInternal(Shadow shadow) {
  523. return FuzzyBoolean.YES;
  524. }
  525. @Override
  526. public void resolveBindings(IScope scope, Bindings bindings) {
  527. }
  528. @Override
  529. public void postRead(ResolvedType enclosingType) {
  530. }
  531. @Override
  532. public Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
  533. if (isDeclare(bindings.getEnclosingAdvice())) {
  534. // Enforce rule about which designators are supported in declare
  535. inAspect.getWorld().showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.IF_IN_DECLARE),
  536. bindings.getEnclosingAdvice().getSourceLocation(), null);
  537. return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
  538. }
  539. return makeIfTruePointcut(state);
  540. }
  541. @Override
  542. public void write(CompressingDataOutputStream s) throws IOException {
  543. s.writeByte(IF_TRUE);
  544. }
  545. @Override
  546. public int hashCode() {
  547. int result = 37;
  548. return result;
  549. }
  550. @Override
  551. public String toString() {
  552. return "if(true)";
  553. }
  554. }
  555. /**
  556. * Called when it is determined that the pointcut refers to a constant value of TRUE or FALSE - enabling exact matching and no
  557. * unnecessary calls to the method representing the if body.
  558. */
  559. public void setAlways(boolean matches) {
  560. extraParameterFlags |= Advice.ConstantReference;
  561. if (matches) {
  562. extraParameterFlags |= Advice.ConstantValue;
  563. }
  564. }
  565. }