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.

ThisOrTargetPointcut.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. * Xerox/PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.*;
  14. import org.aspectj.util.FuzzyBoolean;
  15. import org.aspectj.weaver.*;
  16. import org.aspectj.weaver.ast.*;
  17. //
  18. /**
  19. * Corresponds to target or this pcd.
  20. *
  21. * <p>type is initially a WildTypePattern. If it stays that way, it's a this(Foo)
  22. * type deal.
  23. * however, the resolveBindings method may convert it to a BindingTypePattern,
  24. * in which
  25. * case, it's a this(foo) type deal.
  26. *
  27. * @author Erik Hilsdale
  28. * @author Jim Hugunin
  29. */
  30. public class ThisOrTargetPointcut extends NameBindingPointcut {
  31. private boolean isThis;
  32. private TypePattern type;
  33. public ThisOrTargetPointcut(boolean isThis, TypePattern type) {
  34. this.isThis = isThis;
  35. this.type = type;
  36. }
  37. private boolean couldMatch(Shadow shadow) {
  38. return isThis ? shadow.hasThis() : shadow.hasTarget();
  39. }
  40. public FuzzyBoolean match(Shadow shadow) {
  41. if (!couldMatch(shadow)) return FuzzyBoolean.NO;
  42. TypeX typeToMatch = isThis ? shadow.getThisType() : shadow.getTargetType();
  43. //if (typeToMatch == ResolvedTypeX.MISSING) return FuzzyBoolean.NO;
  44. return type.matches(typeToMatch.resolve(shadow.getIWorld()), TypePattern.DYNAMIC);
  45. }
  46. public void write(DataOutputStream s) throws IOException {
  47. s.writeByte(Pointcut.THIS_OR_TARGET);
  48. s.writeBoolean(isThis);
  49. type.write(s);
  50. writeLocation(s);
  51. }
  52. public static Pointcut read(DataInputStream s, ISourceContext context) throws IOException {
  53. boolean isThis = s.readBoolean();
  54. TypePattern type = TypePattern.read(s, context);
  55. ThisOrTargetPointcut ret = new ThisOrTargetPointcut(isThis, type);
  56. ret.readLocation(context, s);
  57. return ret;
  58. }
  59. public void resolveBindings(IScope scope, Bindings bindings) {
  60. type = type.resolveBindings(scope, bindings, true, true);
  61. // ??? handle non-formal
  62. }
  63. public void postRead(ResolvedTypeX enclosingType) {
  64. type.postRead(enclosingType);
  65. }
  66. public boolean equals(Object other) {
  67. if (!(other instanceof ThisOrTargetPointcut)) return false;
  68. ThisOrTargetPointcut o = (ThisOrTargetPointcut)other;
  69. return o.isThis == this.isThis && o.type.equals(this.type);
  70. }
  71. public int hashCode() {
  72. int result = 17;
  73. result = 37*result + (isThis ? 0 : 1);
  74. result = 37*result + type.hashCode();
  75. return result;
  76. }
  77. public String toString() {
  78. return (isThis ? "this(" : "target(") + type + ")";
  79. }
  80. public Test findResidue(Shadow shadow, ExposedState state) {
  81. if (!couldMatch(shadow)) return Literal.FALSE;
  82. if (type == TypePattern.ANY) return Literal.TRUE;
  83. Var var = isThis ? shadow.getThisVar() : shadow.getTargetVar();
  84. return exposeStateForVar(var, type, state, shadow.getIWorld());
  85. }
  86. public Pointcut concretize1(ResolvedTypeX inAspect, IntMap bindings) {
  87. return new ThisOrTargetPointcut(isThis, type.remapAdviceFormals(bindings));
  88. }
  89. }