Search in sources :

Example 6 with MethodYield

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

the class ExplodedGraphWalkerTest method different_exceptions_lead_to_different_program_states_with_catch_exception_block.

@Test
public void different_exceptions_lead_to_different_program_states_with_catch_exception_block() {
    Set<Type> encounteredExceptions = new HashSet<>();
    int[] tested = { 0 };
    JavaCheckVerifier.verifyNoIssue("src/test/files/se/ExceptionalSymbolicValueStacked.java", new SymbolicExecutionVisitor(Collections.emptyList(), new BehaviorCache(new SquidClassLoader(new ArrayList<>()))) {

        private ExplodedGraphWalker explodedGraphWalker;

        @Override
        public void visitNode(Tree tree) {
            if (explodedGraphWalker == null) {
                explodedGraphWalker = new ExplodedGraphWalker(this.behaviorCache, (SemanticModel) context.getSemanticModel()) {

                    private ExplodedGraph.Node firstExceptionalNode = null;

                    @Override
                    public void enqueue(ProgramPoint newProgramPoint, ProgramState programState, boolean exitPath, MethodYield methodYield) {
                        SymbolicValue.ExceptionalSymbolicValue exceptionSV = null;
                        SymbolicValue peekValue = programState.peekValue();
                        boolean getNode = false;
                        if (peekValue instanceof SymbolicValue.ExceptionalSymbolicValue) {
                            exceptionSV = (SymbolicValue.ExceptionalSymbolicValue) peekValue;
                            Type exceptionType = exceptionSV.exceptionType();
                            if (exceptionType != null && (exceptionType.is("org.foo.MyException1") || exceptionType.is("org.foo.MyException2"))) {
                                encounteredExceptions.add(exceptionType);
                                getNode = true;
                            }
                        }
                        int workListSize = workList.size();
                        super.enqueue(newProgramPoint, programState, exitPath, methodYield);
                        if (getNode) {
                            if (firstExceptionalNode == null) {
                                firstExceptionalNode = workList.peekFirst();
                            }
                            assertThat(workList.size()).as("Should have created a new node in the graph for each of the exceptions").isEqualTo(workListSize + 1);
                            assertThat(workList.peekFirst().programState.peekValue()).as("Exceptional Symbolic Value should stay on the stack").isEqualTo(exceptionSV);
                            tested[0]++;
                        }
                    }
                };
            }
            MethodTree methodTree = (MethodTree) tree;
            if ("foo".equals(methodTree.symbol().name())) {
                explodedGraphWalker.visitMethod(methodTree, methodBehaviorForSymbol(methodTree.symbol()));
            } else {
                super.visitNode(methodTree);
            }
        }
    });
    assertThat(encounteredExceptions).hasSize(2);
    assertThat(tested[0]).isEqualTo(2);
}
Also used : MethodYield(org.sonar.java.se.xproc.MethodYield) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayList(java.util.ArrayList) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Type(org.sonar.plugins.java.api.semantic.Type) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) HashSet(java.util.HashSet) Test(org.junit.Test)

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