Example #1
0
 private static void addSuppressWarningsAll(
     JCModifiers mods, JavacNode node, int pos, JCTree source) {
   TreeMaker maker = node.getTreeMaker();
   JCExpression suppressWarningsType = chainDots(node, "java", "lang", "SuppressWarnings");
   JCLiteral allLiteral = maker.Literal("all");
   suppressWarningsType.pos = pos;
   allLiteral.pos = pos;
   JCAnnotation annotation =
       recursiveSetGeneratedBy(
           maker.Annotation(suppressWarningsType, List.<JCExpression>of(allLiteral)), source);
   annotation.pos = pos;
   mods.annotations = mods.annotations.append(annotation);
 }
 /**
  * Process a single compound annotation, returning its Attribute. Used from MemberEnter for
  * attaching the attributes to the annotated symbol.
  */
 Attribute.Compound enterAnnotation(JCAnnotation a, Type expected, Env<AttrContext> env) {
   // The annotation might have had its type attributed (but not checked)
   // by attr.attribAnnotationTypes during MemberEnter, in which case we do not
   // need to do it again.
   Type at =
       (a.annotationType.type != null
           ? a.annotationType.type
           : attr.attribType(a.annotationType, env));
   a.type = chk.checkType(a.annotationType.pos(), at, expected);
   if (a.type.isErroneous())
     return new Attribute.Compound(a.type, List.<Pair<MethodSymbol, Attribute>>nil());
   if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0) {
     log.error(a.annotationType.pos(), "not.annotation.type", a.type.toString());
     return new Attribute.Compound(a.type, List.<Pair<MethodSymbol, Attribute>>nil());
   }
   List<JCExpression> args = a.args;
   if (args.length() == 1 && args.head.getTag() != JCTree.ASSIGN) {
     // special case: elided "value=" assumed
     args.head = make.at(args.head.pos).Assign(make.Ident(names.value), args.head);
   }
   ListBuffer<Pair<MethodSymbol, Attribute>> buf = new ListBuffer<Pair<MethodSymbol, Attribute>>();
   for (List<JCExpression> tl = args; tl.nonEmpty(); tl = tl.tail) {
     JCExpression t = tl.head;
     if (t.getTag() != JCTree.ASSIGN) {
       log.error(t.pos(), "annotation.value.must.be.name.value");
       continue;
     }
     JCAssign assign = (JCAssign) t;
     if (assign.lhs.getTag() != JCTree.IDENT) {
       log.error(t.pos(), "annotation.value.must.be.name.value");
       continue;
     }
     JCIdent left = (JCIdent) assign.lhs;
     Symbol method =
         rs.resolveQualifiedMethod(left.pos(), env, a.type, left.name, List.<Type>nil(), null);
     left.sym = method;
     left.type = method.type;
     if (method.owner != a.type.tsym)
       log.error(left.pos(), "no.annotation.member", left.name, a.type);
     Type result = method.type.getReturnType();
     Attribute value = enterAttributeValue(result, assign.rhs, env);
     if (!method.type.isErroneous())
       buf.append(new Pair<MethodSymbol, Attribute>((MethodSymbol) method, value));
     t.type = result;
   }
   return new Attribute.Compound(a.type, buf.toList());
 }
Example #3
0
 public void visitAnnotation(JCAnnotation tree) {
   try {
     print("@");
     printExpr(tree.annotationType);
     if (tree.args.nonEmpty()) {
       print("(");
       if (tree.args.length() == 1 && tree.args.get(0) instanceof JCAssign) {
         JCExpression lhs = ((JCAssign) tree.args.get(0)).lhs;
         if (lhs instanceof JCIdent && ((JCIdent) lhs).name.toString().equals("value"))
           tree.args = List.of(((JCAssign) tree.args.get(0)).rhs);
       }
       printExprs(tree.args);
       print(")");
     }
   } catch (IOException e) {
     throw new UncheckedIOException(e);
   }
 }
Example #4
0
  static List<JCAnnotation> unboxAndRemoveAnnotationParameter(
      JCAnnotation ast, String parameterName, String errorName, JavacNode errorNode) {
    ListBuffer<JCExpression> params = ListBuffer.lb();
    ListBuffer<JCAnnotation> result = ListBuffer.lb();

    errorNode.removeDeferredErrors();

    outer:
    for (JCExpression param : ast.args) {
      String nameOfParam = "value";
      JCExpression valueOfParam = null;
      if (param instanceof JCAssign) {
        JCAssign assign = (JCAssign) param;
        if (assign.lhs instanceof JCIdent) {
          JCIdent ident = (JCIdent) assign.lhs;
          nameOfParam = ident.name.toString();
        }
        valueOfParam = assign.rhs;
      }

      if (!parameterName.equals(nameOfParam)) {
        params.append(param);
        continue outer;
      }

      if (valueOfParam instanceof JCAnnotation) {
        String dummyAnnotationName = ((JCAnnotation) valueOfParam).annotationType.toString();
        dummyAnnotationName = dummyAnnotationName.replace("_", "");
        if (dummyAnnotationName.length() > 0) {
          errorNode.addError(
              "The correct format is "
                  + errorName
                  + "@_({@SomeAnnotation, @SomeOtherAnnotation}))");
          continue outer;
        }
        for (JCExpression expr : ((JCAnnotation) valueOfParam).args) {
          if (expr instanceof JCAssign && ((JCAssign) expr).lhs instanceof JCIdent) {
            JCIdent id = (JCIdent) ((JCAssign) expr).lhs;
            if ("value".equals(id.name.toString())) {
              expr = ((JCAssign) expr).rhs;
            } else {
              errorNode.addError(
                  "The correct format is "
                      + errorName
                      + "@_({@SomeAnnotation, @SomeOtherAnnotation}))");
              continue outer;
            }
          }

          if (expr instanceof JCAnnotation) {
            result.append((JCAnnotation) expr);
          } else if (expr instanceof JCNewArray) {
            for (JCExpression expr2 : ((JCNewArray) expr).elems) {
              if (expr2 instanceof JCAnnotation) {
                result.append((JCAnnotation) expr2);
              } else {
                errorNode.addError(
                    "The correct format is "
                        + errorName
                        + "@_({@SomeAnnotation, @SomeOtherAnnotation}))");
                continue outer;
              }
            }
          } else {
            errorNode.addError(
                "The correct format is "
                    + errorName
                    + "@_({@SomeAnnotation, @SomeOtherAnnotation}))");
            continue outer;
          }
        }
      } else {
        if (valueOfParam instanceof JCNewArray && ((JCNewArray) valueOfParam).elems.isEmpty()) {
          // Then we just remove it and move on (it's onMethod={} for example).
        } else {
          errorNode.addError(
              "The correct format is "
                  + errorName
                  + "@_({@SomeAnnotation, @SomeOtherAnnotation}))");
        }
      }
    }
    ast.args = params.toList();
    return result.toList();
  }
Example #5
0
  /**
   * Creates an instance of {@code AnnotationValues} for the provided AST Node.
   *
   * @param type An annotation class type, such as {@code lombok.Getter.class}.
   * @param node A Lombok AST node representing an annotation in source code.
   */
  public static <A extends Annotation> AnnotationValues<A> createAnnotation(
      Class<A> type, final JavacNode node) {
    Map<String, AnnotationValue> values = new HashMap<String, AnnotationValue>();
    JCAnnotation anno = (JCAnnotation) node.get();
    List<JCExpression> arguments = anno.getArguments();
    for (Method m : type.getDeclaredMethods()) {
      if (!Modifier.isPublic(m.getModifiers())) continue;
      String name = m.getName();
      java.util.List<String> raws = new ArrayList<String>();
      java.util.List<Object> guesses = new ArrayList<Object>();
      java.util.List<Object> expressions = new ArrayList<Object>();
      final java.util.List<DiagnosticPosition> positions = new ArrayList<DiagnosticPosition>();
      boolean isExplicit = false;

      for (JCExpression arg : arguments) {
        String mName;
        JCExpression rhs;

        if (arg instanceof JCAssign) {
          JCAssign assign = (JCAssign) arg;
          mName = assign.lhs.toString();
          rhs = assign.rhs;
        } else {
          rhs = arg;
          mName = "value";
        }

        if (!mName.equals(name)) continue;
        isExplicit = true;
        if (rhs instanceof JCNewArray) {
          List<JCExpression> elems = ((JCNewArray) rhs).elems;
          for (JCExpression inner : elems) {
            raws.add(inner.toString());
            expressions.add(inner);
            guesses.add(calculateGuess(inner));
            positions.add(inner.pos());
          }
        } else {
          raws.add(rhs.toString());
          expressions.add(rhs);
          guesses.add(calculateGuess(rhs));
          positions.add(rhs.pos());
        }
      }

      values.put(
          name,
          new AnnotationValue(node, raws, expressions, guesses, isExplicit) {
            @Override
            public void setError(String message, int valueIdx) {
              if (valueIdx < 0) node.addError(message);
              else node.addError(message, positions.get(valueIdx));
            }

            @Override
            public void setWarning(String message, int valueIdx) {
              if (valueIdx < 0) node.addWarning(message);
              else node.addWarning(message, positions.get(valueIdx));
            }
          });
    }

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