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.

ExceptionRange.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.bcel;
  13. import org.aspectj.apache.bcel.generic.InstructionHandle;
  14. import org.aspectj.apache.bcel.generic.InstructionList;
  15. import org.aspectj.weaver.UnresolvedType;
  16. /**
  17. * exceptionRanges are set initially to be low priority. The various setPriority methods should be used accordingly. The priority is
  18. * used when we pack the exception table into a method... the exception table should be sorted from high to low priority. Exceptions
  19. * we generate for advice is either high priority (higher than anything coming from the original method... most kinds of
  20. * non-execution advice) or low priority (lower than anything coming from the original method, for execution advice).
  21. *
  22. * <p>
  23. * ??? This does not account for handler, or any other "statement-level" advice. When such statement level advice happens, we may
  24. * want to go to a float level, so we can set the priority of advice to be lower than anything it encloses, and higher than anything
  25. * enclosing it.
  26. */
  27. /*
  28. * we're actually using the fact that we're an instruction targeter, for the handler
  29. */
  30. public final class ExceptionRange extends Range {
  31. private InstructionHandle handler;
  32. private final UnresolvedType exceptionType;
  33. private final int priority;
  34. // ---- initialization
  35. /**
  36. * After this constructor is called, this range is not well situated unless {@link #associateWithTargets} is called
  37. *
  38. * XXX priority should be fixed
  39. */
  40. public ExceptionRange(InstructionList body, UnresolvedType exceptionType, int priority) {
  41. super(body);
  42. this.exceptionType = exceptionType;
  43. this.priority = priority;
  44. }
  45. /**
  46. * @param insideExisting
  47. */
  48. public ExceptionRange(InstructionList body, UnresolvedType exceptionType, boolean insideExisting) {
  49. this(body, exceptionType, insideExisting ? Integer.MAX_VALUE : -1);
  50. }
  51. public void associateWithTargets(InstructionHandle start, InstructionHandle end, InstructionHandle handler) {
  52. // assert body.contains(start) && body.contains(end) && body.contains(handler)
  53. this.start = start;
  54. this.end = end;
  55. this.handler = handler;
  56. start.addTargeter(this);
  57. end.addTargeter(this);
  58. handler.addTargeter(this);
  59. }
  60. // ----
  61. public InstructionHandle getHandler() {
  62. return handler;
  63. }
  64. public UnresolvedType getCatchType() {
  65. return exceptionType;
  66. }
  67. public int getPriority() {
  68. return priority;
  69. }
  70. // ---- from object
  71. public String toString() {
  72. String str;
  73. if (exceptionType == null) {
  74. str = "finally";
  75. } else {
  76. str = "catch " + exceptionType;
  77. }
  78. // if (priority >= 0 && priority < Integer.MAX_VALUE) {
  79. // str += " (priority " + priority + ")";
  80. // }
  81. return str;
  82. }
  83. public boolean equals(Object other) {
  84. if (!(other instanceof ExceptionRange))
  85. return false;
  86. ExceptionRange o = (ExceptionRange) other;
  87. return o.getStart() == getStart() && o.getEnd() == getEnd() && o.handler == handler
  88. && ((o.exceptionType == null) ? (exceptionType == null) : o.exceptionType.equals(exceptionType))
  89. && o.priority == priority;
  90. }
  91. private volatile int hashCode = 0;
  92. public int hashCode() {
  93. if (hashCode == 0) {
  94. int ret = 17;
  95. ret = 37 * ret + getStart().hashCode();
  96. ret = 37 * ret + getEnd().hashCode();
  97. ret = 37 * ret + handler.hashCode();
  98. ret = 37 * ret + ((exceptionType == null) ? 0 : exceptionType.hashCode());
  99. ret = 37 * ret + priority;
  100. hashCode = ret;
  101. }
  102. return hashCode;
  103. }
  104. public void updateTarget(InstructionHandle oldIh, InstructionHandle newIh, InstructionList newBody) {
  105. super.updateTarget(oldIh, newIh, newBody);
  106. // we're guaranteed that start, end, and handler are distinct instruction handles.
  107. if (oldIh == handler) {
  108. handler = newIh;
  109. }
  110. }
  111. public static boolean isExceptionStart(InstructionHandle ih) {
  112. if (!isRangeHandle(ih))
  113. return false;
  114. Range r = getRange(ih);
  115. if (!(r instanceof ExceptionRange))
  116. return false;
  117. ExceptionRange er = (ExceptionRange) r;
  118. return er.getStart() == ih;
  119. }
  120. public static boolean isExceptionEnd(InstructionHandle ih) {
  121. if (!isRangeHandle(ih))
  122. return false;
  123. Range r = getRange(ih);
  124. if (!(r instanceof ExceptionRange))
  125. return false;
  126. ExceptionRange er = (ExceptionRange) r;
  127. return er.getEnd() == ih;
  128. }
  129. }