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.

PerObject.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.util.FuzzyBoolean;
  16. import org.aspectj.weaver.Advice;
  17. import org.aspectj.weaver.AjcMemberMaker;
  18. import org.aspectj.weaver.CompressingDataOutputStream;
  19. import org.aspectj.weaver.ISourceContext;
  20. import org.aspectj.weaver.PerObjectInterfaceTypeMunger;
  21. import org.aspectj.weaver.ResolvedType;
  22. import org.aspectj.weaver.ResolvedTypeMunger;
  23. import org.aspectj.weaver.Shadow;
  24. import org.aspectj.weaver.UnresolvedType;
  25. import org.aspectj.weaver.VersionedDataInputStream;
  26. import org.aspectj.weaver.World;
  27. import org.aspectj.weaver.ast.Expr;
  28. import org.aspectj.weaver.ast.Test;
  29. import org.aspectj.weaver.ast.Var;
  30. public class PerObject extends PerClause {
  31. private final boolean isThis;
  32. private final Pointcut entry;
  33. private static final int thisKindSet;
  34. private static final int targetKindSet;
  35. static {
  36. int thisFlags = Shadow.ALL_SHADOW_KINDS_BITS;
  37. int targFlags = Shadow.ALL_SHADOW_KINDS_BITS;
  38. for (int i = 0; i < Shadow.SHADOW_KINDS.length; i++) {
  39. Shadow.Kind kind = Shadow.SHADOW_KINDS[i];
  40. if (kind.neverHasThis()) {
  41. thisFlags -= kind.bit;
  42. }
  43. if (kind.neverHasTarget()) {
  44. targFlags -= kind.bit;
  45. }
  46. }
  47. thisKindSet = thisFlags;
  48. targetKindSet = targFlags;
  49. }
  50. public PerObject(Pointcut entry, boolean isThis) {
  51. this.entry = entry;
  52. this.isThis = isThis;
  53. }
  54. public Object accept(PatternNodeVisitor visitor, Object data) {
  55. return visitor.visit(this, data);
  56. }
  57. @Override
  58. public Object traverse(PatternNodeVisitor visitor, Object data) {
  59. Object ret = accept(visitor, data);
  60. if (this.entry != null)
  61. this.entry.traverse(visitor, ret);
  62. return ret;
  63. }
  64. public int couldMatchKinds() {
  65. return isThis ? thisKindSet : targetKindSet;
  66. }
  67. // -----
  68. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  69. return FuzzyBoolean.MAYBE;
  70. }
  71. protected FuzzyBoolean matchInternal(Shadow shadow) {
  72. // System.err.println("matches " + this + " ? " + shadow + ", " +
  73. // shadow.hasTarget());
  74. // ??? could probably optimize this better by testing could match
  75. if (isThis) {
  76. return FuzzyBoolean.fromBoolean(shadow.hasThis());
  77. } else {
  78. return FuzzyBoolean.fromBoolean(shadow.hasTarget());
  79. }
  80. }
  81. public void resolveBindings(IScope scope, Bindings bindings) {
  82. // assert bindings == null;
  83. entry.resolve(scope);
  84. }
  85. public Pointcut parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
  86. PerObject ret = new PerObject(entry.parameterizeWith(typeVariableMap, w), isThis);
  87. ret.copyLocationFrom(this);
  88. return ret;
  89. }
  90. private Var getVar(Shadow shadow) {
  91. return isThis ? shadow.getThisVar() : shadow.getTargetVar();
  92. }
  93. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  94. Expr myInstance = Expr.makeCallExpr(AjcMemberMaker.perObjectAspectOfMethod(inAspect), new Expr[] { getVar(shadow) },
  95. inAspect);
  96. state.setAspectInstance(myInstance);
  97. return Test.makeCall(AjcMemberMaker.perObjectHasAspectMethod(inAspect), new Expr[] { getVar(shadow) });
  98. }
  99. public PerClause concretize(ResolvedType inAspect) {
  100. PerObject ret = new PerObject(entry, isThis);
  101. ret.inAspect = inAspect;
  102. if (inAspect.isAbstract()) {
  103. return ret;
  104. }
  105. World world = inAspect.getWorld();
  106. Pointcut concreteEntry = entry.concretize(inAspect, inAspect, 0, null);
  107. // concreteEntry = new AndPointcut(this, concreteEntry);
  108. // concreteEntry.state = Pointcut.CONCRETE;
  109. inAspect.crosscuttingMembers.addConcreteShadowMunger(Advice.makePerObjectEntry(world, concreteEntry, isThis, inAspect));
  110. // FIXME AV - don't use lateMunger here due to test
  111. // "inheritance, around advice and abstract pointcuts"
  112. // see #75442 thread. Issue with weaving order.
  113. ResolvedTypeMunger munger = new PerObjectInterfaceTypeMunger(inAspect, concreteEntry);
  114. inAspect.crosscuttingMembers.addLateTypeMunger(world.getWeavingSupport().concreteTypeMunger(munger, inAspect));
  115. // ATAJ: add a munger to add the aspectOf(..) to the @AJ aspects
  116. if (inAspect.isAnnotationStyleAspect() && !inAspect.isAbstract()) {
  117. inAspect.crosscuttingMembers.addLateTypeMunger(inAspect.getWorld().getWeavingSupport().makePerClauseAspect(inAspect,
  118. getKind()));
  119. }
  120. // ATAJ inline around advice support - don't use a late munger to allow
  121. // around inling for itself
  122. if (inAspect.isAnnotationStyleAspect() && !inAspect.getWorld().isXnoInline()) {
  123. inAspect.crosscuttingMembers.addTypeMunger(world.getWeavingSupport().createAccessForInlineMunger(inAspect));
  124. }
  125. return ret;
  126. }
  127. public void write(CompressingDataOutputStream s) throws IOException {
  128. PEROBJECT.write(s);
  129. entry.write(s);
  130. s.writeBoolean(isThis);
  131. writeLocation(s);
  132. }
  133. public static PerClause readPerClause(VersionedDataInputStream s, ISourceContext context) throws IOException {
  134. PerClause ret = new PerObject(Pointcut.read(s, context), s.readBoolean());
  135. ret.readLocation(context, s);
  136. return ret;
  137. }
  138. public PerClause.Kind getKind() {
  139. return PEROBJECT;
  140. }
  141. public boolean isThis() {
  142. return isThis;
  143. }
  144. public String toString() {
  145. return "per" + (isThis ? "this" : "target") + "(" + entry + ")";
  146. }
  147. public String toDeclarationString() {
  148. return toString();
  149. }
  150. public Pointcut getEntry() {
  151. return entry;
  152. }
  153. public boolean equals(Object other) {
  154. if (!(other instanceof PerObject)) {
  155. return false;
  156. }
  157. PerObject pc = (PerObject) other;
  158. return (pc.isThis && isThis) && ((pc.inAspect == null) ? (inAspect == null) : pc.inAspect.equals(inAspect))
  159. && ((pc.entry == null) ? (entry == null) : pc.entry.equals(entry));
  160. }
  161. public int hashCode() {
  162. int result = 17;
  163. result = 37 * result + (isThis ? 0 : 1);
  164. result = 37 * result + ((inAspect == null) ? 0 : inAspect.hashCode());
  165. result = 37 * result + ((entry == null) ? 0 : entry.hashCode());
  166. return result;
  167. }
  168. }