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

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