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

The following examples show how to use com.sun.source.tree.Tree.Kind#ENHANCED_FOR_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: Unbalanced.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@TriggerPattern(value="$mods$ $type[] $name = $init$;")
public static ErrorDescription after(HintContext ctx) {
    VariableElement var = testElement(ctx);

    if (var == null) return null;

    Tree parent = ctx.getPath().getParentPath().getLeaf();

    if (parent.getKind() == Kind.ENHANCED_FOR_LOOP
        && ((EnhancedForLoopTree) parent).getVariable() == ctx.getPath().getLeaf()) {
        return null;
    }
    
    TreePath init = ctx.getVariables().get("$init$");

    if (init != null) {
        boolean asWrite = true;
        
        if (init.getLeaf().getKind() == Kind.NEW_ARRAY) {
            NewArrayTree nat = (NewArrayTree) init.getLeaf();

            if (nat.getInitializers() == null || nat.getInitializers().isEmpty()) {
                asWrite = false;
            }
        }
        
        if (asWrite) {
            record(ctx.getInfo(), var, State.WRITE);
        }
    }

    return produceWarning(ctx, "ERR_UnbalancedArray");
}
 
Example 2
Source File: Unbalanced.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@TriggerPattern(value="$mods$ $type $name = $init$;")
public static ErrorDescription after(HintContext ctx) {
    if (testElement(ctx) == null) return null;

    TreePath init = ctx.getVariables().get("$init$");

    if (init != null) {
        if (init.getLeaf().getKind() != Kind.NEW_CLASS) return null;

        NewClassTree nct = (NewClassTree) init.getLeaf();

        if (nct.getClassBody() != null || nct.getArguments().size() > 1) return null;

        if (nct.getArguments().size() == 1) {
            TypeMirror tm = ctx.getInfo().getTrees().getTypeMirror(new TreePath(init, nct.getArguments().get(0)));

            if (tm == null || tm.getKind() != TypeKind.INT) return null;
        }
    }

    if (   ctx.getPath().getParentPath().getLeaf().getKind() == Kind.ENHANCED_FOR_LOOP
        && ((EnhancedForLoopTree) ctx.getPath().getParentPath().getLeaf()).getVariable() == ctx.getPath().getLeaf()) {
        return null;
    }
    
    return produceWarning(ctx, "ERR_UnbalancedCollection");
}
 
Example 3
Source File: AddParameterOrLocalFix.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean isEnhancedForLoopVariable(TreePath tp) {
    if (tp.getLeaf().getKind() != Kind.VARIABLE)
        return false;
    TreePath context = tp.getParentPath();
    if (context == null || context.getLeaf().getKind() != Kind.ENHANCED_FOR_LOOP)
        return false;
    return true;
}
 
Example 4
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * @param tp tested {@link TreePath}
 * @return true if <code>tp</code> is an IDENTIFIER in a VARIABLE in an ENHANCED_FOR_LOOP
 */
public static boolean isEnhancedForLoopIdentifier(TreePath tp) {
    if (tp == null || tp.getLeaf().getKind() != Kind.IDENTIFIER)
        return false;
    TreePath parent = tp.getParentPath();
    if (parent == null || parent.getLeaf().getKind() != Kind.VARIABLE)
        return false;
    TreePath context = parent.getParentPath();
    if (context == null || context.getLeaf().getKind() != Kind.ENHANCED_FOR_LOOP)
        return false;
    return true;
}
 
Example 5
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());
}