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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * Xerox/PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.*;
  14. import java.util.*;
  15. import org.aspectj.weaver.*;
  16. import org.aspectj.weaver.TypeX;
  17. public class ThrowsPattern extends PatternNode {
  18. private TypePatternList required;
  19. private TypePatternList forbidden;
  20. public static final ThrowsPattern ANY =
  21. new ThrowsPattern(TypePatternList.EMPTY, TypePatternList.EMPTY);
  22. public ThrowsPattern(TypePatternList required, TypePatternList forbidden) {
  23. this.required = required;
  24. this.forbidden = forbidden;
  25. }
  26. public String toString() {
  27. if (this == ANY) return "";
  28. String ret = "throws " + required.toString();
  29. if (forbidden.size() > 0) {
  30. ret = ret + " !(" + forbidden.toString() + ")";
  31. }
  32. return ret;
  33. }
  34. public boolean equals(Object other) {
  35. if (!(other instanceof ThrowsPattern)) return false;
  36. ThrowsPattern o = (ThrowsPattern)other;
  37. return o.required.equals(this.required) &&
  38. o.forbidden.equals(this.forbidden);
  39. }
  40. public int hashCode() {
  41. int result = 17;
  42. result = 37*result + required.hashCode();
  43. result = 37*result + forbidden.hashCode();
  44. return result;
  45. }
  46. public ThrowsPattern resolveBindings(IScope scope, Bindings bindings) {
  47. if (this == ANY) return this;
  48. required = required.resolveBindings(scope, bindings, false, false);
  49. forbidden = forbidden.resolveBindings(scope, bindings, false, false);
  50. return this;
  51. }
  52. public boolean matches(TypeX[] tys, World world) {
  53. if (this == ANY) return true;
  54. //System.out.println("matching: " + this + " with " + Arrays.asList(tys));
  55. ResolvedTypeX[] types = world.resolve(tys);
  56. int len = types.length;
  57. for (int j=0, lenj = required.size(); j < lenj; j++) {
  58. if (! matchesAny(required.get(j), types)) {
  59. return false;
  60. }
  61. }
  62. for (int j=0, lenj = forbidden.size(); j < lenj; j++) {
  63. if (matchesAny(forbidden.get(j), types)) {
  64. return false;
  65. }
  66. }
  67. return true;
  68. }
  69. private boolean matchesAny(
  70. TypePattern typePattern,
  71. ResolvedTypeX[] types)
  72. {
  73. for (int i = types.length - 1; i >= 0; i--) {
  74. if (typePattern.matchesExactly(types[i])) return true;
  75. }
  76. return false;
  77. }
  78. public static ThrowsPattern read(DataInputStream s, ISourceContext context) throws IOException {
  79. TypePatternList required = TypePatternList.read(s, context);
  80. TypePatternList forbidden = TypePatternList.read(s, context);
  81. if (required.size() == 0 && forbidden.size() == 0) return ANY;
  82. ThrowsPattern ret = new ThrowsPattern(required, forbidden);
  83. //XXXret.readLocation(context, s);
  84. return ret;
  85. }
  86. public void write(DataOutputStream s) throws IOException {
  87. required.write(s);
  88. forbidden.write(s);
  89. //XXXwriteLocation(s);
  90. }
  91. }