Search in sources :

Example 51 with MethodInvocationTree

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

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

the class MapComputeIfAbsentOrPresentCheck method checkPostStatement.

@Override
public ProgramState checkPostStatement(CheckerContext context, Tree syntaxNode) {
    if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) syntaxNode;
        if (MAP_GET.matches(mit)) {
            ProgramState psBeforeInvocation = context.getNode().programState;
            ProgramState psAfterInvocation = context.getState();
            SymbolicValue keySV = psBeforeInvocation.peekValue(0);
            SymbolicValue mapSV = psBeforeInvocation.peekValue(1);
            SymbolicValue valueSV = psAfterInvocation.peekValue();
            mapGetInvocations.put(mapSV, new MapGetInvocation(valueSV, keySV, mit));
        }
    }
    return super.checkPostStatement(context, syntaxNode);
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Example 53 with MethodInvocationTree

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

the class NonNullSetToNullCheck method callsThisConstructor.

private static boolean callsThisConstructor(MethodTree constructor) {
    List<StatementTree> body = constructor.block().body();
    if (body.isEmpty()) {
        return false;
    }
    StatementTree firstStatement = body.get(0);
    if (!firstStatement.is(Tree.Kind.EXPRESSION_STATEMENT)) {
        return false;
    }
    ExpressionTree expression = ((ExpressionStatementTree) firstStatement).expression();
    if (!expression.is(Tree.Kind.METHOD_INVOCATION)) {
        return false;
    }
    ExpressionTree methodSelect = ((MethodInvocationTree) expression).methodSelect();
    return methodSelect.is(Tree.Kind.IDENTIFIER) && "this".equals(((IdentifierTree) methodSelect).name());
}
Also used : ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 54 with MethodInvocationTree

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

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

the class ExceptionalYieldChecker method flowsForMethodArguments.

private static Set<Flow> flowsForMethodArguments(ExplodedGraph.Node node, MethodInvocationTree mit, int parameterCausingExceptionIndex) {
    ProgramState programState = node.programState;
    List<ProgramState.SymbolicValueSymbol> arguments = Lists.reverse(programState.peekValuesAndSymbols(mit.arguments().size()));
    SymbolicValue parameterCausingExceptionSV = arguments.get(parameterCausingExceptionIndex).symbolicValue();
    Set<SymbolicValue> argSymbolicValues = new LinkedHashSet<>();
    Set<Symbol> argSymbols = new LinkedHashSet<>();
    arguments.stream().filter(svs -> parameterCausingExceptionSV == svs.symbolicValue() || hasConstraintOtherThanNonNull(svs, programState)).forEach(svs -> {
        argSymbolicValues.add(svs.symbolicValue());
        Symbol symbol = svs.symbol();
        if (symbol != null) {
            argSymbols.add(symbol);
        }
    });
    List<Class<? extends Constraint>> domains = domainsFromArguments(programState, argSymbolicValues);
    return FlowComputation.flow(node, argSymbolicValues, c -> true, c -> false, domains, argSymbols);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) ProgramState(org.sonar.java.se.ProgramState) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) Lists(com.google.common.collect.Lists) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) LinkedHashSet(java.util.LinkedHashSet) ExceptionalCheckBasedYield(org.sonar.java.se.xproc.ExceptionalCheckBasedYield) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) Set(java.util.Set) ExplodedGraph(org.sonar.java.se.ExplodedGraph) Tree(org.sonar.plugins.java.api.tree.Tree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Objects(java.util.Objects) List(java.util.List) Flow(org.sonar.java.se.Flow) FlowComputation(org.sonar.java.se.FlowComputation) Constraint(org.sonar.java.se.constraint.Constraint) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Collections(java.util.Collections) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) Constraint(org.sonar.java.se.constraint.Constraint) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Aggregations

MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)87 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)44 Test (org.junit.Test)30 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)30 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)29 Symbol (org.sonar.plugins.java.api.semantic.Symbol)26 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 Tree (org.sonar.plugins.java.api.tree.Tree)21 Type (org.sonar.plugins.java.api.semantic.Type)14 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)14 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)14 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)13 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)11 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)10 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)10 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)9 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)8 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)8 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)7