Search in sources :

Example 1 with MethodYield

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

the class BytecodeEGWalkerTest method test_enqueueing_of_catch_blocks2.

@Test
public void test_enqueueing_of_catch_blocks2() {
    MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "testCatchBlockEnqueue2()Z");
    List<MethodYield> yields = mb.yields();
    assertThat(yields).hasSize(1);
    // result should have TRUE constraint, but wrong yield with FALSE constraint is also created
    // and two yields are reduced subsequently
    assertThat(mb.happyPathYields().findFirst().get().resultConstraint()).isNull();
    assertThat(mb.exceptionalPathYields().findFirst().isPresent()).isFalse();
}
Also used : MethodYield(org.sonar.java.se.xproc.MethodYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 2 with MethodYield

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

the class BytecodeEGWalkerTest method test_method_throwing_exception.

@Test
public void test_method_throwing_exception() throws Exception {
    MethodBehavior methodBehavior = getMethodBehavior("throw_exception()V");
    assertThat(methodBehavior.yields()).hasSize(1);
    MethodYield methodYield = methodBehavior.yields().get(0);
    assertThat(methodYield).isInstanceOf(ExceptionalYield.class);
}
Also used : MethodYield(org.sonar.java.se.xproc.MethodYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 3 with MethodYield

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

the class BytecodeEGWalkerTest method test_enqueueing_of_exit_block.

@Test
public void test_enqueueing_of_exit_block() {
    MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "enqueueExitBlock()Z");
    List<MethodYield> yields = mb.yields();
    assertThat(yields).hasSize(1);
    assertThat(mb.happyPathYields().findFirst().isPresent()).isFalse();
    ExceptionalYield exceptionalYield = mb.exceptionalPathYields().findFirst().get();
    Type exceptionType = exceptionalYield.exceptionType(semanticModel);
    assertThat(exceptionType.is("java.io.FileNotFoundException")).isTrue();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodYield(org.sonar.java.se.xproc.MethodYield) ExceptionalYield(org.sonar.java.se.xproc.ExceptionalYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Test(org.junit.Test)

Example 4 with MethodYield

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

the class BytecodeEGWalkerExecuteTest method method_returning_new_should_have_not_null_result.

@Test
public void method_returning_new_should_have_not_null_result() {
    MethodBehavior mb = walker.getMethodBehavior(BytecodeEGWalkerExecuteTest.class.getCanonicalName() + "#newObject()Ljava/lang/Object;", squidClassLoader);
    List<MethodYield> yields = mb.yields();
    assertThat(yields).hasSize(1);
    MethodYield yield = yields.get(0);
    assertThat(yield).isInstanceOf(HappyPathYield.class);
    ConstraintsByDomain resultConstraint = ((HappyPathYield) yield).resultConstraint();
    assertThat(resultConstraint).isNotNull();
    assertThat(resultConstraint.get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    TypedConstraint typeConstraint = (TypedConstraint) resultConstraint.get(TypedConstraint.class);
    assertThat(typeConstraint.type.equals("java.lang.String")).isTrue();
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) MethodYield(org.sonar.java.se.xproc.MethodYield) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) TypedConstraint(org.sonar.java.se.constraint.TypedConstraint) Test(org.junit.Test)

Example 5 with MethodYield

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

the class ExplodedGraphWalker method enqueueExceptionalPaths.

private void enqueueExceptionalPaths(ProgramState ps, Symbol methodSymbol, @Nullable MethodYield methodYield) {
    Set<CFG.Block> exceptionBlocks = ((CFG.Block) node.programPoint.block).exceptions();
    List<CFG.Block> catchBlocks = exceptionBlocks.stream().filter(CFG.Block.IS_CATCH_BLOCK).collect(Collectors.toList());
    SymbolicValue peekValue = ps.peekValue();
    Preconditions.checkState(peekValue instanceof SymbolicValue.ExceptionalSymbolicValue, "Top of stack should always contains exceptional SV");
    SymbolicValue.ExceptionalSymbolicValue exceptionSV = (SymbolicValue.ExceptionalSymbolicValue) peekValue;
    // only consider the first match, as order of catch block is important
    List<CFG.Block> caughtBlocks = catchBlocks.stream().filter(b -> isCaughtByBlock(exceptionSV.exceptionType(), b)).sorted((b1, b2) -> Integer.compare(b2.id(), b1.id())).collect(Collectors.toList());
    if (!caughtBlocks.isEmpty()) {
        caughtBlocks.forEach(b -> enqueue(new ProgramPoint(b), ps, methodYield));
        return;
    }
    // branch to any unchecked exception catch
    catchBlocks.stream().filter(ExplodedGraphWalker::isCatchingUncheckedException).forEach(b -> enqueue(new ProgramPoint(b), ps, methodYield));
    // store the exception as exit value in case of method exit in next block
    ps.storeExitValue();
    // use other exceptional blocks, i.e. finally block and exit blocks
    List<CFG.Block> otherBlocks = exceptionBlocks.stream().filter(CFG.Block.IS_CATCH_BLOCK.negate().or(b -> methodSymbol.isUnknown())).collect(Collectors.toList());
    if (otherBlocks.isEmpty()) {
        // explicitly add the exception branching to method exit
        CFG.Block methodExit = node.programPoint.block.successors().stream().map(b -> (CFG.Block) b).filter(CFG.Block::isMethodExitBlock).findFirst().orElse(exitBlock);
        enqueue(new ProgramPoint(methodExit), ps, true, methodYield);
    } else {
        otherBlocks.forEach(b -> enqueue(new ProgramPoint(b), ps, true, methodYield));
    }
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) ExpressionUtils(org.sonar.java.model.ExpressionUtils) MethodYield(org.sonar.java.se.xproc.MethodYield) Loggers(org.sonar.api.utils.log.Loggers) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) NullDereferenceCheck(org.sonar.java.se.checks.NullDereferenceCheck) SECheck(org.sonar.java.se.checks.SECheck) RedundantAssignmentsCheck(org.sonar.java.se.checks.RedundantAssignmentsCheck) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Map(java.util.Map) MethodMatcher(org.sonar.java.matcher.MethodMatcher) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) ImmutableSet(com.google.common.collect.ImmutableSet) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) Collection(java.util.Collection) Set(java.util.Set) JavaTree(org.sonar.java.model.JavaTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) Collectors(java.util.stream.Collectors) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) DebugCheck(org.sonar.java.DebugCheck) StreamConsumedCheck(org.sonar.java.se.checks.StreamConsumedCheck) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) NullableAnnotationUtils.isGloballyAnnotatedParameterNonNull(org.sonar.java.se.NullableAnnotationUtils.isGloballyAnnotatedParameterNonNull) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) Joiner(com.google.common.base.Joiner) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) NullableAnnotationUtils.isAnnotatedNonNull(org.sonar.java.se.NullableAnnotationUtils.isAnnotatedNonNull) HashMap(java.util.HashMap) Deque(java.util.Deque) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) NullableAnnotationUtils.isGloballyAnnotatedParameterNullable(org.sonar.java.se.NullableAnnotationUtils.isGloballyAnnotatedParameterNullable) ArrayList(java.util.ArrayList) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) JavaType(org.sonar.java.resolve.JavaType) DivisionByZeroCheck(org.sonar.java.se.checks.DivisionByZeroCheck) ConstraintManager(org.sonar.java.se.constraint.ConstraintManager) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) NoWayOutLoopCheck(org.sonar.java.se.checks.NoWayOutLoopCheck) LiveVariables(org.sonar.java.cfg.LiveVariables) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) LinkedList(java.util.LinkedList) MethodMatcherCollection(org.sonar.java.matcher.MethodMatcherCollection) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) UnclosedResourcesCheck(org.sonar.java.se.checks.UnclosedResourcesCheck) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) Types(org.sonar.java.resolve.Types) Iterator(java.util.Iterator) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) Type(org.sonar.plugins.java.api.semantic.Type) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) OptionalGetBeforeIsPresentCheck(org.sonar.java.se.checks.OptionalGetBeforeIsPresentCheck) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) NonNullSetToNullCheck(org.sonar.java.se.checks.NonNullSetToNullCheck) CFG(org.sonar.java.cfg.CFG) SemanticModel(org.sonar.java.resolve.SemanticModel) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) CheckForNull(javax.annotation.CheckForNull) LocksNotUnlockedCheck(org.sonar.java.se.checks.LocksNotUnlockedCheck) NullableAnnotationUtils.isAnnotatedNullable(org.sonar.java.se.NullableAnnotationUtils.isAnnotatedNullable) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) CFG(org.sonar.java.cfg.CFG) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Aggregations

MethodYield (org.sonar.java.se.xproc.MethodYield)6 Test (org.junit.Test)5 MethodBehavior (org.sonar.java.se.xproc.MethodBehavior)4 Type (org.sonar.plugins.java.api.semantic.Type)3 ArrayList (java.util.ArrayList)2 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)2 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)2 BehaviorCache (org.sonar.java.se.xproc.BehaviorCache)2 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)2 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)2 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2 Tree (org.sonar.plugins.java.api.tree.Tree)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Joiner (com.google.common.base.Joiner)1 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Lists (com.google.common.collect.Lists)1 Collection (java.util.Collection)1