Java Code Examples for com.sun.source.tree.Tree.Kind#ANNOTATION

The following examples show how to use com.sun.source.tree.Tree.Kind#ANNOTATION . 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: ImmutableTreeTranslator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected final AnnotationTree rewriteChildren(AnnotationTree tree) {
       Tree annotationType = translate(tree.getAnnotationType());
List<? extends ExpressionTree> args = translate(tree.getArguments());
if (annotationType!=tree.getAnnotationType() || !args.equals(tree.getArguments())) {
           if (args != tree.getArguments())
               args = optimize(args);
    AnnotationTree n = tree.getKind() == Kind.ANNOTATION
                   ? make.Annotation(annotationType, args)
                   : make.TypeAnnotation(annotationType, args);
           model.setType(n, model.getType(tree));
    copyCommentTo(tree,n);
    tree = n;
           if (tree.getArguments().size() != args.size())
               model.setPos(tree, NOPOS);
           else
               copyPosTo(tree,n);
}
return tree;
   }
 
Example 2
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static List<AnnotationTree> findArrayValue(AnnotationTree at, String name) {
    ExpressionTree fixesArray = findValue(at, name);
    List<AnnotationTree> fixes = new LinkedList<AnnotationTree>();

    if (fixesArray != null && fixesArray.getKind() == Kind.NEW_ARRAY) {
        NewArrayTree trees = (NewArrayTree) fixesArray;

        for (ExpressionTree fix : trees.getInitializers()) {
            if (fix.getKind() == Kind.ANNOTATION) {
                fixes.add((AnnotationTree) fix);
            }
        }
    }

    if (fixesArray != null && fixesArray.getKind() == Kind.ANNOTATION) {
        fixes.add((AnnotationTree) fixesArray);
    }
    
    return fixes;
}
 
Example 3
Source File: AssignmentIssues.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Hint(displayName = "#DN_org.netbeans.modules.java.hints.AssignmentIssues.nestedAssignment", description = "#DESC_org.netbeans.modules.java.hints.AssignmentIssues.nestedAssignment", category = "assignment_issues", enabled = false, suppressWarnings = "NestedAssignment", options=Options.QUERY) //NOI18N
@TriggerTreeKind({Kind.ASSIGNMENT, Kind.AND_ASSIGNMENT, Kind.DIVIDE_ASSIGNMENT,
    Kind.LEFT_SHIFT_ASSIGNMENT, Kind.MINUS_ASSIGNMENT, Kind.MULTIPLY_ASSIGNMENT,
    Kind.OR_ASSIGNMENT, Kind.PLUS_ASSIGNMENT, Kind.REMAINDER_ASSIGNMENT, Kind.RIGHT_SHIFT_ASSIGNMENT,
    Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT, Kind.XOR_ASSIGNMENT})
public static ErrorDescription nestedAssignment(HintContext context) {
    final TreePath path = context.getPath();
    final Kind parentKind = path.getParentPath().getLeaf().getKind();
    if (parentKind != Kind.EXPRESSION_STATEMENT && parentKind != Kind.ANNOTATION) {
        return ErrorDescriptionFactory.forTree(context, path, NbBundle.getMessage(AssignmentIssues.class, "MSG_NestedAssignment", path.getLeaf())); //NOI18N
    }
    return null;
}
 
Example 4
Source File: TreeFactory.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private AnnotationTree modifyAnnotationAttrValue(AnnotationTree annotation, int index, ExpressionTree attrValue, Operation op) {
    AnnotationTree copy = annotation.getKind() == Kind.ANNOTATION
            ? Annotation(annotation.getAnnotationType(), c(annotation.getArguments(), index, attrValue, op))
            : TypeAnnotation(annotation.getAnnotationType(), c(annotation.getArguments(), index, attrValue, op));
    return copy;
}
 
Example 5
Source File: IntroduceHint.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static TreePath validateSelection(CompilationInfo ci, int start, int end, Set<TypeKind> ignoredTypes) {
    int[] span = TreeUtils.ignoreWhitespaces(ci, Math.min(start, end), Math.max(start, end));

    start = span[0];
    end   = span[1];
    
    TreePath tp = ci.getTreeUtilities().pathFor((start + end) / 2 + 1);

    for ( ; tp != null; tp = tp.getParentPath()) {
        Tree leaf = tp.getLeaf();

        if (   !ExpressionTree.class.isAssignableFrom(leaf.getKind().asInterface())
            && (leaf.getKind() != Kind.VARIABLE || ((VariableTree) leaf).getInitializer() == null))
           continue;

        long treeStart = ci.getTrees().getSourcePositions().getStartPosition(ci.getCompilationUnit(), leaf);
        long treeEnd   = ci.getTrees().getSourcePositions().getEndPosition(ci.getCompilationUnit(), leaf);

        if (treeStart != start || treeEnd != end) {
            continue;
        }

        TypeMirror type = ci.getTrees().getTypeMirror(tp);

        if (type != null && type.getKind() == TypeKind.ERROR) {
            type = ci.getTrees().getOriginalType((ErrorType) type);
        }

        if (type == null || ignoredTypes.contains(type.getKind()))
            continue;

        if(tp.getLeaf().getKind() == Kind.ASSIGNMENT)
            continue;

        if (tp.getLeaf().getKind() == Kind.ANNOTATION)
            continue;

        if (!TreeUtils.isInsideClass(tp))
            return null;

        TreePath candidate = tp;

        tp = tp.getParentPath();

        while (tp != null) {
            switch (tp.getLeaf().getKind()) {
                case VARIABLE:
                    VariableTree vt = (VariableTree) tp.getLeaf();
                    if (vt.getInitializer() == leaf) {
                        return candidate;
                    } else {
                        return null;
                    }
                case NEW_CLASS:
                    NewClassTree nct = (NewClassTree) tp.getLeaf();
                    
                    if (nct.getIdentifier().equals(candidate.getLeaf())) { //avoid disabling hint ie inside of anonymous class higher in treepath
                        for (Tree p : nct.getArguments()) {
                            if (p == leaf) {
                                return candidate;
                            }
                        }

                        return null;
                    }
            }

            leaf = tp.getLeaf();
            tp = tp.getParentPath();
        }

        return candidate;
    }

    return null;
}
 
Example 6
Source File: CreateQualifier.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected List<Fix> analyze( CompilationInfo compilationInfo, int offset )
    throws IOException 
{
    TreePath errorPath = findUnresolvedElement(compilationInfo, offset);
    if ( !checkProject(compilationInfo) || errorPath == null) {
        return Collections.<Fix>emptyList();
    }

    if (compilationInfo.getElements().getTypeElement("java.lang.Object") == null) { // NOI18N
        // broken java platform
        return Collections.<Fix>emptyList();
    }
    
    Element element = compilationInfo.getTrees().getElement(errorPath);
    if ( element == null || element.getSimpleName() == null || 
            errorPath.getLeaf().getKind() != Kind.IDENTIFIER )
    {
        return Collections.<Fix>emptyList();
    }
    
    TreePath parentPath = errorPath.getParentPath();
    if ( parentPath.getLeaf().getKind() != Kind.ANNOTATION ){
        return Collections.<Fix>emptyList();
    }
    Element annotation = compilationInfo.getTrees().getElement(parentPath);
    TreePath path = parentPath;
    while (path != null ){
        Tree leaf = path.getLeaf();
        Kind leafKind = leaf.getKind();
        if ( TreeUtilities.CLASS_TREE_KINDS.contains(leafKind) ){
            Element clazz = compilationInfo.getTrees().getElement(path);
            if ( clazz != null && clazz.getKind() == ElementKind.CLASS )
            {
                return analyzeClass( compilationInfo , (TypeElement)clazz , 
                        annotation );
            }
        }
        else if ( leafKind == Kind.VARIABLE){
            Element var = compilationInfo.getTrees().getElement(path);
            if ( var == null ){
                return null;
            }
            Element parent = var.getEnclosingElement();
            if ( var.getKind() == ElementKind.FIELD && 
                    (parent instanceof TypeElement))
            {
                return analyzeField( compilationInfo , var , annotation ,
                        (TypeElement)parent);
            }
        }
        else if ( leafKind == Kind.METHOD ){
            Element method = compilationInfo.getTrees().getElement(path);
            if ( method != null && method.getKind() == ElementKind.METHOD){
                return analyzeMethodParameter( compilationInfo , 
                        (ExecutableElement)method , annotation );
            }
        }
        path = path.getParentPath();
    }
    
    return null;
}