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.

WithincodePointcut.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.DataOutputStream;
  14. import java.io.IOException;
  15. import java.lang.reflect.Member;
  16. import java.util.HashSet;
  17. import java.util.Set;
  18. import org.aspectj.lang.JoinPoint;
  19. import org.aspectj.runtime.reflect.Factory;
  20. import org.aspectj.util.FuzzyBoolean;
  21. import org.aspectj.weaver.ISourceContext;
  22. import org.aspectj.weaver.IntMap;
  23. import org.aspectj.weaver.ResolvedTypeX;
  24. import org.aspectj.weaver.Shadow;
  25. import org.aspectj.weaver.VersionedDataInputStream;
  26. import org.aspectj.weaver.ast.Literal;
  27. import org.aspectj.weaver.ast.Test;
  28. public class WithincodePointcut extends Pointcut {
  29. SignaturePattern signature;
  30. private static final Set matchedShadowKinds = new HashSet();
  31. static {
  32. matchedShadowKinds.addAll(Shadow.ALL_SHADOW_KINDS);
  33. for (int i = 0; i < Shadow.SHADOW_KINDS.length; i++) {
  34. if (Shadow.SHADOW_KINDS[i].isEnclosingKind())
  35. matchedShadowKinds.remove(Shadow.SHADOW_KINDS[i]);
  36. }
  37. // these next two are needed for inlining of field initializers
  38. matchedShadowKinds.add(Shadow.ConstructorExecution);
  39. matchedShadowKinds.add(Shadow.Initialization);
  40. }
  41. public WithincodePointcut(SignaturePattern signature) {
  42. this.signature = signature;
  43. this.pointcutKind = WITHINCODE;
  44. }
  45. public Set couldMatchKinds() {
  46. return matchedShadowKinds;
  47. }
  48. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  49. return FuzzyBoolean.MAYBE;
  50. }
  51. protected FuzzyBoolean matchInternal(Shadow shadow) {
  52. //This will not match code in local or anonymous classes as if
  53. //they were withincode of the outer signature
  54. return FuzzyBoolean.fromBoolean(
  55. signature.matches(shadow.getEnclosingCodeSignature(), shadow.getIWorld()));
  56. }
  57. public FuzzyBoolean match(JoinPoint jp, JoinPoint.StaticPart encJP) {
  58. return FuzzyBoolean.fromBoolean(signature.matches(encJP));
  59. }
  60. /* (non-Javadoc)
  61. * @see org.aspectj.weaver.patterns.Pointcut#matchesDynamically(java.lang.Object, java.lang.Object, java.lang.Object[])
  62. */
  63. public boolean matchesDynamically(Object thisObject, Object targetObject,
  64. Object[] args) {
  65. return true;
  66. }
  67. /* (non-Javadoc)
  68. * @see org.aspectj.weaver.patterns.Pointcut#matchesStatically(java.lang.String, java.lang.reflect.Member, java.lang.Class, java.lang.Class, java.lang.reflect.Member)
  69. */
  70. public FuzzyBoolean matchesStatically(String joinpointKind, Member member,
  71. Class thisClass, Class targetClass, Member withinCode) {
  72. if (withinCode == null) return FuzzyBoolean.NO;
  73. return FuzzyBoolean.fromBoolean(signature.matches(Factory.makeEncSJP(withinCode)));
  74. }
  75. public void write(DataOutputStream s) throws IOException {
  76. s.writeByte(Pointcut.WITHINCODE);
  77. signature.write(s);
  78. writeLocation(s);
  79. }
  80. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  81. WithincodePointcut ret = new WithincodePointcut(SignaturePattern.read(s, context));
  82. ret.readLocation(context, s);
  83. return ret;
  84. }
  85. public void resolveBindings(IScope scope, Bindings bindings) {
  86. signature = signature.resolveBindings(scope, bindings);
  87. }
  88. public void resolveBindingsFromRTTI() {
  89. signature = signature.resolveBindingsFromRTTI();
  90. }
  91. public void postRead(ResolvedTypeX enclosingType) {
  92. signature.postRead(enclosingType);
  93. }
  94. public boolean equals(Object other) {
  95. if (!(other instanceof WithincodePointcut)) return false;
  96. WithincodePointcut o = (WithincodePointcut)other;
  97. return o.signature.equals(this.signature);
  98. }
  99. public int hashCode() {
  100. int result = 43;
  101. result = 37*result + signature.hashCode();
  102. return result;
  103. }
  104. public String toString() {
  105. return "withincode(" + signature + ")";
  106. }
  107. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  108. return match(shadow).alwaysTrue() ? Literal.TRUE : Literal.FALSE;
  109. }
  110. public Pointcut concretize1(ResolvedTypeX inAspect, IntMap bindings) {
  111. Pointcut ret = new WithincodePointcut(signature);
  112. ret.copyLocationFrom(this);
  113. return ret;
  114. }
  115. }