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.

PerTypeWithin.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* *******************************************************************
  2. * Copyright (c) 2005 IBM
  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. * Andy Clement initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.IOException;
  14. import java.util.Map;
  15. import org.aspectj.bridge.IMessage;
  16. import org.aspectj.bridge.ISourceLocation;
  17. import org.aspectj.bridge.Message;
  18. import org.aspectj.util.FuzzyBoolean;
  19. import org.aspectj.weaver.Advice;
  20. import org.aspectj.weaver.AjcMemberMaker;
  21. import org.aspectj.weaver.CompressingDataOutputStream;
  22. import org.aspectj.weaver.ISourceContext;
  23. import org.aspectj.weaver.Member;
  24. import org.aspectj.weaver.PerTypeWithinTargetTypeMunger;
  25. import org.aspectj.weaver.ResolvedType;
  26. import org.aspectj.weaver.ResolvedTypeMunger;
  27. import org.aspectj.weaver.Shadow;
  28. import org.aspectj.weaver.UnresolvedType;
  29. import org.aspectj.weaver.VersionedDataInputStream;
  30. import org.aspectj.weaver.World;
  31. import org.aspectj.weaver.ast.Expr;
  32. import org.aspectj.weaver.ast.Literal;
  33. import org.aspectj.weaver.ast.Test;
  34. // PTWIMPL Represents a parsed pertypewithin()
  35. public class PerTypeWithin extends PerClause {
  36. private TypePattern typePattern;
  37. // Any shadow could be considered within a pertypewithin() type pattern
  38. private static final int kindSet = Shadow.ALL_SHADOW_KINDS_BITS;
  39. public TypePattern getTypePattern() {
  40. return typePattern;
  41. }
  42. public PerTypeWithin(TypePattern p) {
  43. typePattern = p;
  44. }
  45. @Override
  46. public Object accept(PatternNodeVisitor visitor, Object data) {
  47. return visitor.visit(this, data);
  48. }
  49. @Override
  50. public int couldMatchKinds() {
  51. return kindSet;
  52. }
  53. @Override
  54. public Pointcut parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
  55. PerTypeWithin ret = new PerTypeWithin(typePattern.parameterizeWith(typeVariableMap, w));
  56. ret.copyLocationFrom(this);
  57. return ret;
  58. }
  59. // -----
  60. @Override
  61. public FuzzyBoolean fastMatch(FastMatchInfo info) {
  62. if (typePattern.annotationPattern instanceof AnyAnnotationTypePattern) {
  63. return isWithinType(info.getType());
  64. }
  65. return FuzzyBoolean.MAYBE;
  66. }
  67. @Override
  68. protected FuzzyBoolean matchInternal(Shadow shadow) {
  69. ResolvedType enclosingType = shadow.getIWorld().resolve(shadow.getEnclosingType(), true);
  70. if (enclosingType.isMissing()) {
  71. // PTWIMPL ?? Add a proper message
  72. IMessage msg = new Message("Cant find type pertypewithin matching...", shadow.getSourceLocation(), true,
  73. new ISourceLocation[] { getSourceLocation() });
  74. shadow.getIWorld().getMessageHandler().handleMessage(msg);
  75. }
  76. // See pr106554 - we can't put advice calls in an interface when the
  77. // advice is defined
  78. // in a pertypewithin aspect - the JPs only exist in the static
  79. // initializer and can't
  80. // call the localAspectOf() method.
  81. if (enclosingType.isInterface()) {
  82. return FuzzyBoolean.NO;
  83. }
  84. if (!(enclosingType.canBeSeenBy(inAspect) || inAspect.isPrivilegedAspect())) {
  85. return FuzzyBoolean.NO;
  86. }
  87. typePattern.resolve(shadow.getIWorld());
  88. return isWithinType(enclosingType);
  89. }
  90. @Override
  91. public void resolveBindings(IScope scope, Bindings bindings) {
  92. typePattern = typePattern.resolveBindings(scope, bindings, false, false);
  93. }
  94. @Override
  95. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  96. // Member ptwField =
  97. // AjcMemberMaker.perTypeWithinField(shadow.getEnclosingType
  98. // (),inAspect);
  99. Expr myInstance = Expr.makeCallExpr(AjcMemberMaker.perTypeWithinLocalAspectOf(shadow.getEnclosingType(), inAspect/*
  100. * shadow.
  101. * getEnclosingType
  102. * ( )
  103. */),
  104. Expr.NONE, inAspect);
  105. state.setAspectInstance(myInstance);
  106. // this worked at one point
  107. // Expr myInstance =
  108. // Expr.makeFieldGet(ptwField,shadow.getEnclosingType()
  109. // .resolve(shadow.getIWorld()));//inAspect);
  110. // state.setAspectInstance(myInstance);
  111. // return Test.makeFieldGetCall(ptwField,null,Expr.NONE);
  112. // cflowField, cflowCounterIsValidMethod, Expr.NONE
  113. // This is what is in the perObject variant of this ...
  114. // Expr myInstance =
  115. // Expr.makeCallExpr(AjcMemberMaker.perTypeWithinAspectOfMethod(inAspect)
  116. // ,
  117. // new Expr[] {getVar(shadow)}, inAspect);
  118. // state.setAspectInstance(myInstance);
  119. // return
  120. // Test.makeCall(AjcMemberMaker.perTypeWithinHasAspectMethod(inAspect),
  121. // new Expr[] { getVar(shadow) });
  122. //
  123. return match(shadow).alwaysTrue() ? Literal.TRUE : Literal.FALSE;
  124. }
  125. @Override
  126. public PerClause concretize(ResolvedType inAspect) {
  127. PerTypeWithin ret = new PerTypeWithin(typePattern);
  128. ret.copyLocationFrom(this);
  129. ret.inAspect = inAspect;
  130. if (inAspect.isAbstract()) {
  131. return ret;
  132. }
  133. World world = inAspect.getWorld();
  134. SignaturePattern sigpat = new SignaturePattern(Member.STATIC_INITIALIZATION, ModifiersPattern.ANY, TypePattern.ANY,
  135. TypePattern.ANY,// typePattern,
  136. NamePattern.ANY, TypePatternList.ANY, ThrowsPattern.ANY, AnnotationTypePattern.ANY);
  137. Pointcut staticInitStar = new KindedPointcut(Shadow.StaticInitialization, sigpat);
  138. Pointcut withinTp = new WithinPointcut(typePattern);
  139. Pointcut andPcut = new AndPointcut(staticInitStar, withinTp);
  140. // We want the pointcut to be:
  141. // 'staticinitialization(*) && within(<typepattern>)' -
  142. // we *cannot* shortcut this to staticinitialization(<typepattern>)
  143. // because it doesnt mean the same thing.
  144. // This munger will initialize the aspect instance field in the matched type
  145. inAspect.crosscuttingMembers.addConcreteShadowMunger(Advice.makePerTypeWithinEntry(world, andPcut, inAspect));
  146. ResolvedTypeMunger munger = new PerTypeWithinTargetTypeMunger(inAspect, ret);
  147. inAspect.crosscuttingMembers.addTypeMunger(world.getWeavingSupport().concreteTypeMunger(munger, inAspect));
  148. // ATAJ: add a munger to add the aspectOf(..) to the @AJ aspects
  149. if (inAspect.isAnnotationStyleAspect() && !inAspect.isAbstract()) {
  150. inAspect.crosscuttingMembers.addLateTypeMunger(world.getWeavingSupport().makePerClauseAspect(inAspect, getKind()));
  151. }
  152. // ATAJ inline around advice support - don't use a late munger to allow
  153. // around inling for itself
  154. if (inAspect.isAnnotationStyleAspect() && !world.isXnoInline()) {
  155. inAspect.crosscuttingMembers.addTypeMunger(world.getWeavingSupport().createAccessForInlineMunger(inAspect));
  156. }
  157. return ret;
  158. }
  159. @Override
  160. public void write(CompressingDataOutputStream s) throws IOException {
  161. PERTYPEWITHIN.write(s);
  162. typePattern.write(s);
  163. writeLocation(s);
  164. }
  165. public static PerClause readPerClause(VersionedDataInputStream s, ISourceContext context) throws IOException {
  166. PerClause ret = new PerTypeWithin(TypePattern.read(s, context));
  167. ret.readLocation(context, s);
  168. return ret;
  169. }
  170. @Override
  171. public PerClause.Kind getKind() {
  172. return PERTYPEWITHIN;
  173. }
  174. @Override
  175. public String toString() {
  176. return "pertypewithin(" + typePattern + ")";
  177. }
  178. @Override
  179. public String toDeclarationString() {
  180. return toString();
  181. }
  182. private FuzzyBoolean isWithinType(ResolvedType type) {
  183. while (type != null) {
  184. if (typePattern.matchesStatically(type)) {
  185. return FuzzyBoolean.YES;
  186. }
  187. type = type.getDeclaringType();
  188. }
  189. return FuzzyBoolean.NO;
  190. }
  191. @Override
  192. public boolean equals(Object other) {
  193. if (!(other instanceof PerTypeWithin)) {
  194. return false;
  195. }
  196. PerTypeWithin pc = (PerTypeWithin) other;
  197. return ((pc.inAspect == null) ? (inAspect == null) : pc.inAspect.equals(inAspect))
  198. && ((pc.typePattern == null) ? (typePattern == null) : pc.typePattern.equals(typePattern));
  199. }
  200. @Override
  201. public int hashCode() {
  202. int result = 17;
  203. result = 37 * result + ((inAspect == null) ? 0 : inAspect.hashCode());
  204. result = 37 * result + ((typePattern == null) ? 0 : typePattern.hashCode());
  205. return result;
  206. }
  207. }