use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class AnnotationArgumentOrderCheck method visitNode.
@Override
public void visitNode(Tree tree) {
AnnotationTree annotationTree = (AnnotationTree) tree;
TypeSymbol annotationSymbol = annotationTree.symbolType().symbol();
if (annotationSymbol.isUnknown()) {
return;
}
List<String> declarationNames = new ArrayList<>();
for (Symbol symbol : annotationSymbol.memberSymbols()) {
declarationNames.add(symbol.name());
}
List<String> annotationArguments = new ArrayList<>();
for (ExpressionTree argument : annotationTree.arguments()) {
if (argument.is(Tree.Kind.ASSIGNMENT)) {
AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) argument;
IdentifierTree nameTree = (IdentifierTree) assignmentTree.variable();
annotationArguments.add(nameTree.name());
}
}
declarationNames.retainAll(annotationArguments);
if (!declarationNames.equals(annotationArguments)) {
reportIssue(annotationTree.annotationType(), "Reorder annotation arguments to match the order of declaration.");
}
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class CastArithmeticOperandCheck method visitAssignmentExpression.
@Override
public void visitAssignmentExpression(AssignmentExpressionTree aet) {
if (aet.is(Tree.Kind.ASSIGNMENT)) {
Type varType = aet.symbolType();
ExpressionTree expr = aet.expression();
checkExpression(varType, expr);
}
super.visitAssignmentExpression(aet);
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class AssignmentInSubExpressionCheck method skipChainedAssignments.
private ExpressionTree skipChainedAssignments(ExpressionTree expressionTree) {
ExpressionTree tree = ExpressionUtils.skipParentheses(expressionTree);
while (tree instanceof AssignmentExpressionTree) {
AssignmentExpressionTree assignmentExpressionTree = (AssignmentExpressionTree) tree;
scan(assignmentExpressionTree.variable());
tree = ExpressionUtils.skipParentheses(assignmentExpressionTree.expression());
}
return tree;
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class ExpressionUtilsTest method test_simple_assignments.
@Test
public void test_simple_assignments() throws Exception {
File file = new File("src/test/java/org/sonar/java/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);
assertThat(assignments).hasSize(4);
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(0))).isTrue();
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(1))).isTrue();
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(2))).isFalse();
assertThat(ExpressionUtils.isSimpleAssignment(assignments.get(3))).isFalse();
}
use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.
the class ReassignmentFinder method getClosestReassignmentOrDeclarationExpression.
@CheckForNull
public static ExpressionTree getClosestReassignmentOrDeclarationExpression(Tree startingPoint, Symbol referenceSymbol) {
Tree result = referenceSymbol.declaration();
List<IdentifierTree> usages = referenceSymbol.usages();
if (usages.size() != 1) {
List<AssignmentExpressionTree> reassignments = getReassignments(referenceSymbol.owner().declaration(), usages);
SyntaxToken startPointToken = startingPoint.firstToken();
Tree lastReassignment = getClosestReassignment(startPointToken, reassignments);
if (lastReassignment != null) {
result = lastReassignment;
}
}
ExpressionTree initializerOrExpression = getInitializerOrExpression(result);
if (initializerOrExpression == startingPoint) {
return getClosestReassignmentOrDeclarationExpression(result, referenceSymbol);
}
return initializerOrExpression;
}
Aggregations