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.

WithinPointcut.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.Set;
  17. import org.aspectj.bridge.IMessage;
  18. import org.aspectj.bridge.ISourceLocation;
  19. import org.aspectj.bridge.Message;
  20. import org.aspectj.lang.JoinPoint;
  21. import org.aspectj.util.FuzzyBoolean;
  22. import org.aspectj.weaver.ISourceContext;
  23. import org.aspectj.weaver.IntMap;
  24. import org.aspectj.weaver.ResolvedTypeX;
  25. import org.aspectj.weaver.Shadow;
  26. import org.aspectj.weaver.VersionedDataInputStream;
  27. import org.aspectj.weaver.WeaverMessages;
  28. import org.aspectj.weaver.ast.Literal;
  29. import org.aspectj.weaver.ast.Test;
  30. public class WithinPointcut extends Pointcut {
  31. TypePattern typePattern;
  32. public WithinPointcut(TypePattern type) {
  33. this.typePattern = type;
  34. this.pointcutKind = WITHIN;
  35. }
  36. private FuzzyBoolean isWithinType(ResolvedTypeX type) {
  37. while (type != null) {
  38. if (typePattern.matchesStatically(type)) {
  39. return FuzzyBoolean.YES;
  40. }
  41. type = type.getDeclaringType();
  42. }
  43. return FuzzyBoolean.NO;
  44. }
  45. public Set couldMatchKinds() {
  46. return Shadow.ALL_SHADOW_KINDS;
  47. }
  48. public FuzzyBoolean fastMatch(FastMatchInfo info) {
  49. if (typePattern.annotationPattern instanceof AnyAnnotationTypePattern) {
  50. return isWithinType(info.getType());
  51. }
  52. return FuzzyBoolean.MAYBE;
  53. }
  54. protected FuzzyBoolean matchInternal(Shadow shadow) {
  55. ResolvedTypeX enclosingType = shadow.getIWorld().resolve(shadow.getEnclosingType(),true);
  56. if (enclosingType == ResolvedTypeX.MISSING) {
  57. IMessage msg = new Message(
  58. WeaverMessages.format(WeaverMessages.CANT_FIND_TYPE_WITHINPCD,
  59. shadow.getEnclosingType().getName()),
  60. shadow.getSourceLocation(),true,new ISourceLocation[]{getSourceLocation()});
  61. shadow.getIWorld().getMessageHandler().handleMessage(msg);
  62. }
  63. typePattern.resolve(shadow.getIWorld());
  64. return isWithinType(enclosingType);
  65. }
  66. public FuzzyBoolean match(JoinPoint jp, JoinPoint.StaticPart encJp) {
  67. return isWithinType(encJp.getSignature().getDeclaringType());
  68. }
  69. /* (non-Javadoc)
  70. * @see org.aspectj.weaver.patterns.Pointcut#matchesDynamically(java.lang.Object, java.lang.Object, java.lang.Object[])
  71. */
  72. public boolean matchesDynamically(Object thisObject, Object targetObject,
  73. Object[] args) {
  74. return true;
  75. }
  76. /* (non-Javadoc)
  77. * @see org.aspectj.weaver.patterns.Pointcut#matchesStatically(java.lang.String, java.lang.reflect.Member, java.lang.Class, java.lang.Class, java.lang.reflect.Member)
  78. */
  79. public FuzzyBoolean matchesStatically(
  80. String joinpointKind, Member member, Class thisClass,
  81. Class targetClass, Member withinCode) {
  82. if ((member != null) &&
  83. !(joinpointKind.equals(Shadow.ConstructorCall.getName()) ||
  84. joinpointKind.equals(Shadow.MethodCall.getName()) ||
  85. joinpointKind.equals(Shadow.FieldGet.getName()) ||
  86. joinpointKind.equals(Shadow.FieldSet.getName()))
  87. ) {
  88. return isWithinType(member.getDeclaringClass());
  89. } else {
  90. return isWithinType(thisClass);
  91. }
  92. }
  93. private FuzzyBoolean isWithinType(Class type) {
  94. while (type != null) {
  95. if (typePattern.matchesStatically(type)) {
  96. return FuzzyBoolean.YES;
  97. }
  98. type = type.getDeclaringClass();
  99. }
  100. return FuzzyBoolean.NO;
  101. }
  102. public void write(DataOutputStream s) throws IOException {
  103. s.writeByte(Pointcut.WITHIN);
  104. typePattern.write(s);
  105. writeLocation(s);
  106. }
  107. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  108. TypePattern type = TypePattern.read(s, context);
  109. WithinPointcut ret = new WithinPointcut(type);
  110. ret.readLocation(context, s);
  111. return ret;
  112. }
  113. public void resolveBindings(IScope scope, Bindings bindings) {
  114. typePattern = typePattern.resolveBindings(scope, bindings, false, false);
  115. }
  116. public void resolveBindingsFromRTTI() {
  117. typePattern = typePattern.resolveBindingsFromRTTI(false,false);
  118. }
  119. public void postRead(ResolvedTypeX enclosingType) {
  120. typePattern.postRead(enclosingType);
  121. }
  122. public boolean couldEverMatchSameJoinPointsAs(WithinPointcut other) {
  123. return typePattern.couldEverMatchSameTypesAs(other.typePattern);
  124. }
  125. public boolean equals(Object other) {
  126. if (!(other instanceof WithinPointcut)) return false;
  127. WithinPointcut o = (WithinPointcut)other;
  128. return o.typePattern.equals(this.typePattern);
  129. }
  130. public int hashCode() {
  131. int result = 43;
  132. result = 37*result + typePattern.hashCode();
  133. return result;
  134. }
  135. public String toString() {
  136. return "within(" + typePattern + ")";
  137. }
  138. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  139. return match(shadow).alwaysTrue() ? Literal.TRUE : Literal.FALSE;
  140. }
  141. public Pointcut concretize1(ResolvedTypeX inAspect, IntMap bindings) {
  142. Pointcut ret = new WithinPointcut(typePattern);
  143. ret.copyLocationFrom(this);
  144. return ret;
  145. }
  146. }