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.

DeclareSoft.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.IMessage;
  16. import org.aspectj.weaver.CompressingDataOutputStream;
  17. import org.aspectj.weaver.ISourceContext;
  18. import org.aspectj.weaver.ResolvedType;
  19. import org.aspectj.weaver.TypeVariableReferenceType;
  20. import org.aspectj.weaver.UnresolvedType;
  21. import org.aspectj.weaver.VersionedDataInputStream;
  22. import org.aspectj.weaver.WeaverMessages;
  23. import org.aspectj.weaver.World;
  24. public class DeclareSoft extends Declare {
  25. private TypePattern exception;
  26. private Pointcut pointcut;
  27. public DeclareSoft(TypePattern exception, Pointcut pointcut) {
  28. this.exception = exception;
  29. this.pointcut = pointcut;
  30. }
  31. @Override
  32. public Object accept(PatternNodeVisitor visitor, Object data) {
  33. return visitor.visit(this, data);
  34. }
  35. public Object traverse(PatternNodeVisitor visitor, Object data) {
  36. Object ret = accept(visitor, data);
  37. if (this.exception != null)
  38. this.exception.traverse(visitor, ret);
  39. if (this.pointcut != null)
  40. this.pointcut.traverse(visitor, ret);
  41. return ret;
  42. }
  43. @Override
  44. public Declare parameterizeWith(Map typeVariableBindingMap, World w) {
  45. DeclareSoft ret = new DeclareSoft(exception.parameterizeWith(typeVariableBindingMap, w), pointcut.parameterizeWith(
  46. typeVariableBindingMap, w));
  47. ret.copyLocationFrom(this);
  48. return ret;
  49. }
  50. @Override
  51. public String toString() {
  52. StringBuilder buf = new StringBuilder();
  53. buf.append("declare soft: ");
  54. buf.append(exception);
  55. buf.append(": ");
  56. buf.append(pointcut);
  57. buf.append(";");
  58. return buf.toString();
  59. }
  60. @Override
  61. public boolean equals(Object other) {
  62. if (!(other instanceof DeclareSoft)) {
  63. return false;
  64. }
  65. DeclareSoft o = (DeclareSoft) other;
  66. return o.pointcut.equals(pointcut) && o.exception.equals(exception);
  67. }
  68. @Override
  69. public int hashCode() {
  70. int result = 19;
  71. result = 37 * result + pointcut.hashCode();
  72. result = 37 * result + exception.hashCode();
  73. return result;
  74. }
  75. @Override
  76. public void write(CompressingDataOutputStream s) throws IOException {
  77. s.writeByte(Declare.SOFT);
  78. exception.write(s);
  79. pointcut.write(s);
  80. writeLocation(s);
  81. }
  82. public static Declare read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  83. Declare ret = new DeclareSoft(TypePattern.read(s, context), Pointcut.read(s, context));
  84. ret.readLocation(context, s);
  85. return ret;
  86. }
  87. public Pointcut getPointcut() {
  88. return pointcut;
  89. }
  90. public TypePattern getException() {
  91. return exception;
  92. }
  93. @Override
  94. public void resolve(IScope scope) {
  95. exception = exception.resolveBindings(scope, null, false, true);
  96. ResolvedType excType = exception.getExactType().resolve(scope.getWorld());
  97. if (!excType.isMissing()) {
  98. if (excType.isTypeVariableReference()) {
  99. TypeVariableReferenceType typeVariableRT = (TypeVariableReferenceType) excType;
  100. // a declare soft in a generic abstract aspect, we need to check the upper bound
  101. // WIBBLE
  102. excType = typeVariableRT.getTypeVariable().getFirstBound().resolve(scope.getWorld());
  103. }
  104. if (!scope.getWorld().getCoreType(UnresolvedType.THROWABLE).isAssignableFrom(excType)) {
  105. scope.getWorld()
  106. .showMessage(IMessage.ERROR, WeaverMessages.format(WeaverMessages.NOT_THROWABLE, excType.getName()),
  107. exception.getSourceLocation(), null);
  108. pointcut = Pointcut.makeMatchesNothing(Pointcut.RESOLVED);
  109. return;
  110. }
  111. // ENH 42743 suggests that we don't soften runtime exceptions.
  112. if (scope.getWorld().getCoreType(UnresolvedType.RUNTIME_EXCEPTION).isAssignableFrom(excType)) {
  113. scope.getWorld().getLint().runtimeExceptionNotSoftened.signal(new String[] { excType.getName() }, exception
  114. .getSourceLocation(), null);
  115. pointcut = Pointcut.makeMatchesNothing(Pointcut.RESOLVED);
  116. return;
  117. }
  118. }
  119. pointcut = pointcut.resolve(scope);
  120. }
  121. @Override
  122. public boolean isAdviceLike() {
  123. return false;
  124. }
  125. @Override
  126. public String getNameSuffix() {
  127. return "soft";
  128. }
  129. }