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.

ThrowsPattern.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.patterns;
  13. import java.io.IOException;
  14. import java.util.Map;
  15. import org.aspectj.weaver.CompressingDataOutputStream;
  16. import org.aspectj.weaver.ISourceContext;
  17. import org.aspectj.weaver.ResolvedType;
  18. import org.aspectj.weaver.UnresolvedType;
  19. import org.aspectj.weaver.VersionedDataInputStream;
  20. import org.aspectj.weaver.World;
  21. public class ThrowsPattern extends PatternNode {
  22. private TypePatternList required;
  23. private TypePatternList forbidden;
  24. public static final ThrowsPattern ANY = new ThrowsPattern(TypePatternList.EMPTY, TypePatternList.EMPTY);
  25. public ThrowsPattern(TypePatternList required, TypePatternList forbidden) {
  26. this.required = required;
  27. this.forbidden = forbidden;
  28. }
  29. public TypePatternList getRequired() {
  30. return required;
  31. }
  32. public TypePatternList getForbidden() {
  33. return forbidden;
  34. }
  35. public String toString() {
  36. if (this == ANY) {
  37. return "";
  38. }
  39. String ret = "throws " + required.toString();
  40. if (forbidden.size() > 0) {
  41. ret = ret + " !(" + forbidden.toString() + ")";
  42. }
  43. return ret;
  44. }
  45. public boolean equals(Object other) {
  46. if (!(other instanceof ThrowsPattern)) {
  47. return false;
  48. }
  49. ThrowsPattern o = (ThrowsPattern) other;
  50. boolean ret = o.required.equals(this.required) && o.forbidden.equals(this.forbidden);
  51. return ret;
  52. }
  53. public int hashCode() {
  54. int result = 17;
  55. result = 37 * result + required.hashCode();
  56. result = 37 * result + forbidden.hashCode();
  57. return result;
  58. }
  59. public ThrowsPattern resolveBindings(IScope scope, Bindings bindings) {
  60. if (this == ANY) {
  61. return this;
  62. }
  63. required = required.resolveBindings(scope, bindings, false, false);
  64. forbidden = forbidden.resolveBindings(scope, bindings, false, false);
  65. return this;
  66. }
  67. public ThrowsPattern parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
  68. ThrowsPattern ret = new ThrowsPattern(required.parameterizeWith(typeVariableMap, w), forbidden.parameterizeWith(
  69. typeVariableMap, w));
  70. ret.copyLocationFrom(this);
  71. return ret;
  72. }
  73. public boolean matches(UnresolvedType[] tys, World world) {
  74. if (this == ANY) {
  75. return true;
  76. }
  77. // System.out.println("matching: " + this + " with " + Arrays.asList(tys));
  78. ResolvedType[] types = world.resolve(tys);
  79. // int len = types.length;
  80. for (int j = 0, lenj = required.size(); j < lenj; j++) {
  81. if (!matchesAny(required.get(j), types)) {
  82. return false;
  83. }
  84. }
  85. for (int j = 0, lenj = forbidden.size(); j < lenj; j++) {
  86. if (matchesAny(forbidden.get(j), types)) {
  87. return false;
  88. }
  89. }
  90. return true;
  91. }
  92. private boolean matchesAny(TypePattern typePattern, ResolvedType[] types) {
  93. for (int i = types.length - 1; i >= 0; i--) {
  94. if (typePattern.matchesStatically(types[i])) {
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. public static ThrowsPattern read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  101. TypePatternList required = TypePatternList.read(s, context);
  102. TypePatternList forbidden = TypePatternList.read(s, context);
  103. if (required.size() == 0 && forbidden.size() == 0) {
  104. return ANY;
  105. }
  106. ThrowsPattern ret = new ThrowsPattern(required, forbidden);
  107. // XXXret.readLocation(context, s);
  108. return ret;
  109. }
  110. public void write(CompressingDataOutputStream s) throws IOException {
  111. required.write(s);
  112. forbidden.write(s);
  113. // XXXwriteLocation(s);
  114. }
  115. public Object accept(PatternNodeVisitor visitor, Object data) {
  116. return visitor.visit(this, data);
  117. }
  118. public Object traverse(PatternNodeVisitor visitor, Object data) {
  119. Object ret = accept(visitor, data);
  120. forbidden.traverse(visitor, data);
  121. required.traverse(visitor, data);
  122. return ret;
  123. }
  124. }