Search in sources :

Example 36 with SyntaxToken

use of org.sonar.plugins.java.api.tree.SyntaxToken in project sonar-java by SonarSource.

the class NestedIfStatementsCheck method visitTryStatement.

@Override
public void visitTryStatement(TryStatementTree tree) {
    SyntaxToken tryKeyword = tree.tryKeyword();
    checkNesting(tryKeyword);
    nestingLevel.push(tryKeyword);
    scan(tree.block());
    nestingLevel.pop();
    scan(tree.resourceList());
    scan(tree.catches());
    scan(tree.finallyBlock());
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken)

Example 37 with SyntaxToken

use of org.sonar.plugins.java.api.tree.SyntaxToken in project sonar-java by SonarSource.

the class EnumConstantTreeImpl method children.

@Override
public Iterable<Tree> children() {
    ImmutableList.Builder<Tree> iteratorBuilder = ImmutableList.builder();
    // the identifierTree simpleName is also present in initializer
    iteratorBuilder.add(modifiers(), initializer());
    SyntaxToken endToken = endToken();
    if (endToken != null) {
        iteratorBuilder.add(endToken);
    }
    return iteratorBuilder.build();
}
Also used : InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) ImmutableList(com.google.common.collect.ImmutableList) Tree(org.sonar.plugins.java.api.tree.Tree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ModifiersTree(org.sonar.plugins.java.api.tree.ModifiersTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) EnumConstantTree(org.sonar.plugins.java.api.tree.EnumConstantTree)

Example 38 with SyntaxToken

use of org.sonar.plugins.java.api.tree.SyntaxToken in project sonar-java by SonarSource.

the class IndentationCheck method checkIndentation.

private void checkIndentation(Tree tree, int expectedLevel) {
    SyntaxToken firstSyntaxToken = tree.firstToken();
    String line = fileLines.get(firstSyntaxToken.line() - 1);
    int level = firstSyntaxToken.column();
    for (int i = 0; i < firstSyntaxToken.column(); i++) {
        if (line.charAt(i) == '\t') {
            level += indentationLevel - 1;
        }
    }
    if (level != expectedLevel && !isExcluded(tree, firstSyntaxToken.line())) {
        context.addIssue(((JavaTree) tree).getLine(), this, "Make this line start at column " + (expectedLevel + 1) + ".");
        isBlockAlreadyReported = true;
    }
    excludeIssueAtLine = tree.lastToken().line();
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken)

Example 39 with SyntaxToken

use of org.sonar.plugins.java.api.tree.SyntaxToken in project sonar-java by SonarSource.

the class ConditionalOnNewLineCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    IfStatementTree ifStatementTree = (IfStatementTree) tree;
    SyntaxToken lastToken = ifStatementTree.thenStatement().lastToken();
    if (ifStatementTree.elseKeyword() == null) {
        if (previousToken != null && isOnSameLineAsPreviousIf(ifStatementTree)) {
            reportIssue(ifStatementTree.ifKeyword(), "Move this \"if\" to a new line or add the missing \"else\".", Collections.singletonList(new JavaFileScannerContext.Location("", previousToken)), null);
        }
        previousToken = lastToken;
    }
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Example 40 with SyntaxToken

use of org.sonar.plugins.java.api.tree.SyntaxToken in project sonar-java by SonarSource.

the class DoubleCheckedLockingCheck method ifSynchronizedIfPattern.

private void ifSynchronizedIfPattern(IfFieldEqNull parentIf, IfStatementTree nestedIf) {
    if (thenStmtInitializeField(nestedIf.thenStatement(), parentIf.field) && !parentIf.field.isVolatile() && !methodIsSynchronized) {
        SyntaxToken synchronizedKeyword = synchronizedStmtStack.peek().synchronizedTree.synchronizedKeyword();
        reportIssue(synchronizedKeyword, "Remove this dangerous instance of double-checked locking.", createFlow(parentIf.ifTree, nestedIf), null);
    }
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken)

Aggregations

SyntaxToken (org.sonar.plugins.java.api.tree.SyntaxToken)47 Tree (org.sonar.plugins.java.api.tree.Tree)9 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)7 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)7 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)7 Test (org.junit.Test)6 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)6 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)5 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)5 ImmutableList (com.google.common.collect.ImmutableList)4 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)4 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)4 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)3 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)3 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 CheckForNull (javax.annotation.CheckForNull)2