use of org.sonar.plugins.java.api.tree.ExpressionTree 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;
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class UselessIncrementCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
if (tree.is(Tree.Kind.RETURN_STATEMENT)) {
ExpressionTree returnExpression = ((ReturnStatementTree) tree).expression();
if (returnExpression != null && isPostfix(returnExpression)) {
UnaryExpressionTree unaryExpression = (UnaryExpressionTree) returnExpression;
ExpressionTree expression = ExpressionUtils.skipParentheses(unaryExpression.expression());
if (expression.is(Tree.Kind.IDENTIFIER) && ((IdentifierTree) expression).symbol().owner().isMethodSymbol()) {
reportIssue(unaryExpression);
}
}
} else {
AssignmentExpressionTree aet = (AssignmentExpressionTree) tree;
if (isPostfix(aet.expression())) {
UnaryExpressionTree postfix = (UnaryExpressionTree) aet.expression();
if (SyntacticEquivalence.areEquivalent(aet.variable(), postfix.expression())) {
reportIssue(postfix);
}
}
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class SerializableFieldInSerializableClassCheck method checkCollectionAssignments.
private void checkCollectionAssignments(List<IdentifierTree> usages) {
for (IdentifierTree usage : usages) {
Tree parentTree = usage.parent();
if (parentTree.is(Tree.Kind.ASSIGNMENT)) {
AssignmentExpressionTree assignment = (AssignmentExpressionTree) parentTree;
ExpressionTree expression = assignment.expression();
if (usage.equals(assignment.variable()) && !expression.is(Tree.Kind.NULL_LITERAL) && isUnserializableCollection(expression.symbolType())) {
reportIssue(usage);
}
}
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class ExplodedGraphWalker method executeLogicalAssignment.
private void executeLogicalAssignment(AssignmentExpressionTree tree) {
ExpressionTree variable = tree.variable();
// FIXME handle also assignments with this SONARJAVA-2242
if (variable.is(Tree.Kind.IDENTIFIER)) {
ProgramState.Pop unstack = programState.unstackValue(2);
ProgramState.SymbolicValueSymbol assignedTo = unstack.valuesAndSymbols.get(1);
ProgramState.SymbolicValueSymbol value = unstack.valuesAndSymbols.get(0);
programState = unstack.state;
SymbolicValue symbolicValue = constraintManager.createBinarySymbolicValue(tree, ImmutableList.of(assignedTo, value));
Symbol symbol = ((IdentifierTree) variable).symbol();
programState = programState.stackValue(symbolicValue, symbol);
programState = programState.put(symbol, symbolicValue);
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method addConstantValue.
private static void addConstantValue(AnnotationTree tree, AnnotationInstanceResolve annotationInstance) {
Collection<Symbol> scopeSymbols = tree.annotationType().symbolType().symbol().memberSymbols();
for (ExpressionTree expressionTree : tree.arguments()) {
String name = "";
for (Symbol scopeSymbol : scopeSymbols) {
if (scopeSymbol.isMethodSymbol()) {
name = scopeSymbol.name();
break;
}
}
annotationInstance.addValue(new AnnotationValueResolve(name, expressionTree));
}
}
Aggregations