Search in sources :

Example 1 with SyntaxTrivia

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

the class ValueBasedUtilsTest method getCommentValue.

private static boolean getCommentValue(VariableTree member) {
    SyntaxTrivia trivia = member.firstToken().trivias().get(0);
    String value = trivia.comment().substring(16);
    return Boolean.valueOf(value);
}
Also used : SyntaxTrivia(org.sonar.plugins.java.api.tree.SyntaxTrivia)

Example 2 with SyntaxTrivia

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

the class FileLinesVisitor method visitToken.

@Override
public void visitToken(SyntaxToken syntaxToken) {
    linesOfCode.add(syntaxToken.line());
    for (SyntaxTrivia trivia : syntaxToken.trivias()) {
        int baseLine = trivia.startLine();
        String[] lines = trivia.comment().split("(\r)?\n|\r", -1);
        for (int i = 0; i < lines.length; i++) {
            linesOfComments.add(baseLine + i);
        }
    }
}
Also used : SyntaxTrivia(org.sonar.plugins.java.api.tree.SyntaxTrivia)

Example 3 with SyntaxTrivia

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

the class SymbolicValueFactoryTest method testFactory.

@Test
public void testFactory() {
    final IdentifierTree tree = new IdentifierTreeImpl(new InternalSyntaxToken(1, 1, "id", Collections.<SyntaxTrivia>emptyList(), 0, 0, false));
    final ConstraintManager manager = new ConstraintManager();
    SymbolicValue symbolicValue = manager.createSymbolicValue(tree);
    assertThat(symbolicValue.getClass()).as("Created without factory").isSameAs(SymbolicValue.class);
    manager.setValueFactory(new TestSymbolicValueFactory());
    symbolicValue = manager.createSymbolicValue(tree);
    assertThat(symbolicValue.getClass()).as("Created with factory").isSameAs(TestSymbolicValue.class);
    assertThat(symbolicValue.references(symbolicValue)).isFalse();
    manager.setValueFactory(new TestSymbolicValueFactory());
    try {
        manager.setValueFactory(new TestSymbolicValueFactory());
        fail("Able to add a second factory to the contraints manager");
    } catch (IllegalStateException e) {
        assertThat(e.getMessage()).as("Exception message").isEqualTo("The symbolic value factory has already been defined by another checker!");
    }
    symbolicValue = manager.createSymbolicValue(tree);
    assertThat(symbolicValue.getClass()).as("Created with first factory").isSameAs(TestSymbolicValue.class);
    symbolicValue = manager.createSymbolicValue(tree);
    assertThat(symbolicValue.getClass()).as("Created after factory usage").isSameAs(SymbolicValue.class);
}
Also used : ConstraintManager(org.sonar.java.se.constraint.ConstraintManager) IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) SyntaxTrivia(org.sonar.plugins.java.api.tree.SyntaxTrivia) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Test(org.junit.Test)

Example 4 with SyntaxTrivia

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

the class CommentedOutCodeLineCheck method leaveFile.

/**
 * Detects commented-out code in remaining candidates.
 */
private void leaveFile() {
    List<Integer> commentedOutCodeLines = Lists.newArrayList();
    for (SyntaxTrivia syntaxTrivia : comments) {
        commentedOutCodeLines.addAll(handleCommentsForTrivia(syntaxTrivia));
    }
    // Greedy algorithm to split lines on blocks and to report only one violation per block
    Collections.sort(commentedOutCodeLines);
    int prev = Integer.MIN_VALUE;
    for (Integer commentedOutCodeLine : commentedOutCodeLines) {
        if (prev + 1 < commentedOutCodeLine) {
            addIssue(commentedOutCodeLine, "This block of commented-out lines of code should be removed.");
        }
        prev = commentedOutCodeLine;
    }
    comments = null;
}
Also used : SyntaxTrivia(org.sonar.plugins.java.api.tree.SyntaxTrivia)

Example 5 with SyntaxTrivia

use of org.sonar.plugins.java.api.tree.SyntaxTrivia 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)

Aggregations

SyntaxTrivia (org.sonar.plugins.java.api.tree.SyntaxTrivia)7 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)2 SyntaxToken (org.sonar.plugins.java.api.tree.SyntaxToken)2 Token (com.sonar.sslr.api.Token)1 Trivia (com.sonar.sslr.api.Trivia)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 InternalSyntaxTrivia (org.sonar.java.model.InternalSyntaxTrivia)1 IdentifierTreeImpl (org.sonar.java.model.expression.IdentifierTreeImpl)1 ConstraintManager (org.sonar.java.se.constraint.ConstraintManager)1 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1