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

The following examples show how to use com.sun.source.tree.Tree.Kind#IF . 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: DoubleCheck.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static TreePath findOuterIf(HintContext ctx, TreePath treePath) {
    while (!ctx.isCanceled()) {
        treePath = treePath.getParentPath();
        if (treePath == null) {
            break;
        }
        Tree leaf = treePath.getLeaf();
        
        if (leaf.getKind() == Kind.IF) {
            return treePath;
        }
        
        if (leaf.getKind() == Kind.BLOCK) {
            BlockTree b = (BlockTree)leaf;
            if (b.getStatements().size() == 1) {
                // ok, empty blocks can be around synchronized(this) 
                // statements
                continue;
            }
        }
        
        return null;
    }
    return null;
}
 
Example 2
Source File: Flow.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public Boolean visitBlock(BlockTree node, ConstructorData p) {
    List<? extends StatementTree> statements = new ArrayList<StatementTree>(node.getStatements());
    
    for (int i = 0; i < statements.size(); i++) {
        StatementTree st = statements.get(i);
        
        if (st.getKind() == Kind.IF) {
            IfTree it = (IfTree) st; 
            if (it.getElseStatement() == null && Utilities.exitsFromAllBranchers(info, new TreePath(new TreePath(getCurrentPath(), it), it.getThenStatement()))) {
                generalizedIf(it.getCondition(), it.getThenStatement(), statements.subList(i + 1, statements.size()), false);
                break;
            }
        }
        
        scan(st, null);
    }
    
    return null;
}
 
Example 3
Source File: NPECheck.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean ignore(HintContext ctx, boolean equalsToNull) {
    TreePath test = ctx.getPath().getParentPath();
    
    while (test != null && !StatementTree.class.isAssignableFrom(test.getLeaf().getKind().asInterface())) {
        test = test.getParentPath();
    }
    
    if (test == null) return false;
    
    if (test.getLeaf().getKind() == Kind.ASSERT && !equalsToNull) {
        return verifyConditions(ctx, ((AssertTree) test.getLeaf()).getCondition(), equalsToNull);
    } else if (test.getLeaf().getKind() == Kind.IF && equalsToNull) {
        StatementTree last;
        IfTree it = (IfTree) test.getLeaf();

        switch (it.getThenStatement().getKind()) {
            case BLOCK:
                List<? extends StatementTree> statements = ((BlockTree) it.getThenStatement()).getStatements();
                last = !statements.isEmpty() ? statements.get(statements.size() - 1) : null;
                break;
            default:
                last = it.getThenStatement();
                break;
        }
        
        return last != null && last.getKind() == Kind.THROW && verifyConditions(ctx, ((IfTree) test.getLeaf()).getCondition(), equalsToNull);
    }
    
    return false;
}
 
Example 4
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());
}
 
Example 5
Source File: DoubleCheck.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Checks, that the two if statements test the same variable. Returns the tree that references
 * the guard variable if the two ifs are the same, or {@code null} if the ifs do not match.
 * @param info
 * @param statementTP
 * @param secondTP
 * @return 
 */
private static TreePath sameIfAndValidate(CompilationInfo info, TreePath statementTP, TreePath secondTP, TreePath[] fieldRef) {
    StatementTree statement = (StatementTree) statementTP.getLeaf();
    
    if (statement.getKind() != Kind.IF) {
        return null;
    }
    
    IfTree first = (IfTree)statement;
    IfTree second = (IfTree) secondTP.getLeaf();
    
    if (first.getElseStatement() != null) {
        return null;
    }
    if (second.getElseStatement() != null) {
        return null;
    }
    
    TreePath varFirst = equalToNull(new TreePath(statementTP, first.getCondition()));
    TreePath varSecond = equalToNull(new TreePath(secondTP, second.getCondition()));
    
    if (varFirst == null || varSecond == null) {
        return null;
    }

    Element firstVariable = info.getTrees().getElement(varFirst);
    Element secondVariable = info.getTrees().getElement(varSecond);
    Element target = firstVariable;
    if (firstVariable != null && firstVariable.equals(secondVariable)) {
        TreePath var = info.getTrees().getPath(firstVariable);
        if (info.getSourceVersion().compareTo(SourceVersion.RELEASE_5) < 0) {
            fieldRef[0] = var;
            return varFirst;
        }
        if (firstVariable.getKind() == ElementKind.LOCAL_VARIABLE) {
            // check how the variable was assigned:
            TreePath methodPath = Utilities.findTopLevelBlock(varFirst);
            FlowResult fr = Flow.assignmentsForUse(info, methodPath, new AtomicBoolean(false));
            Iterable<? extends TreePath> itp = fr.getAssignmentsForUse().get(varFirst.getLeaf());
            
            if (itp != null) {
                Iterator<? extends TreePath> i = itp.iterator();
                if (i.hasNext()) {
                    TreePath v = i.next();
                    if (!i.hasNext()) {
                        // if the local variable has exactly one possible value,
                        // use it as the field 
                        target = info.getTrees().getElement(v);
                        if (target != null && target.getKind() == ElementKind.FIELD) {
                            var = info.getTrees().getPath(target);
                            if (!sameCompilationUnit(var, varFirst)) {
                                // the variable is somewhere ... 
                                var = info.getTrees().getPath(firstVariable);
                            }
                        }
                    }
                }
            }
        }
        fieldRef[0] = var;
        return varFirst;
    }
    
    return null;
}
 
Example 6
Source File: DeclarationForInstanceOf.java    From netbeans with Apache License 2.0 4 votes vote down vote up
List<ErrorDescription> run(CompilationInfo info, TreePath treePath, int offset) {
    TreePath ifPath = treePath;
    
    while (ifPath != null) {
        Kind lk = ifPath.getLeaf().getKind();
        
        if (lk == Kind.IF) {
            break;
        }
        
        if (lk == Kind.METHOD || TreeUtilities.CLASS_TREE_KINDS.contains(lk)) {
            return null;
        }
        
        ifPath = ifPath.getParentPath();
    }
    
    if (ifPath == null) {
        return null;
    }
    
    InstanceOfTree leaf = (InstanceOfTree) treePath.getLeaf();
    
    if (leaf.getType() == null || leaf.getType().getKind() == Kind.ERRONEOUS) {
        return null;
    }
    
    TypeMirror castTo = info.getTrees().getTypeMirror(new TreePath(treePath, leaf.getType()));
    TreePath expression = new TreePath(treePath, leaf.getExpression());
    TypeMirror expressionType = info.getTrees().getTypeMirror(expression);
    
    if (!(Utilities.isValidType(castTo) && Utilities.isValidType(expressionType)) || !info.getTypeUtilities().isCastable(expressionType, castTo)) {
        return null;
    }
    
    List<Fix> fix = Collections.<Fix>singletonList(new FixImpl(info.getJavaSource(), TreePathHandle.create(ifPath, info), TreePathHandle.create(expression, info), TypeMirrorHandle.create(castTo), Utilities.getName(castTo)));
    String displayName = NbBundle.getMessage(DeclarationForInstanceOf.class, "ERR_DeclarationForInstanceof");
    ErrorDescription err = ErrorDescriptionFactory.createErrorDescription(Severity.HINT, displayName, fix, info.getFileObject(), offset, offset);

    return Collections.singletonList(err);
}