Java Code Examples for com.sun.source.tree.AnnotationTree#getArguments()

The following examples show how to use com.sun.source.tree.AnnotationTree#getArguments() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static ExpressionTree findValue(AnnotationTree m, String name) {
    for (ExpressionTree et : m.getArguments()) {
        if (et.getKind() == Kind.ASSIGNMENT) {
            AssignmentTree at = (AssignmentTree) et;
            String varName = ((IdentifierTree) at.getVariable()).getName().toString();

            if (varName.equals(name)) {
                return at.getExpression();
            }
        }

        if (et instanceof LiteralTree/*XXX*/ && "value".equals(name)) {
            return et;
        }
    }

    return null;
}
 
Example 2
Source File: JSEmbeddingProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Void visitMethod(
        final MethodTree m,
        final List<? super LiteralTree> p) {
    for (AnnotationTree a : m.getModifiers().getAnnotations()) {
        final TypeElement ae = (TypeElement) trees.getElement(TreePath.getPath(cu, a.getAnnotationType()));
        if (ae != null && JS_ANNOTATION.contentEquals(ae.getQualifiedName())) {
            final List<? extends ExpressionTree> args =  a.getArguments();
            for (ExpressionTree kvp : args) {
                if (kvp instanceof AssignmentTree) {
                    final AssignmentTree assignemt = (AssignmentTree) kvp;
                    if (BODY.equals(assignemt.getVariable().toString())) {
                        inEmbedding = true;
                        try {
                            scan(assignemt.getExpression(), p);
                        } finally {
                            inEmbedding = false;
                        }
                    }
                }
            }
        }
    }
    return null;
}
 
Example 3
Source File: XPFlagCleaner.java    From piranha with Apache License 2.0 5 votes vote down vote up
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {

  for (String name : handledAnnotations) {
    AnnotationTree at =
        ASTHelpers.getAnnotationWithSimpleName(tree.getModifiers().getAnnotations(), name);

    if (at != null) {
      for (ExpressionTree et : at.getArguments()) {
        if (et.getKind() == Kind.ASSIGNMENT) {
          AssignmentTree assn = (AssignmentTree) et;
          if (ASTHelpers.getSymbol(assn.getExpression())
              .getQualifiedName()
              .toString()
              .endsWith(xpFlagName)) {
            Description.Builder builder = buildDescription(tree);
            SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
            if (isTreated) {
              fixBuilder.delete(at);
              decrementAllSymbolUsages(at, state, fixBuilder);
            } else {
              fixBuilder.delete(tree);
              decrementAllSymbolUsages(tree, state, fixBuilder);
            }
            builder.addFix(fixBuilder.build());
            return builder.build();
          }
        }
      }
    }
  }

  return Description.NO_MATCH;
}
 
Example 4
Source File: CopyFinder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean visitAnnotation(AnnotationTree node, TreePath p) {
    if (p == null)
        return super.visitAnnotation(node, p);

    AnnotationTree t = (AnnotationTree) p.getLeaf();
    
    List<? extends ExpressionTree> arguments = t.getArguments();
    
    if (arguments.size() == 1) {
        ExpressionTree arg = arguments.get(0);
        
        if (arg.getKind() == Kind.ASSIGNMENT) {
            AssignmentTree at = (AssignmentTree) arg;
            
            if (   at.getVariable().getKind() == Kind.IDENTIFIER
                && isMultistatementWildcardTree(at.getExpression())
                && ((IdentifierTree) at.getVariable()).getName().contentEquals("value")) {
                arguments = Collections.singletonList(at.getExpression());
            }
        }
    }
    
    if (!checkLists(node.getArguments(), arguments, p))
        return false;

    return scan(node.getAnnotationType(), t.getAnnotationType(), p);
}
 
Example 5
Source File: RestUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static List<String> getMimeAnnotationValue(AnnotationTree ant) {
    List<? extends ExpressionTree> ets = ant.getArguments();
    if (ets.size() > 0) {
        String value = getValueFromAnnotation(ets.get(0));
        value = value.replace("\"", "");
        return Arrays.asList(value.split(","));
    }
    return Collections.emptyList();
}
 
Example 6
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static ExpressionTree getAnnotationArgumentTree(AnnotationTree annotationTree, 
        String attrName) 
{
    for (ExpressionTree exTree : annotationTree.getArguments()) {
        if (exTree instanceof AssignmentTree) {
            ExpressionTree annVar = ((AssignmentTree) exTree).getVariable();
            if (annVar instanceof IdentifierTree) {
                if (attrName.equals(((IdentifierTree) annVar).getName().toString())) {
                    return exTree;
                }
            }
        }
    }
    return null;
}
 
Example 7
Source File: PersistentTimerInEjbLite.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void fixTimerAnnotation(WorkingCopy copy) {
    TypeElement scheduleAnnotation = copy.getElements().getTypeElement(EJBAPIAnnotations.SCHEDULE);
    ModifiersTree modifiers = ((MethodTree) copy.getTrees().getPath(methodElement.resolve(copy)).getLeaf()).getModifiers();
    TreeMaker tm = copy.getTreeMaker();
    for (AnnotationTree at : modifiers.getAnnotations()) {
        TreePath tp = new TreePath(new TreePath(copy.getCompilationUnit()), at.getAnnotationType());
        Element e = copy.getTrees().getElement(tp);
        if (scheduleAnnotation.equals(e)) {
            List<? extends ExpressionTree> arguments = at.getArguments();
            for (ExpressionTree et : arguments) {
                if (et.getKind() == Tree.Kind.ASSIGNMENT) {
                    AssignmentTree assignment = (AssignmentTree) et;
                    AssignmentTree newAssignment = tm.Assignment(assignment.getVariable(), tm.Literal(false));
                    if (EJBAPIAnnotations.PERSISTENT.equals(assignment.getVariable().toString())) {
                        copy.rewrite(
                                modifiers,
                                copy.getTreeUtilities().translate(modifiers, Collections.singletonMap(et, newAssignment)));
                        return;
                    }
                }
            }
            List<ExpressionTree> newArguments = new ArrayList<ExpressionTree>(arguments);
            ExpressionTree persistenQualIdent = tm.QualIdent(EJBAPIAnnotations.PERSISTENT);
            newArguments.add(tm.Assignment(persistenQualIdent, tm.Literal(false)));
            AnnotationTree newAnnotation = tm.Annotation(tp.getLeaf(), newArguments);
            copy.rewrite(at, newAnnotation);
            return;
        }
    }
}
 
Example 8
Source File: JavaxFacesBeanIsGonnaBeDeprecated.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void performRewrite(TransformationContext ctx) throws Exception {
    WorkingCopy wc = ctx.getWorkingCopy();
    wc.toPhase(JavaSource.Phase.RESOLVED);
    TreeMaker make = wc.getTreeMaker();

    // rewrite annotations in case of ManagedBean
    if (MANAGED_BEAN.equals(annotation.getAnnotationType().toString())) {
        ModifiersTree modifiers = ((ClassTree) wc.getTrees().getTree(element)).getModifiers();
        AnnotationTree annotationTree = (AnnotationTree) wc.getTrees().getTree(element, annotation);
        List<ExpressionTree> arguments = new ArrayList<>();
        for (ExpressionTree expressionTree : annotationTree.getArguments()) {
            if (expressionTree.getKind() == Tree.Kind.ASSIGNMENT) {
                AssignmentTree at = (AssignmentTree) expressionTree;
                String varName = ((IdentifierTree) at.getVariable()).getName().toString();
                if (varName.equals("name")) { //NOI18N
                    ExpressionTree valueTree = make.Identifier(at.getExpression().toString());
                    arguments.add(valueTree);
                }
            }
        }
        ModifiersTree newModifiersTree = make.removeModifiersAnnotation(modifiers, (AnnotationTree) wc.getTrees().getTree(element, annotation));
        AnnotationTree newTree = GenerationUtils.newInstance(wc).createAnnotation(replacingClass, arguments);
        newModifiersTree = make.addModifiersAnnotation(newModifiersTree, newTree);
        wc.rewrite(modifiers, newModifiersTree);
    }

    // rewrite imports
    List<? extends ImportTree> imports = wc.getCompilationUnit().getImports();
    ImportTree newImportTree = make.Import(make.QualIdent(replacingClass), false);
    for (ImportTree importTree : imports) {
        if (deprecatedClass.equals(importTree.getQualifiedIdentifier().toString())) {
            wc.rewrite(importTree, newImportTree);
        }
    }

}
 
Example 9
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
@Override
public Void visitAnnotation(AnnotationTree node, Void unused) {
    sync(node);

    if (visitSingleMemberAnnotation(node)) {
        return null;
    }

    builder.open(ZERO);
    token("@");
    scan(node.getAnnotationType(), null);
    if (!node.getArguments().isEmpty()) {
        builder.open(plusTwo);
        token("(");
        builder.breakOp();
        boolean first = true;

        // Format the member value pairs one-per-line if any of them are
        // initialized with arrays.
        boolean hasArrayInitializer = Iterables.any(node.getArguments(), IS_ARRAY_VALUE);
        for (ExpressionTree argument : node.getArguments()) {
            if (!first) {
                token(",");
                if (hasArrayInitializer) {
                    builder.forcedBreak();
                } else {
                    builder.breakOp(" ");
                }
            }
            if (argument instanceof AssignmentTree) {
                visitAnnotationArgument((AssignmentTree) argument);
            } else {
                scan(argument, null);
            }
            first = false;
        }
        builder.breakOp(UNIFIED, "", minusTwo, Optional.<BreakTag>absent());
        builder.close();
        token(")", plusTwo);
        builder.close();
        return null;

    } else if (builder.peekToken().equals(Optional.of("("))) {
        token("(");
        token(")");
    }
    builder.close();
    return null;
}
 
Example 10
Source File: UseNbBundleMessages.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static boolean isAlreadyRegistered(TreePath treePath, String key) {
    ModifiersTree modifiers;
    Tree tree = treePath.getLeaf();
    switch (tree.getKind()) {
    case METHOD:
        modifiers = ((MethodTree) tree).getModifiers();
        break;
    case VARIABLE:
        modifiers = ((VariableTree) tree).getModifiers();
        break;
    case CLASS:
    case ENUM:
    case INTERFACE:
    case ANNOTATION_TYPE:
        modifiers = ((ClassTree) tree).getModifiers();
        break;
    default:
        modifiers = null;
    }
    if (modifiers != null) {
        for (AnnotationTree ann : modifiers.getAnnotations()) {
            Tree annotationType = ann.getAnnotationType();
            if (annotationType.toString().matches("((org[.]openide[.]util[.])?NbBundle[.])?Messages")) { // XXX see above
                List<? extends ExpressionTree> args = ann.getArguments();
                if (args.size() != 1) {
                    continue; // ?
                }
                AssignmentTree assign = (AssignmentTree) args.get(0);
                if (!assign.getVariable().toString().equals("value")) {
                    continue; // ?
                }
                ExpressionTree arg = assign.getExpression();
                if (arg.getKind() == Tree.Kind.STRING_LITERAL) {
                    if (isRegistered(key, arg)) {
                        return true;
                    }
                } else if (arg.getKind() == Tree.Kind.NEW_ARRAY) {
                    for (ExpressionTree elt : ((NewArrayTree) arg).getInitializers()) {
                        if (isRegistered(key, elt)) {
                            return true;
                        }
                    }
                } else {
                    // ?
                }
            }
        }
    }
    TreePath parentPath = treePath.getParentPath();
    if (parentPath == null) {
        return false;
    }
    // XXX better to check all sources in the same package
    return isAlreadyRegistered(parentPath, key);
}
 
Example 11
Source File: JavaInputAstVisitor.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Void visitAnnotation(AnnotationTree node, Void unused) {
    sync(node);

    if (visitSingleMemberAnnotation(node)) {
        return null;
    }

    builder.open(ZERO);
    token("@");
    scan(node.getAnnotationType(), null);
    if (!node.getArguments().isEmpty()) {
        builder.open(plusTwo);
        token("(");
        builder.breakOp();
        boolean first = true;

        // Format the member value pairs one-per-line if any of them are
        // initialized with arrays.
        boolean hasArrayInitializer = Iterables.any(node.getArguments(), IS_ARRAY_VALUE);
        for (ExpressionTree argument : node.getArguments()) {
            if (!first) {
                token(",");
                if (hasArrayInitializer) {
                    builder.forcedBreak();
                } else {
                    builder.breakOp(" ");
                }
            }
            if (argument instanceof AssignmentTree) {
                visitAnnotationArgument((AssignmentTree) argument);
            } else {
                scan(argument, null);
            }
            first = false;
        }
        builder.breakOp(UNIFIED, "", minusTwo, Optional.<BreakTag>absent());
        builder.close();
        token(")", plusTwo);
        builder.close();
        return null;

    } else if (builder.peekToken().equals(Optional.of("("))) {
        token("(");
        token(")");
    }
    builder.close();
    return null;
}
 
Example 12
Source File: JavaInputAstVisitor.java    From google-java-format with Apache License 2.0 4 votes vote down vote up
@Override
public Void visitAnnotation(AnnotationTree node, Void unused) {
  sync(node);

  if (visitSingleMemberAnnotation(node)) {
    return null;
  }

  builder.open(ZERO);
  token("@");
  scan(node.getAnnotationType(), null);
  if (!node.getArguments().isEmpty()) {
    builder.open(plusFour);
    token("(");
    builder.breakOp();
    boolean first = true;

    // Format the member value pairs one-per-line if any of them are
    // initialized with arrays.
    boolean hasArrayInitializer =
        Iterables.any(node.getArguments(), JavaInputAstVisitor::isArrayValue);
    for (ExpressionTree argument : node.getArguments()) {
      if (!first) {
        token(",");
        if (hasArrayInitializer) {
          builder.forcedBreak();
        } else {
          builder.breakOp(" ");
        }
      }
      if (argument instanceof AssignmentTree) {
        visitAnnotationArgument((AssignmentTree) argument);
      } else {
        scan(argument, null);
      }
      first = false;
    }
    token(")");
    builder.close();
    builder.close();
    return null;

  } else if (builder.peekToken().equals(Optional.of("("))) {
    token("(");
    token(")");
  }
  builder.close();
  return null;
}