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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  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. public int couldMatchKinds() {
  58. return isThis ? thisKindSet : targetKindSet;
  59. }
  60. // -----
  61. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  62. return FuzzyBoolean.MAYBE;
  63. }
  64. protected FuzzyBoolean matchInternal(Shadow shadow) {
  65. // System.err.println("matches " + this + " ? " + shadow + ", " +
  66. // shadow.hasTarget());
  67. // ??? could probably optimize this better by testing could match
  68. if (isThis) {
  69. return FuzzyBoolean.fromBoolean(shadow.hasThis());
  70. } else {
  71. return FuzzyBoolean.fromBoolean(shadow.hasTarget());
  72. }
  73. }
  74. public void resolveBindings(IScope scope, Bindings bindings) {
  75. // assert bindings == null;
  76. entry.resolve(scope);
  77. }
  78. public Pointcut parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
  79. PerObject ret = new PerObject(entry.parameterizeWith(typeVariableMap, w), isThis);
  80. ret.copyLocationFrom(this);
  81. return ret;
  82. }
  83. private Var getVar(Shadow shadow) {
  84. return isThis ? shadow.getThisVar() : shadow.getTargetVar();
  85. }
  86. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  87. Expr myInstance = Expr.makeCallExpr(AjcMemberMaker.perObjectAspectOfMethod(inAspect), new Expr[] { getVar(shadow) },
  88. inAspect);
  89. state.setAspectInstance(myInstance);
  90. return Test.makeCall(AjcMemberMaker.perObjectHasAspectMethod(inAspect), new Expr[] { getVar(shadow) });
  91. }
  92. public PerClause concretize(ResolvedType inAspect) {
  93. PerObject ret = new PerObject(entry, isThis);
  94. ret.inAspect = inAspect;
  95. if (inAspect.isAbstract()) {
  96. return ret;
  97. }
  98. World world = inAspect.getWorld();
  99. Pointcut concreteEntry = entry.concretize(inAspect, inAspect, 0, null);
  100. // concreteEntry = new AndPointcut(this, concreteEntry);
  101. // concreteEntry.state = Pointcut.CONCRETE;
  102. inAspect.crosscuttingMembers.addConcreteShadowMunger(Advice.makePerObjectEntry(world, concreteEntry, isThis, inAspect));
  103. // FIXME AV - don't use lateMunger here due to test
  104. // "inheritance, around advice and abstract pointcuts"
  105. // see #75442 thread. Issue with weaving order.
  106. ResolvedTypeMunger munger = new PerObjectInterfaceTypeMunger(inAspect, concreteEntry);
  107. inAspect.crosscuttingMembers.addLateTypeMunger(world.getWeavingSupport().concreteTypeMunger(munger, inAspect));
  108. // ATAJ: add a munger to add the aspectOf(..) to the @AJ aspects
  109. if (inAspect.isAnnotationStyleAspect() && !inAspect.isAbstract()) {
  110. inAspect.crosscuttingMembers.addLateTypeMunger(inAspect.getWorld().getWeavingSupport().makePerClauseAspect(inAspect,
  111. getKind()));
  112. }
  113. // ATAJ inline around advice support - don't use a late munger to allow
  114. // around inling for itself
  115. if (inAspect.isAnnotationStyleAspect() && !inAspect.getWorld().isXnoInline()) {
  116. inAspect.crosscuttingMembers.addTypeMunger(world.getWeavingSupport().createAccessForInlineMunger(inAspect));
  117. }
  118. return ret;
  119. }
  120. public void write(CompressingDataOutputStream s) throws IOException {
  121. PEROBJECT.write(s);
  122. entry.write(s);
  123. s.writeBoolean(isThis);
  124. writeLocation(s);
  125. }
  126. public static PerClause readPerClause(VersionedDataInputStream s, ISourceContext context) throws IOException {
  127. PerClause ret = new PerObject(Pointcut.read(s, context), s.readBoolean());
  128. ret.readLocation(context, s);
  129. return ret;
  130. }
  131. public PerClause.Kind getKind() {
  132. return PEROBJECT;
  133. }
  134. public boolean isThis() {
  135. return isThis;
  136. }
  137. public String toString() {
  138. return "per" + (isThis ? "this" : "target") + "(" + entry + ")";
  139. }
  140. public String toDeclarationString() {
  141. return toString();
  142. }
  143. public Pointcut getEntry() {
  144. return entry;
  145. }
  146. public boolean equals(Object other) {
  147. if (!(other instanceof PerObject)) {
  148. return false;
  149. }
  150. PerObject pc = (PerObject) other;
  151. return (pc.isThis && isThis) && ((pc.inAspect == null) ? (inAspect == null) : pc.inAspect.equals(inAspect))
  152. && ((pc.entry == null) ? (entry == null) : pc.entry.equals(entry));
  153. }
  154. public int hashCode() {
  155. int result = 17;
  156. result = 37 * result + (isThis ? 0 : 1);
  157. result = 37 * result + ((inAspect == null) ? 0 : inAspect.hashCode());
  158. result = 37 * result + ((entry == null) ? 0 : entry.hashCode());
  159. return result;
  160. }
  161. }