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

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