use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class ExpressionUtilsTest method test_extract_identifier_mixed_access.
@Test
public void test_extract_identifier_mixed_access() throws Exception {
File file = new File("src/test/files/model/ExpressionUtilsTest.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
MethodTree methodTree = (MethodTree) ((ClassTree) tree.types().get(0)).members().get(1);
List<AssignmentExpressionTree> assignments = findAssignmentExpressionTrees(methodTree);
// This should reflect method 'mixedReference'.
assertThat(assignments).hasSize(4);
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(0))).isTrue();
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(1))).isTrue();
// Contains method invocation.
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(2))).isFalse();
// Compound assignment
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(2))).isFalse();
// The returned identifier should have the same symbol regardless of the explicit usage of this.
assertThat(ExpressionUtils.extractIdentifier(assignments.get(0)).symbol()).isEqualTo(ExpressionUtils.extractIdentifier(assignments.get(1)).symbol());
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class ImmediateReverseBoxingCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
visitMethodInvocationTree((MethodInvocationTree) tree);
} else if (tree.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) tree;
ExpressionTree initializer = variableTree.initializer();
if (initializer != null) {
checkExpression(initializer, variableTree.type().symbolType());
}
} else if (tree.is(Tree.Kind.ASSIGNMENT)) {
AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) tree;
checkExpression(assignmentTree.expression(), assignmentTree.symbolType());
} else {
NewClassTree newClassTree = (NewClassTree) tree;
Symbol.TypeSymbol classSymbol = wrapperClassSymbol(newClassTree);
if (classSymbol != null) {
ExpressionTree arg0 = newClassTree.arguments().get(0);
checkForUnboxing(arg0);
checkForUselessUnboxing(newClassTree.symbolType(), newClassTree.identifier(), arg0);
}
}
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class UCFGJavaVisitor method buildCall.
private void buildCall(Tree element, UCFGBuilder.BlockBuilder blockBuilder, IdentifierGenerator idGenerator) {
if (isStringVarDecl(element)) {
VariableTree variableTree = (VariableTree) element;
String lhs = idGenerator.lookupIdFor(variableTree.simpleName());
if (!idGenerator.isConst(lhs)) {
ExpressionTree initializer = variableTree.initializer();
String source = idGenerator.lookupIdFor(initializer);
blockBuilder.assignTo(variableWithId(lhs), UCFGBuilder.call("__id").withArgs(variableWithId(source)), location(element));
}
return;
}
if (element.is(METHOD_INVOCATION)) {
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) element;
buildMethodInvocation(blockBuilder, idGenerator, methodInvocationTree);
} else if (element.is(PLUS, PLUS_ASSIGNMENT, ASSIGNMENT) && isString(((ExpressionTree) element).symbolType())) {
if (element.is(PLUS)) {
BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) element;
Expression lhs = idGenerator.lookupExpressionFor(binaryExpressionTree.leftOperand());
Expression rhs = idGenerator.lookupExpressionFor(binaryExpressionTree.rightOperand());
Expression.Variable var = variableWithId(idGenerator.newIdFor(binaryExpressionTree));
blockBuilder.assignTo(var, call("__concat").withArgs(lhs, rhs), location(element));
} else if (element.is(PLUS_ASSIGNMENT)) {
Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
if (!var.isConstant()) {
idGenerator.varForExpression(element, ((Expression.Variable) var).id());
blockBuilder.assignTo((Expression.Variable) var, call("__concat").withArgs(var, expr), location(element));
}
} else if (element.is(ASSIGNMENT)) {
Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
if (!var.isConstant()) {
blockBuilder.assignTo((Expression.Variable) var, call("__id").withArgs(expr), location(element));
}
}
}
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class SyntaxTreeNameFinderTest method testFieldAccess.
@Test
public void testFieldAccess() {
MethodTree tree = buildSyntaxTree("public void test() {this.field = value;} Object field;");
BlockTree block = tree.block();
AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("field");
tree = buildSyntaxTree("public void test() {super.field = value;}");
block = tree.block();
assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("field");
tree = buildSyntaxTree("public void test() {A.field = value;}");
block = tree.block();
assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("A");
tree = buildSyntaxTree("public void test() {foo().field = value;}");
block = tree.block();
assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("foo");
}
Aggregations