/projects/aspectj-1.6.9/org.aspectj.matcher-1.6.9/org/aspectj/weaver/patterns/AnnotationPatternList.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 217 lines · 172 code · 25 blank · 20 comment · 38 complexity · 0bc2516827fee18fcf43acaba679c2b9 MD5 · raw file

  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation.
  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. * ******************************************************************/
  10. package org.aspectj.weaver.patterns;
  11. import java.io.IOException;
  12. import java.util.List;
  13. import java.util.Map;
  14. import org.aspectj.util.FuzzyBoolean;
  15. import org.aspectj.weaver.CompressingDataOutputStream;
  16. import org.aspectj.weaver.ISourceContext;
  17. import org.aspectj.weaver.IntMap;
  18. import org.aspectj.weaver.ResolvedType;
  19. import org.aspectj.weaver.VersionedDataInputStream;
  20. import org.aspectj.weaver.World;
  21. /**
  22. * @author colyer
  23. *
  24. * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code
  25. * Templates
  26. */
  27. public class AnnotationPatternList extends PatternNode {
  28. private AnnotationTypePattern[] typePatterns;
  29. int ellipsisCount = 0;
  30. public static final AnnotationPatternList EMPTY = new AnnotationPatternList(new AnnotationTypePattern[] {});
  31. public static final AnnotationPatternList ANY = new AnnotationPatternList(
  32. new AnnotationTypePattern[] { AnnotationTypePattern.ELLIPSIS });
  33. public AnnotationPatternList() {
  34. typePatterns = new AnnotationTypePattern[0];
  35. ellipsisCount = 0;
  36. }
  37. public AnnotationPatternList(AnnotationTypePattern[] arguments) {
  38. this.typePatterns = arguments;
  39. for (int i = 0; i < arguments.length; i++) {
  40. if (arguments[i] == AnnotationTypePattern.ELLIPSIS) {
  41. ellipsisCount++;
  42. }
  43. }
  44. }
  45. public AnnotationPatternList(List l) {
  46. this((AnnotationTypePattern[]) l.toArray(new AnnotationTypePattern[l.size()]));
  47. }
  48. protected AnnotationTypePattern[] getAnnotationPatterns() {
  49. return typePatterns;
  50. }
  51. public AnnotationPatternList parameterizeWith(Map typeVariableMap, World w) {
  52. AnnotationTypePattern[] parameterizedPatterns = new AnnotationTypePattern[this.typePatterns.length];
  53. for (int i = 0; i < parameterizedPatterns.length; i++) {
  54. parameterizedPatterns[i] = this.typePatterns[i].parameterizeWith(typeVariableMap, w);
  55. }
  56. AnnotationPatternList ret = new AnnotationPatternList(parameterizedPatterns);
  57. ret.copyLocationFrom(this);
  58. return ret;
  59. }
  60. public void resolve(World inWorld) {
  61. for (int i = 0; i < typePatterns.length; i++) {
  62. typePatterns[i].resolve(inWorld);
  63. }
  64. }
  65. public FuzzyBoolean matches(ResolvedType[] someArgs) {
  66. // do some quick length tests first
  67. int numArgsMatchedByEllipsis = (someArgs.length + ellipsisCount) - typePatterns.length;
  68. if (numArgsMatchedByEllipsis < 0) {
  69. return FuzzyBoolean.NO;
  70. }
  71. if ((numArgsMatchedByEllipsis > 0) && (ellipsisCount == 0)) {
  72. return FuzzyBoolean.NO;
  73. }
  74. // now work through the args and the patterns, skipping at ellipsis
  75. FuzzyBoolean ret = FuzzyBoolean.YES;
  76. int argsIndex = 0;
  77. for (int i = 0; i < typePatterns.length; i++) {
  78. if (typePatterns[i] == AnnotationTypePattern.ELLIPSIS) {
  79. // match ellipsisMatchCount args
  80. argsIndex += numArgsMatchedByEllipsis;
  81. } else if (typePatterns[i] == AnnotationTypePattern.ANY) {
  82. argsIndex++;
  83. } else {
  84. // match the argument type at argsIndex with the ExactAnnotationTypePattern
  85. // we know it is exact because nothing else is allowed in args
  86. if (someArgs[argsIndex].isPrimitiveType()) {
  87. return FuzzyBoolean.NO; // can never match
  88. }
  89. ExactAnnotationTypePattern ap = (ExactAnnotationTypePattern) typePatterns[i];
  90. FuzzyBoolean matches = ap.matchesRuntimeType(someArgs[argsIndex]);
  91. if (matches == FuzzyBoolean.NO) {
  92. return FuzzyBoolean.MAYBE; // could still match at runtime
  93. } else {
  94. argsIndex++;
  95. ret = ret.and(matches);
  96. }
  97. }
  98. }
  99. return ret;
  100. }
  101. public int size() {
  102. return typePatterns.length;
  103. }
  104. public AnnotationTypePattern get(int index) {
  105. return typePatterns[index];
  106. }
  107. public AnnotationPatternList resolveBindings(IScope scope, Bindings bindings, boolean allowBinding) {
  108. for (int i = 0; i < typePatterns.length; i++) {
  109. AnnotationTypePattern p = typePatterns[i];
  110. if (p != null) {
  111. typePatterns[i] = typePatterns[i].resolveBindings(scope, bindings, allowBinding);
  112. }
  113. }
  114. return this;
  115. }
  116. public AnnotationPatternList resolveReferences(IntMap bindings) {
  117. int len = typePatterns.length;
  118. AnnotationTypePattern[] ret = new AnnotationTypePattern[len];
  119. for (int i = 0; i < len; i++) {
  120. ret[i] = typePatterns[i].remapAdviceFormals(bindings);
  121. }
  122. return new AnnotationPatternList(ret);
  123. }
  124. public String toString() {
  125. StringBuffer buf = new StringBuffer();
  126. buf.append("(");
  127. for (int i = 0, len = typePatterns.length; i < len; i++) {
  128. AnnotationTypePattern type = typePatterns[i];
  129. if (i > 0) {
  130. buf.append(", ");
  131. }
  132. if (type == AnnotationTypePattern.ELLIPSIS) {
  133. buf.append("..");
  134. } else {
  135. String annPatt = type.toString();
  136. buf.append(annPatt.startsWith("@") ? annPatt.substring(1) : annPatt);
  137. }
  138. }
  139. buf.append(")");
  140. return buf.toString();
  141. }
  142. public boolean equals(Object other) {
  143. if (!(other instanceof AnnotationPatternList)) {
  144. return false;
  145. }
  146. AnnotationPatternList o = (AnnotationPatternList) other;
  147. int len = o.typePatterns.length;
  148. if (len != this.typePatterns.length) {
  149. return false;
  150. }
  151. for (int i = 0; i < len; i++) {
  152. if (!this.typePatterns[i].equals(o.typePatterns[i])) {
  153. return false;
  154. }
  155. }
  156. return true;
  157. }
  158. public int hashCode() {
  159. int result = 41;
  160. for (int i = 0, len = typePatterns.length; i < len; i++) {
  161. result = 37 * result + typePatterns[i].hashCode();
  162. }
  163. return result;
  164. }
  165. public static AnnotationPatternList read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  166. short len = s.readShort();
  167. AnnotationTypePattern[] arguments = new AnnotationTypePattern[len];
  168. for (int i = 0; i < len; i++) {
  169. arguments[i] = AnnotationTypePattern.read(s, context);
  170. }
  171. AnnotationPatternList ret = new AnnotationPatternList(arguments);
  172. ret.readLocation(context, s);
  173. return ret;
  174. }
  175. public void write(CompressingDataOutputStream s) throws IOException {
  176. s.writeShort(typePatterns.length);
  177. for (int i = 0; i < typePatterns.length; i++) {
  178. typePatterns[i].write(s);
  179. }
  180. writeLocation(s);
  181. }
  182. public Object accept(PatternNodeVisitor visitor, Object data) {
  183. return visitor.visit(this, data);
  184. }
  185. public Object traverse(PatternNodeVisitor visitor, Object data) {
  186. Object ret = accept(visitor, data);
  187. for (int i = 0; i < typePatterns.length; i++) {
  188. typePatterns[i].traverse(visitor, ret);
  189. }
  190. return ret;
  191. }
  192. }