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.0KB

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