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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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.bridge.MessageUtil;
  16. import org.aspectj.util.FuzzyBoolean;
  17. import org.aspectj.weaver.CompressingDataOutputStream;
  18. import org.aspectj.weaver.ISourceContext;
  19. import org.aspectj.weaver.IntMap;
  20. import org.aspectj.weaver.ResolvedType;
  21. import org.aspectj.weaver.Shadow;
  22. import org.aspectj.weaver.VersionedDataInputStream;
  23. import org.aspectj.weaver.WeaverMessages;
  24. import org.aspectj.weaver.World;
  25. import org.aspectj.weaver.ast.Literal;
  26. import org.aspectj.weaver.ast.Test;
  27. public class WithincodePointcut extends Pointcut {
  28. private SignaturePattern signature;
  29. private static final int matchedShadowKinds;
  30. static {
  31. int flags = Shadow.ALL_SHADOW_KINDS_BITS;
  32. for (int i = 0; i < Shadow.SHADOW_KINDS.length; i++) {
  33. if (Shadow.SHADOW_KINDS[i].isEnclosingKind()) {
  34. flags -= Shadow.SHADOW_KINDS[i].bit;
  35. }
  36. }
  37. // these next two are needed for inlining of field initializers
  38. flags |= Shadow.ConstructorExecution.bit;
  39. flags |= Shadow.Initialization.bit;
  40. matchedShadowKinds = flags;
  41. }
  42. public WithincodePointcut(SignaturePattern signature) {
  43. this.signature = signature;
  44. this.pointcutKind = WITHINCODE;
  45. }
  46. public SignaturePattern getSignature() {
  47. return signature;
  48. }
  49. public int couldMatchKinds() {
  50. return matchedShadowKinds;
  51. }
  52. public Pointcut parameterizeWith(Map typeVariableMap, World w) {
  53. WithincodePointcut ret = new WithincodePointcut(signature.parameterizeWith(typeVariableMap, w));
  54. ret.copyLocationFrom(this);
  55. return ret;
  56. }
  57. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  58. return FuzzyBoolean.MAYBE;
  59. }
  60. protected FuzzyBoolean matchInternal(Shadow shadow) {
  61. // This will not match code in local or anonymous classes as if
  62. // they were withincode of the outer signature
  63. return FuzzyBoolean.fromBoolean(signature.matches(shadow.getEnclosingCodeSignature(), shadow.getIWorld(), false));
  64. }
  65. public void write(CompressingDataOutputStream s) throws IOException {
  66. s.writeByte(Pointcut.WITHINCODE);
  67. signature.write(s);
  68. writeLocation(s);
  69. }
  70. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  71. WithincodePointcut ret = new WithincodePointcut(SignaturePattern.read(s, context));
  72. ret.readLocation(context, s);
  73. return ret;
  74. }
  75. public void resolveBindings(IScope scope, Bindings bindings) {
  76. signature = signature.resolveBindings(scope, bindings);
  77. // look for inappropriate use of parameterized types and tell user...
  78. HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor visitor = new HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor();
  79. signature.getDeclaringType().traverse(visitor, null);
  80. if (visitor.wellHasItThen/* ? */()) {
  81. scope.message(MessageUtil.error(WeaverMessages
  82. .format(WeaverMessages.WITHINCODE_DOESNT_SUPPORT_PARAMETERIZED_DECLARING_TYPES), getSourceLocation()));
  83. }
  84. visitor = new HasThisTypePatternTriedToSneakInSomeGenericOrParameterizedTypePatternMatchingStuffAnywhereVisitor();
  85. signature.getThrowsPattern().traverse(visitor, null);
  86. if (visitor.wellHasItThen/* ? */()) {
  87. scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.NO_GENERIC_THROWABLES), getSourceLocation()));
  88. }
  89. }
  90. public void postRead(ResolvedType enclosingType) {
  91. signature.postRead(enclosingType);
  92. }
  93. public boolean equals(Object other) {
  94. if (!(other instanceof WithincodePointcut)) {
  95. return false;
  96. }
  97. WithincodePointcut o = (WithincodePointcut) other;
  98. return o.signature.equals(this.signature);
  99. }
  100. public int hashCode() {
  101. int result = 43;
  102. result = 37 * result + signature.hashCode();
  103. return result;
  104. }
  105. public String toString() {
  106. return "withincode(" + signature + ")";
  107. }
  108. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  109. return match(shadow).alwaysTrue() ? Literal.TRUE : Literal.FALSE;
  110. }
  111. public Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
  112. Pointcut ret = new WithincodePointcut(signature);
  113. ret.copyLocationFrom(this);
  114. return ret;
  115. }
  116. public Object accept(PatternNodeVisitor visitor, Object data) {
  117. return visitor.visit(this, data);
  118. }
  119. @Override
  120. public Object traverse(PatternNodeVisitor visitor, Object data) {
  121. Object ret = accept(visitor, data);
  122. if (this.signature != null)
  123. this.signature.traverse(visitor, ret);
  124. return ret;
  125. }
  126. }