選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

HandlerPointcut.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.UnresolvedType;
  23. import org.aspectj.weaver.VersionedDataInputStream;
  24. import org.aspectj.weaver.WeaverMessages;
  25. import org.aspectj.weaver.World;
  26. import org.aspectj.weaver.ast.Literal;
  27. import org.aspectj.weaver.ast.Test;
  28. /**
  29. * This is a kind of KindedPointcut. This belongs either in a hierarchy with it or in a new place to share code with other potential
  30. * future statement-level pointcuts like synchronized and throws
  31. */
  32. public class HandlerPointcut extends Pointcut {
  33. TypePattern exceptionType;
  34. private static final int MATCH_KINDS = Shadow.ExceptionHandler.bit;
  35. public HandlerPointcut(TypePattern exceptionType) {
  36. this.exceptionType = exceptionType;
  37. this.pointcutKind = HANDLER;
  38. }
  39. public int couldMatchKinds() {
  40. return MATCH_KINDS;
  41. }
  42. public FuzzyBoolean fastMatch(FastMatchInfo type) {
  43. // ??? should be able to do better by finding all referenced types in type
  44. return FuzzyBoolean.MAYBE;
  45. }
  46. protected FuzzyBoolean matchInternal(Shadow shadow) {
  47. if (shadow.getKind() != Shadow.ExceptionHandler) {
  48. return FuzzyBoolean.NO;
  49. }
  50. exceptionType.resolve(shadow.getIWorld());
  51. // we know we have exactly one parameter since we're checking an exception handler
  52. return exceptionType.matches(shadow.getSignature().getParameterTypes()[0].resolve(shadow.getIWorld()), TypePattern.STATIC);
  53. }
  54. public Pointcut parameterizeWith(Map typeVariableMap, World w) {
  55. HandlerPointcut ret = new HandlerPointcut(exceptionType.parameterizeWith(typeVariableMap, w));
  56. ret.copyLocationFrom(this);
  57. return ret;
  58. }
  59. public boolean equals(Object other) {
  60. if (!(other instanceof HandlerPointcut)) {
  61. return false;
  62. }
  63. HandlerPointcut o = (HandlerPointcut) other;
  64. return o.exceptionType.equals(this.exceptionType);
  65. }
  66. public int hashCode() {
  67. int result = 17;
  68. result = 37 * result + exceptionType.hashCode();
  69. return result;
  70. }
  71. public String toString() {
  72. StringBuilder buf = new StringBuilder();
  73. buf.append("handler(");
  74. buf.append(exceptionType.toString());
  75. buf.append(")");
  76. return buf.toString();
  77. }
  78. public void write(CompressingDataOutputStream s) throws IOException {
  79. s.writeByte(Pointcut.HANDLER);
  80. exceptionType.write(s);
  81. writeLocation(s);
  82. }
  83. public static Pointcut read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  84. HandlerPointcut ret = new HandlerPointcut(TypePattern.read(s, context));
  85. ret.readLocation(context, s);
  86. return ret;
  87. }
  88. // XXX note: there is no namebinding in any kinded pointcut.
  89. // still might want to do something for better error messages
  90. // We want to do something here to make sure we don't sidestep the parameter
  91. // list in capturing type identifiers.
  92. public void resolveBindings(IScope scope, Bindings bindings) {
  93. exceptionType = exceptionType.resolveBindings(scope, bindings, false, false);
  94. boolean invalidParameterization = false;
  95. if (exceptionType.getTypeParameters().size() > 0) {
  96. invalidParameterization = true;
  97. }
  98. UnresolvedType exactType = exceptionType.getExactType();
  99. if (exactType != null && exactType.isParameterizedType()) {
  100. invalidParameterization = true;
  101. }
  102. if (invalidParameterization) {
  103. // no parameterized or generic types for handler
  104. scope.message(MessageUtil.error(WeaverMessages.format(WeaverMessages.HANDLER_PCD_DOESNT_SUPPORT_PARAMETERS),
  105. getSourceLocation()));
  106. }
  107. // XXX add error if exact binding and not an exception
  108. }
  109. protected Test findResidueInternal(Shadow shadow, ExposedState state) {
  110. return match(shadow).alwaysTrue() ? Literal.TRUE : Literal.FALSE;
  111. }
  112. public Pointcut concretize1(ResolvedType inAspect, ResolvedType declaringType, IntMap bindings) {
  113. Pointcut ret = new HandlerPointcut(exceptionType);
  114. ret.copyLocationFrom(this);
  115. return ret;
  116. }
  117. public Object accept(PatternNodeVisitor visitor, Object data) {
  118. return visitor.visit(this, data);
  119. }
  120. }