Search in sources :

Example 1 with Flow

use of org.sonar.java.se.Flow 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)

Example 2 with Flow

use of org.sonar.java.se.Flow in project sonar-java by SonarSource.

the class MethodYieldTest method test_creation_of_flows.

@Test
public void test_creation_of_flows() throws Exception {
    SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/XProcYieldsFlows.java");
    MethodBehavior mb = getMethodBehavior(sev, "foo");
    MethodYield methodYield = mb.happyPathYields().filter(y -> y.resultConstraint() != null && y.resultConstraint().get(ObjectConstraint.class) != ObjectConstraint.NULL).findFirst().get();
    Set<Flow> flowReturnValue = methodYield.flow(ImmutableList.of(-1), Lists.newArrayList(ObjectConstraint.class));
    assertThat(flowReturnValue.iterator().next().isEmpty()).isFalse();
    Set<Flow> flowFirstParam = methodYield.flow(ImmutableList.of(0), Lists.newArrayList(ObjectConstraint.class, BooleanConstraint.class));
    assertThat(flowFirstParam.iterator().next().isEmpty()).isFalse();
}
Also used : SETestUtils.mockMethodBehavior(org.sonar.java.se.SETestUtils.mockMethodBehavior) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) SymbolicExecutionVisitor(org.sonar.java.se.SymbolicExecutionVisitor) Flow(org.sonar.java.se.Flow) Test(org.junit.Test)

Example 3 with Flow

use of org.sonar.java.se.Flow in project sonar-java by SonarSource.

the class StreamNotConsumedCheck method checkEndOfExecutionPath.

@Override
public void checkEndOfExecutionPath(CheckerContext context, ConstraintManager constraintManager) {
    if (context.getState().exitValue() instanceof SymbolicValue.ExceptionalSymbolicValue) {
        // don't report when exiting on exception
        return;
    }
    ProgramState state = context.getState();
    List<SymbolicValue> notConsumed = state.getValuesWithConstraints(NOT_CONSUMED);
    notConsumed.forEach(sv -> {
        Set<Flow> flows = FlowComputation.flow(context.getNode(), Collections.singleton(sv), NOT_CONSUMED::equals, NOT_CONSUMED::equals, Collections.singletonList(StreamConsumedCheck.StreamPipelineConstraint.class), Collections.emptySet());
        Flow flow = flows.iterator().next();
        JavaFileScannerContext.Location location = flow.elements().get(0);
        reportIssue(location.syntaxNode, "Refactor the code so this stream pipeline is used.");
    });
}
Also used : NOT_CONSUMED(org.sonar.java.se.checks.StreamConsumedCheck.StreamPipelineConstraint.NOT_CONSUMED) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Flow(org.sonar.java.se.Flow)

Example 4 with Flow

use of org.sonar.java.se.Flow 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 5 with Flow

use of org.sonar.java.se.Flow 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

Flow (org.sonar.java.se.Flow)6 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)4 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)4 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)4 ProgramState (org.sonar.java.se.ProgramState)3 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)3 Symbol (org.sonar.plugins.java.api.semantic.Symbol)3 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)3 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)3 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)3 Tree (org.sonar.plugins.java.api.tree.Tree)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Lists (com.google.common.collect.Lists)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 ExplodedGraph (org.sonar.java.se.ExplodedGraph)2 FlowComputation (org.sonar.java.se.FlowComputation)2 Constraint (org.sonar.java.se.constraint.Constraint)2 Preconditions (com.google.common.base.Preconditions)1