Search in sources :

Example 41 with SyntaxToken

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

the class SyntaxHighlighterVisitor method highlight.

private void highlight(Tree from, Tree to, TypeOfText typeOfText) {
    SyntaxToken firstToken = from.firstToken();
    SyntaxToken lastToken = to.lastToken();
    highlighting.highlight(firstToken.line(), firstToken.column(), lastToken.line(), lastToken.column() + lastToken.text().length(), typeOfText);
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken)

Example 42 with SyntaxToken

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

the class TreeFactory method newModuleName.

public ModuleNameTree newModuleName(InternalSyntaxToken firstIdentifier, Optional<List<Tuple<InternalSyntaxToken, InternalSyntaxToken>>> rest) {
    List<IdentifierTree> identifiers = new ArrayList<>();
    List<SyntaxToken> separators = new ArrayList<>();
    identifiers.add(new IdentifierTreeImpl(firstIdentifier));
    if (rest.isPresent()) {
        for (Tuple<InternalSyntaxToken, InternalSyntaxToken> modulePart : rest.get()) {
            separators.add(modulePart.first());
            identifiers.add(new IdentifierTreeImpl(modulePart.second()));
        }
    }
    return new ModuleNameTreeImpl(Collections.unmodifiableList(identifiers), Collections.unmodifiableList(separators));
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) ArrayList(java.util.ArrayList) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken)

Example 43 with SyntaxToken

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

the class SubscriptionVisitor method visit.

private void visit(Tree tree) {
    boolean isSubscribed = isSubscribed(tree);
    boolean shouldVisitSyntaxToken = (visitToken || visitTrivia) && tree.is(Tree.Kind.TOKEN);
    if (shouldVisitSyntaxToken) {
        SyntaxToken syntaxToken = (SyntaxToken) tree;
        if (visitToken) {
            visitToken(syntaxToken);
        }
        if (visitTrivia) {
            for (SyntaxTrivia syntaxTrivia : syntaxToken.trivias()) {
                visitTrivia(syntaxTrivia);
            }
        }
    } else if (isSubscribed) {
        visitNode(tree);
    }
    visitChildren(tree);
    if (!shouldVisitSyntaxToken && isSubscribed) {
        leaveNode(tree);
    }
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) SyntaxTrivia(org.sonar.plugins.java.api.tree.SyntaxTrivia)

Example 44 with SyntaxToken

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

the class AnyRuleIssueFilterTest method invalid_tree_does_not_exclude_lines.

@Test
public void invalid_tree_does_not_exclude_lines() {
    // by default, any issue oin line 7 is accepted
    assertThatIssueWillBeAccepted(7).isTrue();
    Tree mockTree = mock(Tree.class);
    // without first nor last token, line can not be excluded
    filter.excludeLines(mockTree);
    assertThatIssueWillBeAccepted(7).isTrue();
    SyntaxToken mockFirstToken = mock(SyntaxToken.class);
    when(mockFirstToken.line()).thenReturn(7);
    when(mockTree.firstToken()).thenReturn(mockFirstToken);
    // without last token, line can not be excluded
    filter.excludeLines(mockTree);
    assertThatIssueWillBeAccepted(7).isTrue();
    SyntaxToken mockLastToken = mock(SyntaxToken.class);
    when(mockLastToken.line()).thenReturn(7);
    when(mockTree.lastToken()).thenReturn(mockLastToken);
    // with first and last token, line 7 can be excluded
    filter.excludeLines(mockTree);
    assertThatIssueWillBeAccepted(7).isFalse();
}
Also used : SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Test(org.junit.Test)

Example 45 with SyntaxToken

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

the class AnalyzerMessage method textSpanFor.

public static AnalyzerMessage.TextSpan textSpanFor(Tree syntaxNode) {
    SyntaxToken firstSyntaxToken = syntaxNode.firstToken();
    SyntaxToken lastSyntaxToken = syntaxNode.lastToken();
    return textSpanBetween(firstSyntaxToken, lastSyntaxToken);
}
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