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.

HandlerPointcut.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.util.FuzzyBoolean;
  20. import org.aspectj.weaver.ISourceContext;
  21. import org.aspectj.weaver.IntMap;
  22. import org.aspectj.weaver.ResolvedTypeX;
  23. import org.aspectj.weaver.Shadow;
  24. import org.aspectj.weaver.VersionedDataInputStream;
  25. import org.aspectj.weaver.ast.Literal;
  26. import org.aspectj.weaver.ast.Test;
  27. import org.aspectj.weaver.internal.tools.PointcutExpressionImpl;
  28. /**
  29. * This is a kind of KindedPointcut. This belongs either in
  30. * a hierarchy with it or in a new place to share code
  31. * with other potential future statement-level pointcuts like
  32. * synchronized and throws
  33. */
  34. public class HandlerPointcut extends Pointcut {
  35. TypePattern exceptionType;
  36. private static final Set MATCH_KINDS = new HashSet();
  37. static {
  38. MATCH_KINDS.add(Shadow.ExceptionHandler);
  39. }
  40. public HandlerPointcut(TypePattern exceptionType) {
  41. this.exceptionType = exceptionType;
  42. this.pointcutKind = HANDLER;
  43. }
  44. public Set couldMatchKinds() {
  45. return MATCH_KINDS;
  46. }
  47. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  48. //??? should be able to do better by finding all referenced types in type
  49. return FuzzyBoolean.MAYBE;
  50. }
  51. protected FuzzyBoolean matchInternal(Shadow shadow) {
  52. if (shadow.getKind() != Shadow.ExceptionHandler) return FuzzyBoolean.NO;
  53. exceptionType.resolve(shadow.getIWorld());
  54. // we know we have exactly one parameter since we're checking an exception handler
  55. return exceptionType.matches(
  56. shadow.getSignature().getParameterTypes()[0].resolve(shadow.getIWorld()),
  57. TypePattern.STATIC);
  58. }
  59. public FuzzyBoolean match(JoinPoint jp, JoinPoint.StaticPart jpsp) {
  60. if (!jp.getKind().equals(JoinPoint.EXCEPTION_HANDLER)) return FuzzyBoolean.NO;
  61. if (jp.getArgs().length > 0) {
  62. Object caughtException = jp.getArgs()[0];
  63. return exceptionType.matches(caughtException,TypePattern.STATIC);
  64. } else {
  65. return FuzzyBoolean.NO;
  66. }
  67. }
  68. /* (non-Javadoc)
  69. * @see org.aspectj.weaver.patterns.Pointcut#matchesDynamically(java.lang.Object, java.lang.Object, java.lang.Object[])
  70. */
  71. public boolean matchesDynamically(Object thisObject, Object targetObject,
  72. Object[] args) {
  73. if (args.length > 0) {
  74. return (exceptionType.matches(args[0],TypePattern.STATIC) == FuzzyBoolean.YES);
  75. } else return false;
  76. }
  77. /* (non-Javadoc)
  78. * @see org.aspectj.weaver.patterns.Pointcut#matchesStatically(java.lang.String, java.lang.reflect.Member, java.lang.Class, java.lang.Class, java.lang.reflect.Member)
  79. */
  80. public FuzzyBoolean matchesStatically(String joinpointKind, Member member,
  81. Class thisClass, Class targetClass, Member withinCode) {
  82. if (!(member instanceof PointcutExpressionImpl.Handler)) {
  83. return FuzzyBoolean.NO;
  84. } else {
  85. Class exceptionClass = ((PointcutExpressionImpl.Handler)member).getHandledExceptionType();
  86. return exceptionType.matches(exceptionClass,TypePattern.STATIC);
  87. }
  88. }
  89. public boolean equals(Object other) {
  90. if (!(other instanceof HandlerPointcut)) return false;
  91. HandlerPointcut o = (HandlerPointcut)other;
  92. return o.exceptionType.equals(this.exceptionType); }
  93. public int hashCode() {
  94. int result = 17;
  95. result = 37*result + exceptionType.hashCode();
  96. return result;
  97. }
  98. public String toString() {
  99. StringBuffer buf = new StringBuffer();
  100. buf.append("handler(");
  101. buf.append(exceptionType.toString());
  102. buf.append(")");
  103. return buf.toString();
  104. }
  105. public void write(DataOutputStream s) throws IOException {
  106. s.writeByte(Pointcut.HANDLER);
  107. exceptionType.write(s);
  108. writeLocation(s);
  109. }
  110. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  111. HandlerPointcut ret = new HandlerPointcut(TypePattern.read(s, context));
  112. ret.readLocation(context, s);
  113. return ret;
  114. }
  115. // XXX note: there is no namebinding in any kinded pointcut.
  116. // still might want to do something for better error messages
  117. // We want to do something here to make sure we don't sidestep the parameter
  118. // list in capturing type identifiers.
  119. public void resolveBindings(IScope scope, Bindings bindings) {
  120. exceptionType = exceptionType.resolveBindings(scope, bindings, false, false);
  121. //XXX add error if exact binding and not an exception
  122. }
  123. public void resolveBindingsFromRTTI() {
  124. exceptionType = exceptionType.resolveBindingsFromRTTI(false,false);
  125. }
  126. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  127. return match(shadow).alwaysTrue() ? Literal.TRUE : Literal.FALSE;
  128. }
  129. public Pointcut concretize1(ResolvedTypeX inAspect, IntMap bindings) {
  130. Pointcut ret = new HandlerPointcut(exceptionType);
  131. ret.copyLocationFrom(this);
  132. return ret;
  133. }
  134. }