Ejemplo n.º 1
0
  /**
   * Checks if the provided annotation type is likely to be the intended type for the given
   * annotation node.
   *
   * <p>This is a guess, but a decent one.
   */
  public static boolean annotationTypeMatches(
      Class<? extends java.lang.annotation.Annotation> type, Node node) {
    if (node.getKind() != Kind.ANNOTATION) return false;
    TypeReference typeRef = ((Annotation) node.get()).type;
    if (typeRef == null || typeRef.getTypeName() == null) return false;
    String typeName = toQualifiedName(typeRef.getTypeName());

    TypeLibrary library = new TypeLibrary();
    library.addType(type.getName());
    TypeResolver resolver =
        new TypeResolver(library, node.getPackageDeclaration(), node.getImportStatements());
    Collection<String> typeMatches = resolver.findTypeMatches(node, typeName);

    for (String match : typeMatches) {
      if (match.equals(type.getName())) return true;
    }

    return false;
  }
Ejemplo n.º 2
0
  /** Provides AnnotationValues with the data it needs to do its thing. */
  public static <A extends java.lang.annotation.Annotation> AnnotationValues<A> createAnnotation(
      Class<A> type, final Node annotationNode) {
    final Annotation annotation = (Annotation) annotationNode.get();
    Map<String, AnnotationValue> values = new HashMap<String, AnnotationValue>();

    final MemberValuePair[] pairs = annotation.memberValuePairs();
    for (Method m : type.getDeclaredMethods()) {
      if (!Modifier.isPublic(m.getModifiers())) continue;
      String name = m.getName();
      List<String> raws = new ArrayList<String>();
      List<Object> guesses = new ArrayList<Object>();
      Expression fullExpression = null;
      Expression[] expressions = null;

      if (pairs != null)
        for (MemberValuePair pair : pairs) {
          char[] n = pair.name;
          String mName = n == null ? "value" : new String(pair.name);
          if (mName.equals(name)) fullExpression = pair.value;
        }

      boolean isExplicit = fullExpression != null;

      if (isExplicit) {
        if (fullExpression instanceof ArrayInitializer) {
          expressions = ((ArrayInitializer) fullExpression).expressions;
        } else expressions = new Expression[] {fullExpression};
        if (expressions != null)
          for (Expression ex : expressions) {
            StringBuffer sb = new StringBuffer();
            ex.print(0, sb);
            raws.add(sb.toString());
            guesses.add(calculateValue(ex));
          }
      }

      final Expression fullExpr = fullExpression;
      final Expression[] exprs = expressions;

      values.put(
          name,
          new AnnotationValue(annotationNode, raws, guesses, isExplicit) {
            @Override
            public void setError(String message, int valueIdx) {
              Expression ex;
              if (valueIdx == -1) ex = fullExpr;
              else ex = exprs != null ? exprs[valueIdx] : null;

              if (ex == null) ex = annotation;

              int sourceStart = ex.sourceStart;
              int sourceEnd = ex.sourceEnd;

              annotationNode.addError(message, sourceStart, sourceEnd);
            }

            @Override
            public void setWarning(String message, int valueIdx) {
              Expression ex;
              if (valueIdx == -1) ex = fullExpr;
              else ex = exprs != null ? exprs[valueIdx] : null;

              if (ex == null) ex = annotation;

              int sourceStart = ex.sourceStart;
              int sourceEnd = ex.sourceEnd;

              annotationNode.addWarning(message, sourceStart, sourceEnd);
            }
          });
    }

    return new AnnotationValues<A>(type, values, annotationNode);
  }