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.

NotTypePattern.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  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.AjAttribute;
  17. import org.aspectj.weaver.CompressingDataOutputStream;
  18. import org.aspectj.weaver.ISourceContext;
  19. import org.aspectj.weaver.ResolvedType;
  20. import org.aspectj.weaver.UnresolvedType;
  21. import org.aspectj.weaver.VersionedDataInputStream;
  22. import org.aspectj.weaver.World;
  23. /**
  24. * !TypePattern
  25. *
  26. * <p>
  27. * any binding to formals is explicitly forbidden for any composite, ! is just the most obviously wrong case.
  28. *
  29. * @author Erik Hilsdale
  30. * @author Jim Hugunin
  31. */
  32. public class NotTypePattern extends TypePattern {
  33. private TypePattern negatedPattern;
  34. private boolean isBangVoid = false; // is this '!void'
  35. private boolean checked = false;
  36. public NotTypePattern(TypePattern pattern) {
  37. super(false, false); // ??? we override all methods that care about includeSubtypes
  38. this.negatedPattern = pattern;
  39. setLocation(pattern.getSourceContext(), pattern.getStart(), pattern.getEnd());
  40. }
  41. public TypePattern getNegatedPattern() {
  42. return negatedPattern;
  43. }
  44. @Override
  45. protected boolean couldEverMatchSameTypesAs(TypePattern other) {
  46. return true;
  47. }
  48. @Override
  49. public FuzzyBoolean matchesInstanceof(ResolvedType type) {
  50. return negatedPattern.matchesInstanceof(type).not();
  51. }
  52. @Override
  53. protected boolean matchesExactly(ResolvedType type) {
  54. return (!negatedPattern.matchesExactly(type) && annotationPattern.matches(type).alwaysTrue());
  55. }
  56. @Override
  57. protected boolean matchesExactly(ResolvedType type, ResolvedType annotatedType) {
  58. return (!negatedPattern.matchesExactly(type, annotatedType) && annotationPattern.matches(annotatedType).alwaysTrue());
  59. }
  60. @Override
  61. public boolean matchesStatically(ResolvedType type) {
  62. return !negatedPattern.matchesStatically(type);
  63. }
  64. @Override
  65. public void setAnnotationTypePattern(AnnotationTypePattern annPatt) {
  66. super.setAnnotationTypePattern(annPatt);
  67. }
  68. @Override
  69. public void setIsVarArgs(boolean isVarArgs) {
  70. negatedPattern.setIsVarArgs(isVarArgs);
  71. }
  72. @Override
  73. public void write(CompressingDataOutputStream s) throws IOException {
  74. s.writeByte(TypePattern.NOT);
  75. negatedPattern.write(s);
  76. annotationPattern.write(s);
  77. writeLocation(s);
  78. }
  79. public static TypePattern read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  80. TypePattern ret = new NotTypePattern(TypePattern.read(s, context));
  81. if (s.getMajorVersion() >= AjAttribute.WeaverVersionInfo.WEAVER_VERSION_MAJOR_AJ150) {
  82. ret.annotationPattern = AnnotationTypePattern.read(s, context);
  83. }
  84. ret.readLocation(context, s);
  85. return ret;
  86. }
  87. @Override
  88. public TypePattern resolveBindings(IScope scope, Bindings bindings, boolean allowBinding, boolean requireExactType) {
  89. if (requireExactType) {
  90. return notExactType(scope);
  91. }
  92. negatedPattern = negatedPattern.resolveBindings(scope, bindings, false, false);
  93. return this;
  94. }
  95. @Override
  96. public boolean isBangVoid() {
  97. if (!checked) {
  98. isBangVoid = negatedPattern.getExactType().isVoid();
  99. checked = true;
  100. }
  101. return isBangVoid;
  102. }
  103. @Override
  104. public TypePattern parameterizeWith(Map<String,UnresolvedType> typeVariableMap, World w) {
  105. TypePattern newNegatedPattern = negatedPattern.parameterizeWith(typeVariableMap, w);
  106. NotTypePattern ret = new NotTypePattern(newNegatedPattern);
  107. ret.copyLocationFrom(this);
  108. return ret;
  109. }
  110. @Override
  111. public String toString() {
  112. StringBuilder buff = new StringBuilder();
  113. if (annotationPattern != AnnotationTypePattern.ANY) {
  114. buff.append('(');
  115. buff.append(annotationPattern.toString());
  116. buff.append(' ');
  117. }
  118. buff.append('!');
  119. buff.append(negatedPattern);
  120. if (annotationPattern != AnnotationTypePattern.ANY) {
  121. buff.append(')');
  122. }
  123. return buff.toString();
  124. }
  125. /*
  126. * (non-Javadoc)
  127. *
  128. * @see java.lang.Object#equals(java.lang.Object)
  129. */
  130. @Override
  131. public boolean equals(Object obj) {
  132. if (!(obj instanceof NotTypePattern)) {
  133. return false;
  134. }
  135. return (negatedPattern.equals(((NotTypePattern) obj).negatedPattern));
  136. }
  137. /*
  138. * (non-Javadoc)
  139. *
  140. * @see java.lang.Object#hashCode()
  141. */
  142. @Override
  143. public int hashCode() {
  144. return 17 + 37 * negatedPattern.hashCode();
  145. }
  146. @Override
  147. public Object accept(PatternNodeVisitor visitor, Object data) {
  148. return visitor.visit(this, data);
  149. }
  150. @Override
  151. public Object traverse(PatternNodeVisitor visitor, Object data) {
  152. Object ret = accept(visitor, data);
  153. negatedPattern.traverse(visitor, ret);
  154. return ret;
  155. }
  156. }