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

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