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

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