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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.lang.reflect.Member;
  17. import java.util.HashSet;
  18. import java.util.Set;
  19. import org.aspectj.lang.JoinPoint;
  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.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. // we know we have exactly one parameter since we're checking an exception handler
  54. return exceptionType.matches(
  55. shadow.getSignature().getParameterTypes()[0].resolve(shadow.getIWorld()),
  56. TypePattern.STATIC);
  57. }
  58. public FuzzyBoolean match(JoinPoint jp, JoinPoint.StaticPart jpsp) {
  59. if (!jp.getKind().equals(JoinPoint.EXCEPTION_HANDLER)) return FuzzyBoolean.NO;
  60. if (jp.getArgs().length > 0) {
  61. Object caughtException = jp.getArgs()[0];
  62. return exceptionType.matches(caughtException,TypePattern.STATIC);
  63. } else {
  64. return FuzzyBoolean.NO;
  65. }
  66. }
  67. /* (non-Javadoc)
  68. * @see org.aspectj.weaver.patterns.Pointcut#matchesDynamically(java.lang.Object, java.lang.Object, java.lang.Object[])
  69. */
  70. public boolean matchesDynamically(Object thisObject, Object targetObject,
  71. Object[] args) {
  72. if (args.length > 0) {
  73. return (exceptionType.matches(args[0],TypePattern.STATIC) == FuzzyBoolean.YES);
  74. } else return false;
  75. }
  76. /* (non-Javadoc)
  77. * @see org.aspectj.weaver.patterns.Pointcut#matchesStatically(java.lang.String, java.lang.reflect.Member, java.lang.Class, java.lang.Class, java.lang.reflect.Member)
  78. */
  79. public FuzzyBoolean matchesStatically(String joinpointKind, Member member,
  80. Class thisClass, Class targetClass, Member withinCode) {
  81. if (!(member instanceof PointcutExpressionImpl.Handler)) {
  82. return FuzzyBoolean.NO;
  83. } else {
  84. Class exceptionClass = ((PointcutExpressionImpl.Handler)member).getHandledExceptionType();
  85. return exceptionType.matches(exceptionClass,TypePattern.STATIC);
  86. }
  87. }
  88. public boolean equals(Object other) {
  89. if (!(other instanceof HandlerPointcut)) return false;
  90. HandlerPointcut o = (HandlerPointcut)other;
  91. return o.exceptionType.equals(this.exceptionType); }
  92. public int hashCode() {
  93. int result = 17;
  94. result = 37*result + exceptionType.hashCode();
  95. return result;
  96. }
  97. public String toString() {
  98. StringBuffer buf = new StringBuffer();
  99. buf.append("handler(");
  100. buf.append(exceptionType.toString());
  101. buf.append(")");
  102. return buf.toString();
  103. }
  104. public void write(DataOutputStream s) throws IOException {
  105. s.writeByte(Pointcut.HANDLER);
  106. exceptionType.write(s);
  107. writeLocation(s);
  108. }
  109. public static Pointcut read(DataInputStream s, ISourceContext context) throws IOException {
  110. HandlerPointcut ret = new HandlerPointcut(TypePattern.read(s, context));
  111. ret.readLocation(context, s);
  112. return ret;
  113. }
  114. // XXX note: there is no namebinding in any kinded pointcut.
  115. // still might want to do something for better error messages
  116. // We want to do something here to make sure we don't sidestep the parameter
  117. // list in capturing type identifiers.
  118. public void resolveBindings(IScope scope, Bindings bindings) {
  119. exceptionType = exceptionType.resolveBindings(scope, bindings, false, false);
  120. //XXX add error if exact binding and not an exception
  121. }
  122. public void resolveBindingsFromRTTI() {
  123. exceptionType = exceptionType.resolveBindingsFromRTTI(false,false);
  124. }
  125. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  126. return match(shadow).alwaysTrue() ? Literal.TRUE : Literal.FALSE;
  127. }
  128. public Pointcut concretize1(ResolvedTypeX inAspect, IntMap bindings) {
  129. Pointcut ret = new HandlerPointcut(exceptionType);
  130. ret.copyLocationFrom(this);
  131. return ret;
  132. }
  133. }