Search in sources :

Example 26 with SyntaxToken

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

the class AnyRuleIssueFilter method filteredLines.

private static Set<Integer> filteredLines(Tree tree) {
    SyntaxToken firstSyntaxToken = tree.firstToken();
    SyntaxToken lastSyntaxToken = tree.lastToken();
    if (firstSyntaxToken != null && lastSyntaxToken != null) {
        int startLine = firstSyntaxToken.line();
        int endLine = lastSyntaxToken.line();
        // includes trivia on top of first syntax token.
        List<SyntaxTrivia> trivias = firstSyntaxToken.trivias();
        if (!trivias.isEmpty()) {
            startLine = trivias.get(0).startLine();
        }
        return ContiguousSet.create(Range.closed(startLine, endLine), DiscreteDomain.integers());
    }
    return new HashSet<>();
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) SyntaxTrivia(org.sonar.plugins.java.api.tree.SyntaxTrivia) HashSet(java.util.HashSet)

Example 27 with SyntaxToken

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

the class ArrayDesignatorOnVariableCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    VariableTree variableTree = (VariableTree) tree;
    TypeTree type = variableTree.type();
    SyntaxToken identifierToken = variableTree.simpleName().identifierToken();
    while (type.is(Tree.Kind.ARRAY_TYPE)) {
        ArrayTypeTree arrayTypeTree = (ArrayTypeTree) type;
        SyntaxToken arrayDesignatorToken = arrayTypeTree.ellipsisToken();
        if (arrayDesignatorToken == null) {
            arrayDesignatorToken = arrayTypeTree.openBracketToken();
        }
        if (isInvalidPosition(arrayDesignatorToken, identifierToken)) {
            reportIssue(arrayDesignatorToken, "Move the array designator from the variable to the type.");
            break;
        }
        type = arrayTypeTree.type();
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree)

Example 28 with SyntaxToken

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

the class LeftCurlyBraceBaseTreeVisitor method visitClass.

@Override
public void visitClass(ClassTree tree) {
    SyntaxToken lastToken = getLastTokenFromSignature(tree);
    if (lastToken != null) {
        checkTokens(lastToken, tree.openBraceToken());
    }
    super.visitClass(tree);
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken)

Example 29 with SyntaxToken

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

the class LeftCurlyBraceBaseTreeVisitor method visitTryStatement.

@Override
public void visitTryStatement(TryStatementTree tree) {
    SyntaxToken closeParenToken = tree.closeParenToken();
    if (closeParenToken != null) {
        checkBlock(closeParenToken, tree.block());
    } else {
        checkBlock(tree.tryKeyword(), tree.block());
    }
    SyntaxToken finallyKeyword = tree.finallyKeyword();
    if (finallyKeyword != null) {
        checkBlock(finallyKeyword, tree.finallyBlock());
    }
    super.visitTryStatement(tree);
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken)

Example 30 with SyntaxToken

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

the class LeftCurlyBraceBaseTreeVisitor method visitEnumConstant.

@Override
public void visitEnumConstant(EnumConstantTree tree) {
    NewClassTree initializer = tree.initializer();
    ClassTree classBody = initializer.classBody();
    if (classBody != null) {
        SyntaxToken openBraceToken = classBody.openBraceToken();
        if (initializer.arguments().closeParenToken() != null) {
            checkTokens(initializer.arguments().closeParenToken(), openBraceToken);
        } else {
            checkTokens(tree.simpleName().identifierToken(), openBraceToken);
        }
    }
    super.visitEnumConstant(tree);
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

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