Search in sources :

Example 56 with Symbol

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;
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 57 with Symbol

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);
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree)

Example 58 with 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);
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 59 with 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;
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) JavaType(org.sonar.java.resolve.JavaType) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 60 with Symbol

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;
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Aggregations

Symbol (org.sonar.plugins.java.api.semantic.Symbol)140 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)47 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)41 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)33 Tree (org.sonar.plugins.java.api.tree.Tree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)28 JavaSymbol (org.sonar.java.resolve.JavaSymbol)27 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)26 Type (org.sonar.plugins.java.api.semantic.Type)24 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)24 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)23 List (java.util.List)19 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)17 Collectors (java.util.stream.Collectors)14 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)13 Set (java.util.Set)12 ImmutableList (com.google.common.collect.ImmutableList)11 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)11