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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.IOException;
  14. import java.util.Map;
  15. import org.aspectj.bridge.ISourceLocation;
  16. import org.aspectj.bridge.MessageUtil;
  17. import org.aspectj.util.FuzzyBoolean;
  18. import org.aspectj.weaver.CompressingDataOutputStream;
  19. import org.aspectj.weaver.ISourceContext;
  20. import org.aspectj.weaver.IntMap;
  21. import org.aspectj.weaver.ResolvedType;
  22. import org.aspectj.weaver.Shadow;
  23. import org.aspectj.weaver.VersionedDataInputStream;
  24. import org.aspectj.weaver.WeaverMessages;
  25. import org.aspectj.weaver.UnresolvedType;
  26. import org.aspectj.weaver.World;
  27. import org.aspectj.weaver.ast.Literal;
  28. import org.aspectj.weaver.ast.Test;
  29. public class WithinPointcut extends Pointcut {
  30. private TypePattern typePattern;
  31. public WithinPointcut(TypePattern type) {
  32. this.typePattern = type;
  33. this.pointcutKind = WITHIN;
  34. }
  35. public TypePattern getTypePattern() {
  36. return typePattern;
  37. }
  38. private FuzzyBoolean isWithinType(ResolvedType type) {
  39. while (type != null) {
  40. if (typePattern.matchesStatically(type)) {
  41. return FuzzyBoolean.YES;
  42. }
  43. type = type.getDeclaringType();
  44. }
  45. return FuzzyBoolean.NO;
  46. }
  47. public int couldMatchKinds() {
  48. return Shadow.ALL_SHADOW_KINDS_BITS;
  49. }
  50. @Override
  51. public Pointcut parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
  52. WithinPointcut ret = new WithinPointcut(this.typePattern.parameterizeWith(typeVariableMap, w));
  53. ret.copyLocationFrom(this);
  54. return ret;
  55. }
  56. public FuzzyBoolean fastMatch(FastMatchInfo info) {
  57. if (typePattern.annotationPattern instanceof AnyAnnotationTypePattern) {
  58. return isWithinType(info.getType());
  59. }
  60. return FuzzyBoolean.MAYBE;
  61. // Possible alternative implementation that fast matches even annotation patterns: '@Foo *'
  62. // typePattern.resolve(info.world);
  63. // return isWithinType(info.getType());
  64. }
  65. protected FuzzyBoolean matchInternal(Shadow shadow) {
  66. ResolvedType enclosingType = shadow.getIWorld().resolve(shadow.getEnclosingType(), true);
  67. if (enclosingType.isMissing()) {
  68. shadow.getIWorld().getLint().cantFindType.signal(new String[] { WeaverMessages.format(
  69. WeaverMessages.CANT_FIND_TYPE_WITHINPCD, shadow.getEnclosingType().getName()) }, shadow.getSourceLocation(),
  70. new ISourceLocation[] { getSourceLocation() });
  71. }
  72. typePattern.resolve(shadow.getIWorld());
  73. return isWithinType(enclosingType);
  74. }
  75. public void write(CompressingDataOutputStream s) throws IOException {
  76. s.writeByte(Pointcut.WITHIN);
  77. typePattern.write(s);
  78. writeLocation(s);
  79. }
  80. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  81. TypePattern type = TypePattern.read(s, context);
  82. WithinPointcut ret = new WithinPointcut(type);
  83. ret.readLocation(context, s);
  84. return ret;
  85. }
  86. public void resolveBindings(IScope scope, Bindings bindings) {
  87. typePattern = typePattern.resolveBindings(scope, bindings, false, false);
  88. // look for parameterized type patterns which are not supported...
  89. HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor visitor = new HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor();
  90. typePattern.traverse(visitor, null);
  91. if (visitor.wellHasItThen/* ? */()) {
  92. scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.WITHIN_PCD_DOESNT_SUPPORT_PARAMETERS),
  93. getSourceLocation()));
  94. }
  95. }
  96. public void postRead(ResolvedType enclosingType) {
  97. typePattern.postRead(enclosingType);
  98. }
  99. public boolean couldEverMatchSameJoinPointsAs(WithinPointcut other) {
  100. return typePattern.couldEverMatchSameTypesAs(other.typePattern);
  101. }
  102. public boolean equals(Object other) {
  103. if (!(other instanceof WithinPointcut)) {
  104. return false;
  105. }
  106. WithinPointcut o = (WithinPointcut) other;
  107. return o.typePattern.equals(this.typePattern);
  108. }
  109. public int hashCode() {
  110. return typePattern.hashCode();
  111. }
  112. public String toString() {
  113. return "within(" + typePattern + ")";
  114. }
  115. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  116. return match(shadow).alwaysTrue() ? Literal.TRUE : Literal.FALSE;
  117. }
  118. public Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
  119. Pointcut ret = new WithinPointcut(typePattern);
  120. ret.copyLocationFrom(this);
  121. return ret;
  122. }
  123. public Object accept(PatternNodeVisitor visitor, Object data) {
  124. return visitor.visit(this, data);
  125. }
  126. }