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.

ThisOrTargetPointcut.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.DataOutputStream;
  14. import java.io.IOException;
  15. import java.lang.reflect.Member;
  16. import java.util.ArrayList;
  17. import java.util.Collections;
  18. import java.util.HashSet;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Set;
  22. import org.aspectj.bridge.IMessage;
  23. import org.aspectj.lang.JoinPoint;
  24. import org.aspectj.util.FuzzyBoolean;
  25. import org.aspectj.weaver.ISourceContext;
  26. import org.aspectj.weaver.IntMap;
  27. import org.aspectj.weaver.ResolvedTypeX;
  28. import org.aspectj.weaver.Shadow;
  29. import org.aspectj.weaver.TypeX;
  30. import org.aspectj.weaver.VersionedDataInputStream;
  31. import org.aspectj.weaver.WeaverMessages;
  32. import org.aspectj.weaver.ast.Literal;
  33. import org.aspectj.weaver.ast.Test;
  34. import org.aspectj.weaver.ast.Var;
  35. //
  36. /**
  37. * Corresponds to target or this pcd.
  38. *
  39. * <p>type is initially a WildTypePattern. If it stays that way, it's a this(Foo)
  40. * type deal.
  41. * however, the resolveBindings method may convert it to a BindingTypePattern,
  42. * in which
  43. * case, it's a this(foo) type deal.
  44. *
  45. * @author Erik Hilsdale
  46. * @author Jim Hugunin
  47. */
  48. public class ThisOrTargetPointcut extends NameBindingPointcut {
  49. private boolean isThis;
  50. private TypePattern type;
  51. private static final Set thisKindSet = new HashSet(Shadow.ALL_SHADOW_KINDS);
  52. private static final Set targetKindSet = new HashSet(Shadow.ALL_SHADOW_KINDS);
  53. static {
  54. for (Iterator iter = Shadow.ALL_SHADOW_KINDS.iterator(); iter.hasNext();) {
  55. Shadow.Kind kind = (Shadow.Kind) iter.next();
  56. if (kind.neverHasThis()) thisKindSet.remove(kind);
  57. if (kind.neverHasTarget()) targetKindSet.remove(kind);
  58. }
  59. }
  60. public ThisOrTargetPointcut(boolean isThis, TypePattern type) {
  61. this.isThis = isThis;
  62. this.type = type;
  63. this.pointcutKind = THIS_OR_TARGET;
  64. }
  65. public boolean isThis() { return isThis; }
  66. public Set couldMatchKinds() {
  67. return isThis ? thisKindSet : targetKindSet;
  68. }
  69. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  70. return FuzzyBoolean.MAYBE;
  71. }
  72. private boolean couldMatch(Shadow shadow) {
  73. return isThis ? shadow.hasThis() : shadow.hasTarget();
  74. }
  75. protected FuzzyBoolean matchInternal(Shadow shadow) {
  76. if (!couldMatch(shadow)) return FuzzyBoolean.NO;
  77. TypeX typeToMatch = isThis ? shadow.getThisType() : shadow.getTargetType();
  78. //if (typeToMatch == ResolvedTypeX.MISSING) return FuzzyBoolean.NO;
  79. return type.matches(typeToMatch.resolve(shadow.getIWorld()), TypePattern.DYNAMIC);
  80. }
  81. public FuzzyBoolean match(JoinPoint jp, JoinPoint.StaticPart encJP) {
  82. Object toMatch = isThis ? jp.getThis() : jp.getTarget();
  83. if (toMatch == null) return FuzzyBoolean.NO;
  84. return type.matches(toMatch.getClass(), TypePattern.DYNAMIC);
  85. }
  86. /* (non-Javadoc)
  87. * @see org.aspectj.weaver.patterns.Pointcut#matchesDynamically(java.lang.Object, java.lang.Object, java.lang.Object[])
  88. */
  89. public boolean matchesDynamically(Object thisObject, Object targetObject,
  90. Object[] args) {
  91. Object toMatch = isThis ? thisObject : targetObject;
  92. if (toMatch == null) return false;
  93. return type.matchesSubtypes(toMatch.getClass());
  94. }
  95. /* (non-Javadoc)
  96. * @see org.aspectj.weaver.patterns.Pointcut#matchesStatically(java.lang.String, java.lang.reflect.Member, java.lang.Class, java.lang.Class, java.lang.reflect.Member)
  97. */
  98. public FuzzyBoolean matchesStatically(String joinpointKind, Member member,
  99. Class thisClass, Class targetClass, Member withinCode) {
  100. Class staticType = isThis ? thisClass : targetClass;
  101. if (joinpointKind.equals(Shadow.StaticInitialization.getName())) {
  102. return FuzzyBoolean.NO; // no this or target at these jps
  103. }
  104. return(((ExactTypePattern)type).willMatchDynamically(staticType));
  105. }
  106. public void write(DataOutputStream s) throws IOException {
  107. s.writeByte(Pointcut.THIS_OR_TARGET);
  108. s.writeBoolean(isThis);
  109. type.write(s);
  110. writeLocation(s);
  111. }
  112. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  113. boolean isThis = s.readBoolean();
  114. TypePattern type = TypePattern.read(s, context);
  115. ThisOrTargetPointcut ret = new ThisOrTargetPointcut(isThis, type);
  116. ret.readLocation(context, s);
  117. return ret;
  118. }
  119. public void resolveBindings(IScope scope, Bindings bindings) {
  120. type = type.resolveBindings(scope, bindings, true, true);
  121. // ??? handle non-formal
  122. }
  123. public void resolveBindingsFromRTTI() {
  124. type = type.resolveBindingsFromRTTI(true,true);
  125. }
  126. public void postRead(ResolvedTypeX enclosingType) {
  127. type.postRead(enclosingType);
  128. }
  129. /* (non-Javadoc)
  130. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingAnnotationTypePatterns()
  131. */
  132. public List getBindingAnnotationTypePatterns() {
  133. return Collections.EMPTY_LIST;
  134. }
  135. /* (non-Javadoc)
  136. * @see org.aspectj.weaver.patterns.NameBindingPointcut#getBindingTypePatterns()
  137. */
  138. public List getBindingTypePatterns() {
  139. if (type instanceof BindingTypePattern) {
  140. List l = new ArrayList();
  141. l.add(type);
  142. return l;
  143. } else return Collections.EMPTY_LIST;
  144. }
  145. public boolean equals(Object other) {
  146. if (!(other instanceof ThisOrTargetPointcut)) return false;
  147. ThisOrTargetPointcut o = (ThisOrTargetPointcut)other;
  148. return o.isThis == this.isThis && o.type.equals(this.type);
  149. }
  150. public int hashCode() {
  151. int result = 17;
  152. result = 37*result + (isThis ? 0 : 1);
  153. result = 37*result + type.hashCode();
  154. return result;
  155. }
  156. public String toString() {
  157. return (isThis ? "this(" : "target(") + type + ")";
  158. }
  159. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  160. if (!couldMatch(shadow)) return Literal.FALSE;
  161. if (type == TypePattern.ANY) return Literal.TRUE;
  162. Var var = isThis ? shadow.getThisVar() : shadow.getTargetVar();
  163. if (type instanceof BindingTypePattern) {
  164. BindingTypePattern btp = (BindingTypePattern)type;
  165. // Check if we have already bound something to this formal
  166. if ((state.get(btp.getFormalIndex())!=null) && (lastMatchedShadowId != shadow.shadowId)){
  167. // ISourceLocation pcdSloc = getSourceLocation();
  168. // ISourceLocation shadowSloc = shadow.getSourceLocation();
  169. // Message errorMessage = new Message(
  170. // "Cannot use "+(isThis?"this()":"target()")+" to match at this location and bind a formal to type '"+var.getType()+
  171. // "' - the formal is already bound to type '"+state.get(btp.getFormalIndex()).getType()+"'"+
  172. // ". The secondary source location points to the problematic "+(isThis?"this()":"target()")+".",
  173. // shadowSloc,true,new ISourceLocation[]{pcdSloc});
  174. // shadow.getIWorld().getMessageHandler().handleMessage(errorMessage);
  175. state.setErroneousVar(btp.getFormalIndex());
  176. //return null;
  177. }
  178. }
  179. return exposeStateForVar(var, type, state, shadow.getIWorld());
  180. }
  181. public Pointcut concretize1(ResolvedTypeX inAspect, IntMap bindings) {
  182. if (isDeclare(bindings.getEnclosingAdvice())) {
  183. // Enforce rule about which designators are supported in declare
  184. inAspect.getWorld().showMessage(IMessage.ERROR,
  185. WeaverMessages.format(WeaverMessages.THIS_OR_TARGET_IN_DECLARE,isThis?"this":"target"),
  186. bindings.getEnclosingAdvice().getSourceLocation(), null);
  187. return Pointcut.makeMatchesNothing(Pointcut.CONCRETE);
  188. }
  189. TypePattern newType = type.remapAdviceFormals(bindings);
  190. if (inAspect.crosscuttingMembers != null) {
  191. inAspect.crosscuttingMembers.exposeType(newType.getExactType());
  192. }
  193. Pointcut ret = new ThisOrTargetPointcut(isThis, newType);
  194. ret.copyLocationFrom(this);
  195. return ret;
  196. }
  197. }