use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class DeadStoreCheck method checkElement.
private Set<Symbol> checkElement(Symbol.MethodSymbol methodSymbol, Set<Symbol> outVar, Set<Tree> assignmentLHS, Tree element) {
Set<Symbol> out = outVar;
switch(element.kind()) {
case PLUS_ASSIGNMENT:
case DIVIDE_ASSIGNMENT:
case MINUS_ASSIGNMENT:
case MULTIPLY_ASSIGNMENT:
case OR_ASSIGNMENT:
case XOR_ASSIGNMENT:
case AND_ASSIGNMENT:
case LEFT_SHIFT_ASSIGNMENT:
case RIGHT_SHIFT_ASSIGNMENT:
case UNSIGNED_RIGHT_SHIFT_ASSIGNMENT:
case REMAINDER_ASSIGNMENT:
case ASSIGNMENT:
handleAssignment(out, assignmentLHS, (AssignmentExpressionTree) element);
break;
case IDENTIFIER:
handleIdentifier(out, assignmentLHS, (IdentifierTree) element);
break;
case VARIABLE:
handleVariable(out, (VariableTree) element);
break;
case NEW_CLASS:
handleNewClass(out, methodSymbol, (NewClassTree) element);
break;
case LAMBDA_EXPRESSION:
LambdaExpressionTree lambda = (LambdaExpressionTree) element;
out.addAll(getUsedLocalVarInSubTree(lambda.body(), methodSymbol));
break;
case METHOD_REFERENCE:
MethodReferenceTree methodRef = (MethodReferenceTree) element;
out.addAll(getUsedLocalVarInSubTree(methodRef.expression(), methodSymbol));
break;
case TRY_STATEMENT:
handleTryStatement(out, methodSymbol, (TryStatementTree) element);
break;
case PREFIX_DECREMENT:
case PREFIX_INCREMENT:
handlePrefixExpression(out, (UnaryExpressionTree) element);
break;
case POSTFIX_INCREMENT:
case POSTFIX_DECREMENT:
handlePostfixExpression(out, (UnaryExpressionTree) element);
break;
case CLASS:
case ENUM:
case ANNOTATION_TYPE:
case INTERFACE:
ClassTree classTree = (ClassTree) element;
out.addAll(getUsedLocalVarInSubTree(classTree, methodSymbol));
break;
default:
}
return out;
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class DeadStoreCheck method handleVariable.
private void handleVariable(Set<Symbol> out, VariableTree localVar) {
Symbol symbol = localVar.symbol();
ExpressionTree initializer = localVar.initializer();
if (initializer != null && !isUsualDefaultValue(initializer) && !out.contains(symbol)) {
createIssue(localVar.equalToken(), initializer, symbol);
}
out.remove(symbol);
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class DeadStoreCheck method handleAssignment.
private void handleAssignment(Set<Symbol> out, Set<Tree> assignmentLHS, AssignmentExpressionTree element) {
ExpressionTree lhs = ExpressionUtils.skipParentheses(element.variable());
if (lhs.is(Tree.Kind.IDENTIFIER)) {
Symbol symbol = ((IdentifierTree) lhs).symbol();
if (isLocalVariable(symbol) && !out.contains(symbol) && (element.is(Tree.Kind.ASSIGNMENT) || isParentExpressionStatement(element))) {
createIssue(element.operatorToken(), element.expression(), symbol);
}
assignmentLHS.add(lhs);
if (element.is(Tree.Kind.ASSIGNMENT)) {
out.remove(symbol);
} else {
out.add(symbol);
}
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class CompareObjectWithEqualsCheck method parentClassImplementsEquals.
private static boolean parentClassImplementsEquals(Symbol.TypeSymbol symbol) {
Type superClass = symbol.superClass();
while (superClass != null && superClass.symbol().isTypeSymbol()) {
Symbol.TypeSymbol superClassSymbol = superClass.symbol();
if (!superClass.is(JAVA_LANG_OBJECT) && hasEqualsMethod(superClassSymbol)) {
return true;
}
superClass = superClassSymbol.superClass();
}
return false;
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class CompareToResultTestCheck method isIdentifierContainingCompareToResult.
private static boolean isIdentifierContainingCompareToResult(IdentifierTree identifier) {
Symbol variableSymbol = identifier.symbol();
if (!variableSymbol.isVariableSymbol()) {
return false;
}
VariableTree variableDefinition = ((Symbol.VariableSymbol) variableSymbol).declaration();
if (variableDefinition != null) {
ExpressionTree initializer = variableDefinition.initializer();
if (initializer != null && initializer.is(Tree.Kind.METHOD_INVOCATION) && variableSymbol.owner().isMethodSymbol()) {
MethodTree method = ((Symbol.MethodSymbol) variableSymbol.owner()).declaration();
return method != null && COMPARE_TO.matches((MethodInvocationTree) initializer) && !isReassigned(variableSymbol, method);
}
}
return false;
}
Aggregations