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.

AndTypePattern.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 AndTypePattern extends TypePattern {
  32. private TypePattern left, right;
  33. public AndTypePattern(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. @Override
  40. protected boolean couldEverMatchSameTypesAs(TypePattern other) {
  41. return true; // don't dive into ands yet....
  42. }
  43. @Override
  44. public FuzzyBoolean matchesInstanceof(ResolvedType type) {
  45. return left.matchesInstanceof(type).and(right.matchesInstanceof(type));
  46. }
  47. @Override
  48. protected boolean matchesExactly(ResolvedType type) {
  49. // ??? if these had side-effects, this sort-circuit could be a mistake
  50. return left.matchesExactly(type) && right.matchesExactly(type);
  51. }
  52. @Override
  53. protected boolean matchesExactly(ResolvedType type, ResolvedType annotatedType) {
  54. return left.matchesExactly(type, annotatedType) && right.matchesExactly(type, annotatedType);
  55. }
  56. @Override
  57. public boolean matchesStatically(ResolvedType type) {
  58. return left.matchesStatically(type) && right.matchesStatically(type);
  59. }
  60. @Override
  61. public void setIsVarArgs(boolean isVarArgs) {
  62. this.isVarArgs = isVarArgs;
  63. left.setIsVarArgs(isVarArgs);
  64. right.setIsVarArgs(isVarArgs);
  65. }
  66. @Override
  67. public void setAnnotationTypePattern(AnnotationTypePattern annPatt) {
  68. if (annPatt == AnnotationTypePattern.ANY) {
  69. return;
  70. }
  71. if (left.annotationPattern == AnnotationTypePattern.ANY) {
  72. left.setAnnotationTypePattern(annPatt);
  73. } else {
  74. left.setAnnotationTypePattern(new AndAnnotationTypePattern(left.annotationPattern, annPatt));
  75. }
  76. if (right.annotationPattern == AnnotationTypePattern.ANY) {
  77. right.setAnnotationTypePattern(annPatt);
  78. } else {
  79. right.setAnnotationTypePattern(new AndAnnotationTypePattern(right.annotationPattern, annPatt));
  80. }
  81. }
  82. @Override
  83. public void write(CompressingDataOutputStream s) throws IOException {
  84. s.writeByte(TypePattern.AND);
  85. left.write(s);
  86. right.write(s);
  87. writeLocation(s);
  88. }
  89. public static TypePattern read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  90. AndTypePattern ret = new AndTypePattern(TypePattern.read(s, context), TypePattern.read(s, context));
  91. ret.readLocation(context, s);
  92. if (ret.left.isVarArgs && ret.right.isVarArgs) {
  93. ret.isVarArgs = true;
  94. }
  95. return ret;
  96. }
  97. @Override
  98. public TypePattern resolveBindings(IScope scope, Bindings bindings, boolean allowBinding, boolean requireExactType) {
  99. if (requireExactType) {
  100. return notExactType(scope);
  101. }
  102. left = left.resolveBindings(scope, bindings, false, false);
  103. right = right.resolveBindings(scope, bindings, false, false);
  104. return this;
  105. }
  106. @Override
  107. public TypePattern parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
  108. TypePattern newLeft = left.parameterizeWith(typeVariableMap, w);
  109. TypePattern newRight = right.parameterizeWith(typeVariableMap, w);
  110. AndTypePattern ret = new AndTypePattern(newLeft, newRight);
  111. ret.copyLocationFrom(this);
  112. return ret;
  113. }
  114. @Override
  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 TypePattern getLeft() {
  133. return left;
  134. }
  135. public TypePattern getRight() {
  136. return right;
  137. }
  138. @Override
  139. public boolean equals(Object obj) {
  140. if (!(obj instanceof AndTypePattern)) {
  141. return false;
  142. }
  143. AndTypePattern atp = (AndTypePattern) obj;
  144. return left.equals(atp.left) && right.equals(atp.right);
  145. }
  146. @Override
  147. public boolean isStarAnnotation() {
  148. return left.isStarAnnotation() && right.isStarAnnotation();
  149. }
  150. /*
  151. * (non-Javadoc)
  152. *
  153. * @see java.lang.Object#hashCode()
  154. */
  155. @Override
  156. public int hashCode() {
  157. int ret = 17;
  158. ret = ret + 37 * left.hashCode();
  159. ret = ret + 37 * right.hashCode();
  160. return ret;
  161. }
  162. @Override
  163. public Object accept(PatternNodeVisitor visitor, Object data) {
  164. return visitor.visit(this, data);
  165. }
  166. @Override
  167. public Object traverse(PatternNodeVisitor visitor, Object data) {
  168. Object ret = accept(visitor, data);
  169. left.traverse(visitor, ret);
  170. right.traverse(visitor, ret);
  171. return ret;
  172. }
  173. }