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.

OrTypePattern.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.util.FuzzyBoolean;
  16. import org.aspectj.weaver.CompressingDataOutputStream;
  17. import org.aspectj.weaver.ISourceContext;
  18. import org.aspectj.weaver.ResolvedType;
  19. import org.aspectj.weaver.VersionedDataInputStream;
  20. import org.aspectj.weaver.World;
  21. /**
  22. * left || right
  23. *
  24. * <p>
  25. * any binding to formals is explicitly forbidden for any composite by the language
  26. *
  27. * @author Erik Hilsdale
  28. * @author Jim Hugunin
  29. */
  30. public class OrTypePattern extends TypePattern {
  31. private TypePattern left, right;
  32. public OrTypePattern(TypePattern left, TypePattern right) {
  33. super(false, false); // ??? we override all methods that care about includeSubtypes
  34. this.left = left;
  35. this.right = right;
  36. setLocation(left.getSourceContext(), left.getStart(), right.getEnd());
  37. }
  38. public TypePattern getRight() {
  39. return right;
  40. }
  41. public TypePattern getLeft() {
  42. return left;
  43. }
  44. /*
  45. * (non-Javadoc)
  46. *
  47. * @see org.aspectj.weaver.patterns.TypePattern#couldEverMatchSameTypesAs(org.aspectj.weaver.patterns.TypePattern)
  48. */
  49. protected boolean couldEverMatchSameTypesAs(TypePattern other) {
  50. return true; // don't dive at the moment...
  51. }
  52. public FuzzyBoolean matchesInstanceof(ResolvedType type) {
  53. return left.matchesInstanceof(type).or(right.matchesInstanceof(type));
  54. }
  55. protected boolean matchesExactly(ResolvedType type) {
  56. // ??? if these had side-effects, this sort-circuit could be a mistake
  57. return left.matchesExactly(type) || right.matchesExactly(type);
  58. }
  59. protected boolean matchesExactly(ResolvedType type, ResolvedType annotatedType) {
  60. // ??? if these had side-effects, this sort-circuit could be a mistake
  61. return left.matchesExactly(type, annotatedType) || right.matchesExactly(type, annotatedType);
  62. }
  63. public boolean matchesStatically(ResolvedType type) {
  64. return left.matchesStatically(type) || right.matchesStatically(type);
  65. }
  66. public void setIsVarArgs(boolean isVarArgs) {
  67. this.isVarArgs = isVarArgs;
  68. left.setIsVarArgs(isVarArgs);
  69. right.setIsVarArgs(isVarArgs);
  70. }
  71. public void setAnnotationTypePattern(AnnotationTypePattern annPatt) {
  72. if (annPatt == AnnotationTypePattern.ANY) {
  73. return;
  74. }
  75. if (left.annotationPattern == AnnotationTypePattern.ANY) {
  76. left.setAnnotationTypePattern(annPatt);
  77. } else {
  78. left.setAnnotationTypePattern(new AndAnnotationTypePattern(left.annotationPattern, annPatt));
  79. }
  80. if (right.annotationPattern == AnnotationTypePattern.ANY) {
  81. right.setAnnotationTypePattern(annPatt);
  82. } else {
  83. right.setAnnotationTypePattern(new AndAnnotationTypePattern(right.annotationPattern, annPatt));
  84. }
  85. }
  86. public void write(CompressingDataOutputStream s) throws IOException {
  87. s.writeByte(TypePattern.OR);
  88. left.write(s);
  89. right.write(s);
  90. writeLocation(s);
  91. }
  92. public static TypePattern read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  93. OrTypePattern ret = new OrTypePattern(TypePattern.read(s, context), TypePattern.read(s, context));
  94. ret.readLocation(context, s);
  95. if (ret.left.isVarArgs && ret.right.isVarArgs) {
  96. ret.isVarArgs = true;
  97. }
  98. return ret;
  99. }
  100. public TypePattern resolveBindings(IScope scope, Bindings bindings, boolean allowBinding, boolean requireExactType) {
  101. if (requireExactType) {
  102. return notExactType(scope);
  103. }
  104. left = left.resolveBindings(scope, bindings, false, false);
  105. right = right.resolveBindings(scope, bindings, false, false);
  106. return this;
  107. }
  108. public TypePattern parameterizeWith(Map typeVariableMap, World w) {
  109. TypePattern newLeft = left.parameterizeWith(typeVariableMap, w);
  110. TypePattern newRight = right.parameterizeWith(typeVariableMap, w);
  111. OrTypePattern ret = new OrTypePattern(newLeft, newRight);
  112. ret.copyLocationFrom(this);
  113. return ret;
  114. }
  115. public String toString() {
  116. StringBuffer buff = new StringBuffer();
  117. if (annotationPattern != AnnotationTypePattern.ANY) {
  118. buff.append('(');
  119. buff.append(annotationPattern.toString());
  120. buff.append(' ');
  121. }
  122. buff.append('(');
  123. buff.append(left.toString());
  124. buff.append(" || ");
  125. buff.append(right.toString());
  126. buff.append(')');
  127. if (annotationPattern != AnnotationTypePattern.ANY) {
  128. buff.append(')');
  129. }
  130. return buff.toString();
  131. }
  132. public boolean isStarAnnotation() {
  133. return left.isStarAnnotation() || right.isStarAnnotation();
  134. }
  135. /*
  136. * (non-Javadoc)
  137. *
  138. * @see java.lang.Object#equals(java.lang.Object)
  139. */
  140. public boolean equals(Object obj) {
  141. if (!(obj instanceof OrTypePattern)) {
  142. return false;
  143. }
  144. OrTypePattern other = (OrTypePattern) obj;
  145. return left.equals(other.left) && right.equals(other.right);
  146. }
  147. /*
  148. * (non-Javadoc)
  149. *
  150. * @see java.lang.Object#hashCode()
  151. */
  152. public int hashCode() {
  153. int ret = 17;
  154. ret = ret + 37 * left.hashCode();
  155. ret = ret + 37 * right.hashCode();
  156. return ret;
  157. }
  158. public Object accept(PatternNodeVisitor visitor, Object data) {
  159. return visitor.visit(this, data);
  160. }
  161. public Object traverse(PatternNodeVisitor visitor, Object data) {
  162. Object ret = accept(visitor, data);
  163. left.traverse(visitor, ret);
  164. right.traverse(visitor, ret);
  165. return ret;
  166. }
  167. }