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);
}
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);
}
}
}
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);
}
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;
}
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<>();
}
Aggregations