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

The following examples show how to use com.sun.source.tree.Tree.Kind#WHILE_LOOP . 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: IteratorToFor.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@TriggerPattern(value = "java.util.Iterator $it = $coll.iterator(); while ($it.hasNext()) {$type $elem = ($type) $it.next(); $rest$;}", 
        constraints = @ConstraintVariableType(variable = "$coll", type = "java.lang.Iterable"))
public static ErrorDescription whileIdiom(HintContext ctx) {
    if (uses(ctx, ctx.getMultiVariables().get("$rest$"), ctx.getVariables().get("$it"))) {
        return null;
    }
    if (!iterable(ctx, ctx.getVariables().get("$coll"), ctx.getVariables().get("$type"))) {
        return null;
    }
    String colString = ctx.getVariables().containsKey("$coll") ? "$coll" : "this";
    Tree highlightTarget = ctx.getPath().getLeaf();
    TreePath elem = ctx.getVariables().get("$elem");
    if (elem.getParentPath() != null && elem.getParentPath().getLeaf().getKind() == Kind.BLOCK) elem = elem.getParentPath();
    if (elem.getParentPath() != null && elem.getParentPath().getLeaf().getKind() == Kind.WHILE_LOOP) highlightTarget = elem.getParentPath().getLeaf();
    return ErrorDescriptionFactory.forName(ctx, highlightTarget, Bundle.ERR_IteratorToFor(),
            JavaFixUtilities.rewriteFix(ctx, Bundle.FIX_IteratorToFor(), ctx.getPath(), "for ($type $elem : " + colString + ") {$rest$;}"));
}
 
Example 2
Source File: Tiny.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Hint(displayName = "#DN_indentation", description = "#DESC_indentation", category="bugs", suppressWarnings="SuspiciousIndentAfterControlStatement", options=Options.QUERY)
@TriggerTreeKind({Kind.IF, Kind.WHILE_LOOP, Kind.FOR_LOOP, Kind.ENHANCED_FOR_LOOP})
public static ErrorDescription indentation(HintContext ctx) {
    Tree firstStatement;
    Tree found = ctx.getPath().getLeaf();
    
    switch (found.getKind()) {
        case IF:
            IfTree it = (IfTree) found;
            if (it.getElseStatement() != null) firstStatement = it.getElseStatement();
            else firstStatement = it.getThenStatement();
            break;
        case WHILE_LOOP:
            firstStatement = ((WhileLoopTree) found).getStatement();
            break;
        case FOR_LOOP:
            firstStatement = ((ForLoopTree) found).getStatement();
            break;
        case ENHANCED_FOR_LOOP:
            firstStatement = ((EnhancedForLoopTree) found).getStatement();
            break;
        default:
            return null;
    }
    
    if (firstStatement != null && firstStatement.getKind() == Kind.BLOCK) {
        return null;
    }
    
    Tree parent = ctx.getPath().getParentPath().getLeaf();
    List<? extends Tree> parentStatements;
    
    switch (parent.getKind()) {
        case BLOCK: parentStatements = ((BlockTree) parent).getStatements(); break;
        case CASE: parentStatements = ((CaseTree) parent).getStatements(); break;
        default: return null;
    }
    
    int index = parentStatements.indexOf(found);
    
    if (index < 0 || index + 1 >= parentStatements.size()) return null;
    
    Tree secondStatement = parentStatements.get(index + 1);
    int firstIndent = indent(ctx, firstStatement);
    int secondIndent = indent(ctx, secondStatement);
    
    if (firstIndent == (-1) || secondIndent == (-1) || firstIndent != secondIndent) return null;
    
    return ErrorDescriptionFactory.forTree(ctx, secondStatement, Bundle.ERR_indentation());
}