Search in sources :

Example 31 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree 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 32 with IdentifierTree

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

the class TypeAndReferenceSolver method resolveAs.

public JavaSymbol resolveAs(Tree tree, int kind, Resolve.Env resolveEnv, boolean associateReference) {
    if (tree.is(Tree.Kind.IDENTIFIER, Tree.Kind.MEMBER_SELECT)) {
        JavaSymbol resolvedSymbol;
        IdentifierTree identifierTree;
        if (tree.is(Tree.Kind.MEMBER_SELECT)) {
            MemberSelectExpressionTree mse = (MemberSelectExpressionTree) tree;
            if (JavaKeyword.CLASS.getValue().equals(mse.identifier().name())) {
                // resolve type of expression xxx.class
                return resolveClassType(tree, resolveEnv, mse);
            }
            identifierTree = mse.identifier();
            List<AnnotationTree> identifierAnnotations = identifierTree.annotations();
            scan(identifierAnnotations);
            completeMetadata((JavaSymbol) identifierTree.symbol(), identifierAnnotations);
            Resolve.Resolution res = getSymbolOfMemberSelectExpression(mse, kind, resolveEnv);
            resolvedSymbol = res.symbol();
            JavaType resolvedType = resolve.resolveTypeSubstitution(res.type(), getType(mse.expression()));
            registerType(identifierTree, resolvedType);
            registerType(tree, resolvedType);
        } else {
            identifierTree = (IdentifierTree) tree;
            Resolve.Resolution resolution = resolve.findIdent(resolveEnv, identifierTree.name(), kind);
            resolvedSymbol = resolution.symbol();
            registerType(tree, resolution.type());
        }
        if (associateReference) {
            associateReference(identifierTree, resolvedSymbol);
        }
        return resolvedSymbol;
    }
    tree.accept(this);
    JavaType type = getType(tree);
    if (tree.is(Tree.Kind.INFERED_TYPE)) {
        type = symbols.deferedType((AbstractTypedTree) tree);
        registerType(tree, type);
    }
    if (type == null) {
        throw new IllegalStateException("Type not resolved " + tree);
    }
    return type.symbol;
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) Resolution(org.sonar.java.resolve.Resolve.Resolution) AbstractTypedTree(org.sonar.java.model.AbstractTypedTree)

Example 33 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree 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 34 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree 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 35 with IdentifierTree

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

the class DebugMethodYieldsOnInvocationsCheck method reportYields.

private void reportYields(MethodInvocationTree mit, CheckerDispatcher checkerDispatcher) {
    MethodBehavior mb = checkerDispatcher.peekMethodBehavior((Symbol.MethodSymbol) mit.symbol());
    if (mb != null && mb.isComplete()) {
        IdentifierTree methodName = getIdentifier(mit.methodSelect());
        String message = String.format("Method '%s' has %d method yields.", methodName.name(), mb.yields().size());
        Set<Flow> flow = flowFromYield(mb, methodName);
        reportIssue(methodName, message, flow);
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Flow(org.sonar.java.se.Flow)

Aggregations

IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)142 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)52 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)50 Symbol (org.sonar.plugins.java.api.semantic.Symbol)32 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)29 Tree (org.sonar.plugins.java.api.tree.Tree)27 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)20 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)15 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)10 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)9 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)8 ArrayList (java.util.ArrayList)7 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)7 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7