Search in sources :

Example 11 with AssignmentExpressionTree

use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree 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);
    }
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 12 with AssignmentExpressionTree

use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.

the class TypeAndReferenceSolver method completeMetadata.

private static void completeMetadata(JavaSymbol symbol, List<AnnotationTree> annotations) {
    for (AnnotationTree tree : annotations) {
        AnnotationInstanceResolve annotationInstance = new AnnotationInstanceResolve((JavaSymbol.TypeJavaSymbol) tree.symbolType().symbol());
        symbol.metadata().addAnnotation(annotationInstance);
        Arguments arguments = tree.arguments();
        if (arguments.size() > 1 || (!arguments.isEmpty() && arguments.get(0).is(Tree.Kind.ASSIGNMENT))) {
            for (ExpressionTree expressionTree : arguments) {
                AssignmentExpressionTree aet = (AssignmentExpressionTree) expressionTree;
                // TODO: Store more precise value than the expression (real value in case of literal, symbol for enums, array of values, solve constants?)
                annotationInstance.addValue(new AnnotationValueResolve(((IdentifierTree) aet.variable()).name(), aet.expression()));
            }
        } else {
            // Constant
            addConstantValue(tree, annotationInstance);
        }
    }
}
Also used : Arguments(org.sonar.plugins.java.api.tree.Arguments) TypeArguments(org.sonar.plugins.java.api.tree.TypeArguments) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree)

Example 13 with AssignmentExpressionTree

use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.

the class TypeAndReferenceSolver method visitAnnotation.

@Override
public void visitAnnotation(AnnotationTree tree) {
    if (((AbstractTypedTree) tree.annotationType()).isTypeSet()) {
        // This handle the case where type and its annotation is shared between two variables.
        return;
    }
    resolveAs(tree.annotationType(), JavaSymbol.TYP);
    Arguments arguments = tree.arguments();
    if (arguments.size() > 1 || (!arguments.isEmpty() && arguments.get(0).is(Tree.Kind.ASSIGNMENT))) {
        // resolve by identifying correct identifier in assignment.
        for (ExpressionTree expressionTree : arguments) {
            AssignmentExpressionTree aet = (AssignmentExpressionTree) expressionTree;
            IdentifierTree variable = (IdentifierTree) aet.variable();
            JavaSymbol identInType = resolve.findMethod(semanticModel.getEnv(tree), getType(tree.annotationType()), variable.name(), ImmutableList.<JavaType>of()).symbol();
            associateReference(variable, identInType);
            JavaType type = identInType.type;
            if (type == null) {
                type = Symbols.unknownType;
            }
            registerType(variable, type);
            resolveAs(aet.expression(), JavaSymbol.VAR);
        }
    } else {
        for (ExpressionTree expressionTree : arguments) {
            resolveAs(expressionTree, JavaSymbol.VAR);
        }
    }
    registerType(tree, getType(tree.annotationType()));
}
Also used : Arguments(org.sonar.plugins.java.api.tree.Arguments) TypeArguments(org.sonar.plugins.java.api.tree.TypeArguments) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AbstractTypedTree(org.sonar.java.model.AbstractTypedTree)

Example 14 with AssignmentExpressionTree

use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.

the class PublicStaticMutableMembersCheck method checkAssignment.

private void checkAssignment(AssignmentExpressionTree node) {
    ExpressionTree variable = ExpressionUtils.skipParentheses(node.variable());
    if (variable.is(Tree.Kind.MEMBER_SELECT)) {
        variable = ((MemberSelectExpressionTree) variable).identifier();
    }
    if (variable.is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree identifierTree = (IdentifierTree) variable;
        Symbol symbol = identifierTree.symbol();
        if (IMMUTABLE_CANDIDATES.contains(symbol) && isMutable(node.expression(), symbol)) {
            reportIssue(identifierTree, "Make member \"" + symbol.name() + "\" \"protected\".");
            IMMUTABLE_CANDIDATES.remove(symbol);
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 15 with AssignmentExpressionTree

use of org.sonar.plugins.java.api.tree.AssignmentExpressionTree in project sonar-java by SonarSource.

the class MutableMembersUsageCheck method visitAssignmentExpression.

@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
    super.visitAssignmentExpression(tree);
    if (!isMutableType(tree.expression())) {
        return;
    }
    ExpressionTree variable = tree.variable();
    Symbol leftSymbol = null;
    if (variable.is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree identifierTree = (IdentifierTree) variable;
        leftSymbol = identifierTree.symbol();
    } else if (variable.is(Tree.Kind.MEMBER_SELECT)) {
        MemberSelectExpressionTree mit = (MemberSelectExpressionTree) variable;
        leftSymbol = mit.identifier().symbol();
    }
    if (leftSymbol != null && leftSymbol.isPrivate()) {
        checkStore(tree.expression());
    }
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Aggregations

AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)34 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)28 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)18 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)13 Symbol (org.sonar.plugins.java.api.semantic.Symbol)11 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)11 Tree (org.sonar.plugins.java.api.tree.Tree)9 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)9 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)8 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)6 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)6 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)5 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)5 SyntaxToken (org.sonar.plugins.java.api.tree.SyntaxToken)5 Test (org.junit.Test)4 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)4 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)4 CheckForNull (javax.annotation.CheckForNull)3 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)3 File (java.io.File)2