use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class StringConcatenationInLoopCheck method isNotLoopLocalVar.
private boolean isNotLoopLocalVar(AssignmentExpressionTree tree) {
IdentifierTree idTree = getIdentifierTree(tree.variable());
Tree envTree = semanticModel.getTree(semanticModel.getEnv(idTree.symbol()));
Tree loopTree = loopLevel.peek();
return envTree == null || !(envTree.equals(loopTree) || envTree.equals(loopStatement(loopTree)));
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class StaticFieldInitializationCheck method visitNode.
@Override
public void visitNode(Tree tree) {
switch(tree.kind()) {
case CLASS:
classWithSynchronizedMethod.push(hasSynchronizedMethod((ClassTree) tree));
break;
case STATIC_INITIALIZER:
withinStaticInitializer.push(true);
break;
case METHOD:
methodUsesLocks.push(false);
break;
case METHOD_INVOCATION:
if (locks.anyMatch((MethodInvocationTree) tree) && methodUsesLocks.size() != 1) {
methodUsesLocks.pop();
methodUsesLocks.push(true);
}
break;
case ASSIGNMENT:
AssignmentExpressionTree aet = (AssignmentExpressionTree) tree;
if (hasSemantic() && aet.variable().is(Tree.Kind.IDENTIFIER) && !isInSyncBlock() && !isInStaticInitializer() && !isUsingLock() && isInClassWithSynchronizedMethod()) {
IdentifierTree variable = (IdentifierTree) aet.variable();
if (isStaticNotVolatileObject(variable)) {
reportIssue(variable, "Synchronize this lazy initialization of '" + variable.name() + "'");
}
}
break;
default:
}
super.visitNode(tree);
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree 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.AssignmentExpressionTree 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.AssignmentExpressionTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method assignment_expression.
/**
* 15.26. Assignment Operators
*/
@Test
public void assignment_expression() {
String code = "class T { void m() { a += 42; } }";
AssignmentExpressionTree tree = (AssignmentExpressionTree) ((ExpressionStatementTree) firstMethodFirstStatement(code)).expression();
assertThat(tree.is(Tree.Kind.PLUS_ASSIGNMENT)).isTrue();
assertThat(tree.variable()).isNotNull();
assertThat(tree.operatorToken().text()).isEqualTo("+=");
assertThat(tree.expression()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 3);
}
Aggregations