Search in sources :

Example 41 with MemberSelectExpressionTree

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

the class TypeAndReferenceSolver method resolveMethodSymbol.

@CheckForNull
private Resolve.Resolution resolveMethodSymbol(Tree methodSelect, Resolve.Env methodEnv, List<JavaType> argTypes, List<JavaType> typeParamTypes) {
    Resolve.Resolution resolution;
    IdentifierTree identifier;
    if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
        MemberSelectExpressionTree mset = (MemberSelectExpressionTree) methodSelect;
        resolveAs(mset.expression(), JavaSymbol.TYP | JavaSymbol.VAR);
        JavaType type = getType(mset.expression());
        if (type.isTagged(JavaType.DEFERRED)) {
            return null;
        }
        identifier = mset.identifier();
        resolution = resolve.findMethod(methodEnv, type, identifier.name(), argTypes, typeParamTypes);
    } else if (methodSelect.is(Tree.Kind.IDENTIFIER)) {
        identifier = (IdentifierTree) methodSelect;
        resolution = resolve.findMethod(methodEnv, identifier.name(), argTypes, typeParamTypes);
    } else {
        throw new IllegalStateException("Method select in method invocation is not of the expected type " + methodSelect);
    }
    registerType(identifier, resolution.type());
    associateReference(identifier, resolution.symbol());
    return resolution;
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Resolution(org.sonar.java.resolve.Resolve.Resolution) CheckForNull(javax.annotation.CheckForNull)

Example 42 with MemberSelectExpressionTree

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

the class TypeAndReferenceSolver method inferReturnTypeFromInferedArgs.

private void inferReturnTypeFromInferedArgs(MethodInvocationTree tree, Resolve.Env methodEnv, List<JavaType> argTypes, List<JavaType> typeParamTypes, JavaType returnType, TypeSubstitution typeSubstitution) {
    List<JavaType> parameterTypes = getParameterTypes(tree.arguments());
    if (!parameterTypes.equals(argTypes)) {
        IdentifierTree identifier = null;
        Resolution resolution = null;
        Tree methodSelect = tree.methodSelect();
        if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
            MemberSelectExpressionTree mset = (MemberSelectExpressionTree) methodSelect;
            JavaType type = getType(mset.expression());
            if (type.isTagged(JavaType.DEFERRED)) {
                throw new IllegalStateException("type of arg should not be defered anymore ??");
            }
            identifier = mset.identifier();
            resolution = resolve.findMethod(methodEnv, type, identifier.name(), parameterTypes, typeParamTypes);
        } else if (methodSelect.is(Tree.Kind.IDENTIFIER)) {
            identifier = (IdentifierTree) methodSelect;
            resolution = resolve.findMethod(methodEnv, identifier.name(), parameterTypes, typeParamTypes);
        }
        if (resolution != null && returnType != resolution.type() && resolution.symbol().isMethodSymbol()) {
            MethodJavaType methodType = (MethodJavaType) resolution.type();
            if (!methodType.resultType.isTagged(JavaType.DEFERRED)) {
                registerType(tree, resolve.applySubstitution(methodType.resultType, typeSubstitution));
                // update type associated to identifier as it may have been inferred deeper when re-resolving method with new parameter types
                registerType(identifier, methodType);
            }
        }
    } else {
        registerType(tree, resolve.applySubstitution(returnType, typeSubstitution));
    }
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AbstractTypedTree(org.sonar.java.model.AbstractTypedTree) UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ContinueStatementTree(org.sonar.plugins.java.api.tree.ContinueStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) WildcardTree(org.sonar.plugins.java.api.tree.WildcardTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) BreakStatementTree(org.sonar.plugins.java.api.tree.BreakStatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) EnumConstantTree(org.sonar.plugins.java.api.tree.EnumConstantTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ImportTree(org.sonar.plugins.java.api.tree.ImportTree) PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) InstanceOfTree(org.sonar.plugins.java.api.tree.InstanceOfTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Resolution(org.sonar.java.resolve.Resolve.Resolution)

Example 43 with MemberSelectExpressionTree

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

the class ExceptionalYieldChecker method reportIssue.

private void reportIssue(ExplodedGraph.Node node, ExceptionalCheckBasedYield yield, SECheck check) {
    MethodInvocationTree mit = (MethodInvocationTree) node.programPoint.syntaxTree();
    ExpressionTree methodSelect = mit.methodSelect();
    String methodName = mit.symbol().name();
    Tree reportTree = methodSelect;
    if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
        reportTree = ((MemberSelectExpressionTree) methodSelect).identifier();
    }
    JavaFileScannerContext.Location methodInvocationMessage;
    int parameterCausingExceptionIndex = yield.parameterCausingExceptionIndex();
    IdentifierTree identifierTree = FlowComputation.getArgumentIdentifier(mit, parameterCausingExceptionIndex);
    if (identifierTree != null) {
        methodInvocationMessage = new JavaFileScannerContext.Location(String.format("'%s' is passed to '%s()'.", identifierTree.name(), methodName), identifierTree);
    } else {
        methodInvocationMessage = new JavaFileScannerContext.Location(String.format("'%s()' is invoked.", methodName), reportTree);
    }
    Flow argumentChangingNameFlows = flowsForArgumentsChangingName(yield, mit);
    Set<Flow> argumentsFlows = flowsForMethodArguments(node, mit, parameterCausingExceptionIndex);
    Set<Flow> exceptionFlows = yield.exceptionFlows();
    ImmutableSet.Builder<Flow> flows = ImmutableSet.builder();
    for (Flow argumentFlow : argumentsFlows) {
        for (Flow exceptionFlow : exceptionFlows) {
            flows.add(Flow.builder().addAll(exceptionFlow).addAll(argumentChangingNameFlows).add(methodInvocationMessage).addAll(argumentFlow).build());
        }
    }
    check.reportIssue(reportTree, String.format(message, methodName), flows.build());
}
Also used : JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ImmutableSet(com.google.common.collect.ImmutableSet) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) Constraint(org.sonar.java.se.constraint.Constraint) Flow(org.sonar.java.se.Flow)

Example 44 with MemberSelectExpressionTree

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

the class NullDereferenceCheck method reportIssue.

private void reportIssue(SymbolicValue currentVal, Tree syntaxNode, ExplodedGraph.Node node) {
    String message = "A \"NullPointerException\" could be thrown; ";
    if (syntaxNode.is(Tree.Kind.MEMBER_SELECT) && ((MemberSelectExpressionTree) syntaxNode).expression().is(Tree.Kind.METHOD_INVOCATION)) {
        message += "\"" + SyntaxTreeNameFinder.getName(syntaxNode) + "()\" can return null.";
    } else {
        message += "\"" + SyntaxTreeNameFinder.getName(syntaxNode) + "\" is nullable here.";
    }
    SymbolicValue val = null;
    if (!SymbolicValue.NULL_LITERAL.equals(currentVal)) {
        val = currentVal;
    }
    Symbol dereferencedSymbol = dereferencedSymbol(syntaxNode);
    Set<Flow> flows = FlowComputation.flow(node, val, Lists.newArrayList(ObjectConstraint.class), dereferencedSymbol).stream().filter(f -> !f.isEmpty()).map(f -> addDereferenceMessage(f, syntaxNode)).collect(Collectors.toSet());
    reportIssue(syntaxNode, message, flows);
}
Also used : ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) ProgramState(org.sonar.java.se.ProgramState) Deque(java.util.Deque) CheckerContext(org.sonar.java.se.CheckerContext) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConstraintManager(org.sonar.java.se.constraint.ConstraintManager) Lists(com.google.common.collect.Lists) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Nullable(javax.annotation.Nullable) Set(java.util.Set) ExplodedGraph(org.sonar.java.se.ExplodedGraph) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) List(java.util.List) CFG(org.sonar.java.cfg.CFG) Flow(org.sonar.java.se.Flow) FlowComputation(org.sonar.java.se.FlowComputation) Preconditions(com.google.common.base.Preconditions) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ArrayDeque(java.util.ArrayDeque) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Flow(org.sonar.java.se.Flow)

Example 45 with MemberSelectExpressionTree

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

the class NullDereferenceCheck method checkPreStatement.

@Override
public ProgramState checkPreStatement(CheckerContext context, Tree syntaxNode) {
    if (context.getState().peekValue() == null) {
        // stack is empty, nothing to do.
        return context.getState();
    }
    if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree methodInvocation = (MethodInvocationTree) syntaxNode;
        Tree methodSelect = methodInvocation.methodSelect();
        if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
            SymbolicValue dereferencedSV = context.getState().peekValue(methodInvocation.arguments().size());
            return checkConstraint(context, methodSelect, dereferencedSV);
        }
    }
    if (syntaxNode.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
        Tree toCheck = ((ArrayAccessExpressionTree) syntaxNode).expression();
        SymbolicValue currentVal = context.getState().peekValue(1);
        return checkConstraint(context, toCheck, currentVal);
    }
    if (syntaxNode.is(Tree.Kind.MEMBER_SELECT)) {
        return checkMemberSelect(context, (MemberSelectExpressionTree) syntaxNode, context.getState().peekValue());
    }
    return context.getState();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Aggregations

MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)60 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)36 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)36 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)17 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)14 Symbol (org.sonar.plugins.java.api.semantic.Symbol)13 Test (org.junit.Test)12 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)10 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)8 Tree (org.sonar.plugins.java.api.tree.Tree)7 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)6 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)5 CheckForNull (javax.annotation.CheckForNull)4 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)4 LiteralTree (org.sonar.plugins.java.api.tree.LiteralTree)4 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)4 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)4 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)4 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)4 Resolution (org.sonar.java.resolve.Resolve.Resolution)3