use of org.sonar.plugins.java.api.tree.SyntaxToken in project sonar-java by SonarSource.
the class SillyBitOperationCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ExpressionTree expression;
SyntaxToken operatorToken;
if (tree.is(Kind.OR, Kind.XOR, Kind.AND)) {
BinaryExpressionTree binary = (BinaryExpressionTree) tree;
expression = binary.rightOperand();
operatorToken = binary.operatorToken();
} else {
AssignmentExpressionTree assignment = (AssignmentExpressionTree) tree;
expression = assignment.expression();
operatorToken = assignment.operatorToken();
}
Long evaluatedExpression = LiteralUtils.longLiteralValue(expression);
if (evaluatedExpression != null && getBitwiseOperationIdentityElement(tree).equals(evaluatedExpression)) {
reportIssue(operatorToken, "Remove this silly bit operation.");
}
}
use of org.sonar.plugins.java.api.tree.SyntaxToken in project sonar-java by SonarSource.
the class RightCurlyBraceToNextBlockAbstractVisitor method visitNode.
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.IF_STATEMENT)) {
IfStatementTree ifStatementTree = (IfStatementTree) tree;
StatementTree thenStatement = ifStatementTree.thenStatement();
if (ifStatementTree.elseKeyword() != null && thenStatement.is(Tree.Kind.BLOCK)) {
checkTokenPosition(ifStatementTree.elseKeyword(), (BlockTree) thenStatement);
}
} else {
TryStatementTree tryStatementTree = (TryStatementTree) tree;
BlockTree block = tryStatementTree.block();
for (CatchTree catchTree : tryStatementTree.catches()) {
checkTokenPosition(catchTree.catchKeyword(), block);
block = catchTree.block();
}
SyntaxToken finallyKeyword = tryStatementTree.finallyKeyword();
if (finallyKeyword != null) {
checkTokenPosition(finallyKeyword, block);
}
}
}
use of org.sonar.plugins.java.api.tree.SyntaxToken in project sonar-java by SonarSource.
the class ShiftOnIntOrLongCheck method checkShiftTree.
private void checkShiftTree(Tree tree, int treeIndex) {
String identifier;
ExpressionTree shift;
SyntaxToken operatorToken;
if (tree.is(Kind.LEFT_SHIFT, Kind.RIGHT_SHIFT)) {
BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree;
if (isZeroMaskShift(binaryExpressionTree)) {
// No issue should be reported for "1 << 0" or "1 >> 0"
return;
}
identifier = getIdentifierName(binaryExpressionTree.leftOperand());
shift = binaryExpressionTree.rightOperand();
operatorToken = binaryExpressionTree.operatorToken();
} else {
AssignmentExpressionTree assignmentExpressionTree = (AssignmentExpressionTree) tree;
identifier = getIdentifierName(assignmentExpressionTree.variable());
shift = assignmentExpressionTree.expression();
operatorToken = assignmentExpressionTree.operatorToken();
}
checkShift((ExpressionTree) tree, shift, identifier, operatorToken, treeIndex);
}
use of org.sonar.plugins.java.api.tree.SyntaxToken in project sonar-java by SonarSource.
the class ListTreeImplTest method separators_order_in_children_iteration.
@Test
public void separators_order_in_children_iteration() throws Exception {
Tree tree1 = new EmptyStatementTreeImpl(null);
Tree tree2 = new EmptyStatementTreeImpl(null);
Tree tree3 = new EmptyStatementTreeImpl(null);
List<Tree> trees = Lists.newArrayList(tree1, tree2, tree3);
SyntaxToken token1 = createToken("token1");
SyntaxToken token2 = createToken("token2");
List<SyntaxToken> separators = Lists.newArrayList(token1, token2);
ListTreeImpl<Tree> listTree = new MyList(trees, separators);
Iterable<Tree> result = listTree.children();
assertThat(Lists.newArrayList(result)).containsExactly(tree1, token1, tree2, token2, tree3);
}
use of org.sonar.plugins.java.api.tree.SyntaxToken in project sonar-java by SonarSource.
the class JavaTreeModelTest method literal.
@Test
public void literal() {
LiteralTree tree = (LiteralTree) expressionOfReturnStatement("class T { int m() { return 1; } }");
assertThat(tree.is(Tree.Kind.INT_LITERAL)).isTrue();
assertThat(tree.value()).isEqualTo("1");
assertThatChildrenIteratorHasSize(tree, 1);
SyntaxToken token = tree.token();
assertThat(token).isNotNull();
assertThat(token.line()).isEqualTo(1);
assertThat(token.column()).isEqualTo(27);
tree = (LiteralTree) expressionOfReturnStatement("class T { long m() { return 1L; } }");
assertThat(tree.is(Tree.Kind.LONG_LITERAL)).isTrue();
assertThat(tree.value()).isEqualTo("1L");
assertThatChildrenIteratorHasSize(tree, 1);
token = tree.token();
assertThat(token).isNotNull();
assertThat(token.line()).isEqualTo(1);
assertThat(token.column()).isEqualTo(28);
tree = (LiteralTree) expressionOfReturnStatement("class T { float m() { return 1F; } }");
assertThat(tree.is(Tree.Kind.FLOAT_LITERAL)).isTrue();
assertThat(tree.value()).isEqualTo("1F");
assertThatChildrenIteratorHasSize(tree, 1);
token = tree.token();
assertThat(token).isNotNull();
assertThat(token.line()).isEqualTo(1);
assertThat(token.column()).isEqualTo(29);
tree = (LiteralTree) expressionOfReturnStatement("class T { double m() { return 1d; } }");
assertThat(tree.is(Tree.Kind.DOUBLE_LITERAL)).isTrue();
assertThat(tree.value()).isEqualTo("1d");
assertThatChildrenIteratorHasSize(tree, 1);
token = tree.token();
assertThat(token).isNotNull();
assertThat(token.line()).isEqualTo(1);
assertThat(token.column()).isEqualTo(30);
tree = (LiteralTree) expressionOfReturnStatement("class T { boolean m() { return true; } }");
assertThat(tree.is(Tree.Kind.BOOLEAN_LITERAL)).isTrue();
assertThat(tree.value()).isEqualTo("true");
assertThatChildrenIteratorHasSize(tree, 1);
token = tree.token();
assertThat(token).isNotNull();
assertThat(token.line()).isEqualTo(1);
assertThat(token.column()).isEqualTo(31);
tree = (LiteralTree) expressionOfReturnStatement("class T { boolean m() { return false; } }");
assertThat(tree.is(Tree.Kind.BOOLEAN_LITERAL)).isTrue();
assertThat(tree.value()).isEqualTo("false");
assertThatChildrenIteratorHasSize(tree, 1);
token = tree.token();
assertThat(token).isNotNull();
assertThat(token.line()).isEqualTo(1);
assertThat(token.column()).isEqualTo(31);
tree = (LiteralTree) expressionOfReturnStatement("class T { char m() { return 'c'; } }");
assertThat(tree.is(Tree.Kind.CHAR_LITERAL)).isTrue();
assertThat(tree.value()).isEqualTo("'c'");
assertThatChildrenIteratorHasSize(tree, 1);
token = tree.token();
assertThat(token).isNotNull();
assertThat(token.line()).isEqualTo(1);
assertThat(token.column()).isEqualTo(28);
tree = (LiteralTree) expressionOfReturnStatement("class T { String m() { return \"s\"; } }");
assertThat(tree.is(Tree.Kind.STRING_LITERAL)).isTrue();
assertThat(tree.value()).isEqualTo("\"s\"");
assertThatChildrenIteratorHasSize(tree, 1);
token = tree.token();
assertThat(token).isNotNull();
assertThat(token.line()).isEqualTo(1);
assertThat(token.column()).isEqualTo(30);
tree = (LiteralTree) expressionOfReturnStatement("class T { Object m() { return null; } }");
assertThat(tree.is(Tree.Kind.NULL_LITERAL)).isTrue();
assertThat(tree.value()).isEqualTo("null");
assertThatChildrenIteratorHasSize(tree, 1);
token = tree.token();
assertThat(token).isNotNull();
assertThat(token.line()).isEqualTo(1);
assertThat(token.column()).isEqualTo(30);
}
Aggregations