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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.DataInputStream;
  14. import java.io.DataOutputStream;
  15. import java.io.IOException;
  16. import java.util.HashSet;
  17. import java.util.Iterator;
  18. import java.util.Set;
  19. import org.aspectj.util.FuzzyBoolean;
  20. import org.aspectj.weaver.Advice;
  21. import org.aspectj.weaver.AjcMemberMaker;
  22. import org.aspectj.weaver.ISourceContext;
  23. import org.aspectj.weaver.PerObjectInterfaceTypeMunger;
  24. import org.aspectj.weaver.ResolvedTypeMunger;
  25. import org.aspectj.weaver.ResolvedTypeX;
  26. import org.aspectj.weaver.Shadow;
  27. import org.aspectj.weaver.World;
  28. import org.aspectj.weaver.ast.Expr;
  29. import org.aspectj.weaver.ast.Test;
  30. import org.aspectj.weaver.ast.Var;
  31. public class PerObject extends PerClause {
  32. private boolean isThis;
  33. private Pointcut entry;
  34. private static final Set thisKindSet = new HashSet(Shadow.ALL_SHADOW_KINDS);
  35. private static final Set targetKindSet = new HashSet(Shadow.ALL_SHADOW_KINDS);
  36. static {
  37. for (Iterator iter = Shadow.ALL_SHADOW_KINDS.iterator(); iter.hasNext();) {
  38. Shadow.Kind kind = (Shadow.Kind) iter.next();
  39. if (kind.neverHasThis()) thisKindSet.remove(kind);
  40. if (kind.neverHasTarget()) targetKindSet.remove(kind);
  41. }
  42. }
  43. public PerObject(Pointcut entry, boolean isThis) {
  44. this.entry = entry;
  45. this.isThis = isThis;
  46. }
  47. public Set couldMatchKinds() {
  48. return isThis ? thisKindSet : targetKindSet;
  49. }
  50. // -----
  51. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  52. return FuzzyBoolean.MAYBE;
  53. }
  54. protected FuzzyBoolean matchInternal(Shadow shadow) {
  55. //System.err.println("matches " + this + " ? " + shadow + ", " + shadow.hasTarget());
  56. //??? could probably optimize this better by testing could match
  57. if (isThis) return FuzzyBoolean.fromBoolean(shadow.hasThis());
  58. else return FuzzyBoolean.fromBoolean(shadow.hasTarget());
  59. }
  60. public void resolveBindings(IScope scope, Bindings bindings) {
  61. // assert bindings == null;
  62. entry.resolve(scope);
  63. }
  64. private Var getVar(Shadow shadow) {
  65. return isThis ? shadow.getThisVar() : shadow.getTargetVar();
  66. }
  67. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  68. Expr myInstance =
  69. Expr.makeCallExpr(AjcMemberMaker.perObjectAspectOfMethod(inAspect),
  70. new Expr[] {getVar(shadow)}, inAspect);
  71. state.setAspectInstance(myInstance);
  72. return Test.makeCall(AjcMemberMaker.perObjectHasAspectMethod(inAspect),
  73. new Expr[] { getVar(shadow) });
  74. }
  75. public PerClause concretize(ResolvedTypeX inAspect) {
  76. PerObject ret = new PerObject(entry, isThis);
  77. ret.inAspect = inAspect;
  78. if (inAspect.isAbstract()) return ret;
  79. World world = inAspect.getWorld();
  80. Pointcut concreteEntry = entry.concretize(inAspect, 0, null);
  81. //concreteEntry = new AndPointcut(this, concreteEntry);
  82. //concreteEntry.state = Pointcut.CONCRETE;
  83. inAspect.crosscuttingMembers.addConcreteShadowMunger(
  84. Advice.makePerObjectEntry(world, concreteEntry, isThis, inAspect));
  85. ResolvedTypeMunger munger =
  86. new PerObjectInterfaceTypeMunger(inAspect, concreteEntry);
  87. inAspect.crosscuttingMembers.addTypeMunger(world.concreteTypeMunger(munger, inAspect));
  88. return ret;
  89. }
  90. public void write(DataOutputStream s) throws IOException {
  91. PEROBJECT.write(s);
  92. entry.write(s);
  93. s.writeBoolean(isThis);
  94. writeLocation(s);
  95. }
  96. public static PerClause readPerClause(DataInputStream s, ISourceContext context) throws IOException {
  97. PerClause ret = new PerObject(Pointcut.read(s, context), s.readBoolean());
  98. ret.readLocation(context, s);
  99. return ret;
  100. }
  101. public PerClause.Kind getKind() {
  102. return PEROBJECT;
  103. }
  104. public String toString() {
  105. return "per" + (isThis ? "this" : "target") +
  106. "(" + entry + ")";
  107. }
  108. }