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.3KB

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