您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ThrowsPattern.java 4.2KB

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