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.

ShadowMatchImpl.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* *******************************************************************
  2. * Copyright (c) 2005 Contributors.
  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. * Adrian Colyer Initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.reflect;
  13. import java.lang.reflect.Member;
  14. import org.aspectj.util.FuzzyBoolean;
  15. import org.aspectj.weaver.ResolvedType;
  16. import org.aspectj.weaver.World;
  17. import org.aspectj.weaver.ast.And;
  18. import org.aspectj.weaver.ast.Call;
  19. import org.aspectj.weaver.ast.FieldGetCall;
  20. import org.aspectj.weaver.ast.HasAnnotation;
  21. import org.aspectj.weaver.ast.ITestVisitor;
  22. import org.aspectj.weaver.ast.Instanceof;
  23. import org.aspectj.weaver.ast.Literal;
  24. import org.aspectj.weaver.ast.Not;
  25. import org.aspectj.weaver.ast.Or;
  26. import org.aspectj.weaver.ast.Test;
  27. import org.aspectj.weaver.ast.Var;
  28. import org.aspectj.weaver.internal.tools.MatchingContextBasedTest;
  29. import org.aspectj.weaver.patterns.ExposedState;
  30. import org.aspectj.weaver.tools.DefaultMatchingContext;
  31. import org.aspectj.weaver.tools.JoinPointMatch;
  32. import org.aspectj.weaver.tools.MatchingContext;
  33. import org.aspectj.weaver.tools.PointcutParameter;
  34. import org.aspectj.weaver.tools.ShadowMatch;
  35. /**
  36. * @author colyer Implementation of ShadowMatch for reflection based worlds.
  37. */
  38. public class ShadowMatchImpl implements ShadowMatch {
  39. private FuzzyBoolean match;
  40. private ExposedState state;
  41. private Test residualTest;
  42. private PointcutParameter[] params;
  43. private Member withinCode;
  44. private Member subject;
  45. private Class<?> withinType;
  46. private MatchingContext matchContext = new DefaultMatchingContext();
  47. public ShadowMatchImpl(FuzzyBoolean match, Test test, ExposedState state, PointcutParameter[] params) {
  48. this.match = match;
  49. this.residualTest = test;
  50. this.state = state;
  51. this.params = params;
  52. }
  53. public void setWithinCode(Member aMember) {
  54. this.withinCode = aMember;
  55. }
  56. public void setSubject(Member aMember) {
  57. this.subject = aMember;
  58. }
  59. public void setWithinType(Class<?> aClass) {
  60. this.withinType = aClass;
  61. }
  62. public boolean alwaysMatches() {
  63. return match.alwaysTrue();
  64. }
  65. public boolean maybeMatches() {
  66. return match.maybeTrue();
  67. }
  68. public boolean neverMatches() {
  69. return match.alwaysFalse();
  70. }
  71. public JoinPointMatch matchesJoinPoint(Object thisObject, Object targetObject, Object[] args) {
  72. if (neverMatches()) {
  73. return JoinPointMatchImpl.NO_MATCH;
  74. }
  75. if (new RuntimeTestEvaluator(residualTest, thisObject, targetObject, args, this.matchContext).matches()) {
  76. return new JoinPointMatchImpl(getPointcutParameters(thisObject, targetObject, args));
  77. } else {
  78. return JoinPointMatchImpl.NO_MATCH;
  79. }
  80. }
  81. /*
  82. * (non-Javadoc)
  83. *
  84. * @see org.aspectj.weaver.tools.ShadowMatch#setMatchingContext(org.aspectj.weaver.tools.MatchingContext)
  85. */
  86. public void setMatchingContext(MatchingContext aMatchContext) {
  87. this.matchContext = aMatchContext;
  88. }
  89. private PointcutParameter[] getPointcutParameters(Object thisObject, Object targetObject, Object[] args) {
  90. Var[] vars = state.vars;
  91. PointcutParameterImpl[] bindings = new PointcutParameterImpl[params.length];
  92. for (int i = 0; i < bindings.length; i++) {
  93. bindings[i] = new PointcutParameterImpl(params[i].getName(), params[i].getType());
  94. bindings[i].setBinding(((ReflectionVar) vars[i]).getBindingAtJoinPoint(thisObject, targetObject, args, subject,
  95. withinCode, withinType));
  96. }
  97. return bindings;
  98. }
  99. private static class RuntimeTestEvaluator implements ITestVisitor {
  100. private boolean matches = true;
  101. private final Test test;
  102. private final Object thisObject;
  103. private final Object targetObject;
  104. private final Object[] args;
  105. private final MatchingContext matchContext;
  106. public RuntimeTestEvaluator(Test aTest, Object thisObject, Object targetObject, Object[] args, MatchingContext context) {
  107. this.test = aTest;
  108. this.thisObject = thisObject;
  109. this.targetObject = targetObject;
  110. this.args = args;
  111. this.matchContext = context;
  112. }
  113. public boolean matches() {
  114. test.accept(this);
  115. return matches;
  116. }
  117. public void visit(And e) {
  118. boolean leftMatches = new RuntimeTestEvaluator(e.getLeft(), thisObject, targetObject, args, matchContext).matches();
  119. if (!leftMatches) {
  120. matches = false;
  121. } else {
  122. matches = new RuntimeTestEvaluator(e.getRight(), thisObject, targetObject, args, matchContext).matches();
  123. }
  124. }
  125. public void visit(Instanceof instanceofTest) {
  126. ReflectionVar v = (ReflectionVar) instanceofTest.getVar();
  127. Object value = v.getBindingAtJoinPoint(thisObject, targetObject, args);
  128. World world = v.getType().getWorld();
  129. ResolvedType desiredType = instanceofTest.getType().resolve(world);
  130. if (value == null) {
  131. matches = false;
  132. } else {
  133. ResolvedType actualType = world.resolve(value.getClass().getName());
  134. matches = desiredType.isAssignableFrom(actualType);
  135. }
  136. }
  137. public void visit(MatchingContextBasedTest matchingContextTest) {
  138. matches = matchingContextTest.matches(this.matchContext);
  139. }
  140. public void visit(Not not) {
  141. matches = !new RuntimeTestEvaluator(not.getBody(), thisObject, targetObject, args, matchContext).matches();
  142. }
  143. public void visit(Or or) {
  144. boolean leftMatches = new RuntimeTestEvaluator(or.getLeft(), thisObject, targetObject, args, matchContext).matches();
  145. if (leftMatches) {
  146. matches = true;
  147. } else {
  148. matches = new RuntimeTestEvaluator(or.getRight(), thisObject, targetObject, args, matchContext).matches();
  149. }
  150. }
  151. public void visit(Literal literal) {
  152. if (literal == Literal.FALSE) {
  153. matches = false;
  154. } else {
  155. matches = true;
  156. }
  157. }
  158. public void visit(Call call) {
  159. throw new UnsupportedOperationException("Can't evaluate call test at runtime");
  160. }
  161. public void visit(FieldGetCall fieldGetCall) {
  162. throw new UnsupportedOperationException("Can't evaluate fieldGetCall test at runtime");
  163. }
  164. public void visit(HasAnnotation hasAnnotation) {
  165. ReflectionVar v = (ReflectionVar) hasAnnotation.getVar();
  166. Object value = v.getBindingAtJoinPoint(thisObject, targetObject, args);
  167. World world = v.getType().getWorld();
  168. ResolvedType actualVarType = world.resolve(value.getClass().getName());
  169. ResolvedType requiredAnnotationType = hasAnnotation.getAnnotationType().resolve(world);
  170. matches = actualVarType.hasAnnotation(requiredAnnotationType);
  171. }
  172. }
  173. }