Search in sources :

Example 1 with SECheck

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

the class CheckerDispatcher method executeCheckPreStatement.

public boolean executeCheckPreStatement(Tree syntaxNode) {
    this.syntaxNode = syntaxNode;
    ProgramState ps;
    for (SECheck checker : checks) {
        ps = checker.checkPreStatement(this, syntaxNode);
        if (ps == null) {
            return false;
        }
        explodedGraphWalker.programState = ps;
    }
    return true;
}
Also used : SECheck(org.sonar.java.se.checks.SECheck)

Example 2 with SECheck

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

the class BehaviorCacheTest method test_peek.

@Test
public void test_peek() throws Exception {
    Set<String> testedPre = new HashSet<>();
    Set<String> testedPost = new HashSet<>();
    SECheck check = new SECheck() {

        @Override
        public ProgramState checkPreStatement(CheckerContext context, Tree syntaxNode) {
            if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
                Symbol.MethodSymbol symbol = (Symbol.MethodSymbol) ((MethodInvocationTree) syntaxNode).symbol();
                MethodBehavior peekMethodBehavior = ((CheckerDispatcher) context).peekMethodBehavior(symbol);
                assertThat(peekMethodBehavior).isNull();
                testedPre.add(symbol.name());
            }
            return context.getState();
        }

        @Override
        public ProgramState checkPostStatement(CheckerContext context, Tree syntaxNode) {
            if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
                Symbol.MethodSymbol symbol = (Symbol.MethodSymbol) ((MethodInvocationTree) syntaxNode).symbol();
                String methodName = symbol.name();
                MethodBehavior peekMethodBehavior = ((CheckerDispatcher) context).peekMethodBehavior(symbol);
                assertThat(peekMethodBehavior).isNotNull();
                if ("foo".equals(methodName) || "isBlank".equals(methodName)) {
                    // foo should have been computed
                    assertThat(peekMethodBehavior.isComplete()).isTrue();
                } else if ("bar".equals(methodName)) {
                    assertThat(peekMethodBehavior.isComplete()).isFalse();
                }
                testedPost.add(methodName);
            }
            return super.checkPostStatement(context, syntaxNode);
        }
    };
    SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/BehaviorCachePeek.java", check);
    assertThat(sev.behaviorCache.peek("org.apache.commons.lang.StringUtils#isBlank(Ljava/lang/String;)Z").isComplete()).isTrue();
    assertThat(sev.behaviorCache.peek("org.foo.A#foo()Z").isComplete()).isTrue();
    assertThat(sev.behaviorCache.peek("org.foo.A#bar()Z").isComplete()).isFalse();
    assertThat(sev.behaviorCache.peek("org.foo.A#unknownMethod()Z")).isNull();
    assertThat(sev.behaviorCache.behaviors.keySet()).containsOnly("org.foo.A#foo()Z");
    assertThat(testedPre).containsOnly("foo", "bar", "isBlank");
    assertThat(testedPost).containsOnly("foo", "bar", "isBlank");
}
Also used : SECheck(org.sonar.java.se.checks.SECheck) Symbol(org.sonar.plugins.java.api.semantic.Symbol) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with SECheck

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

the class ExplodedGraphWalkerTest method private_method_should_be_visited.

@Test
public void private_method_should_be_visited() {
    List<String> visitedMethods = new ArrayList<>();
    JavaCheckVerifier.verifyNoIssue("src/test/files/se/XprocIfaceWithPrivateMethod.java", new SECheck() {

        @Override
        public void init(MethodTree methodTree, CFG cfg) {
            visitedMethods.add(methodTree.symbol().name());
        }
    });
    assertThat(visitedMethods).containsExactly("test", "privateMethod");
}
Also used : SECheck(org.sonar.java.se.checks.SECheck) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) CFG(org.sonar.java.cfg.CFG) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with SECheck

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

the class ExplodedGraphWalkerTest method binary_expression_creates_not_null_value.

@Test
public void binary_expression_creates_not_null_value() throws Exception {
    int[] counter = new int[1];
    SECheck check = new SECheck() {

        @Override
        public ProgramState checkPostStatement(CheckerContext context, Tree syntaxNode) {
            ProgramState state = context.getState();
            if (syntaxNode instanceof BinaryExpressionTree) {
                SymbolicValue sv = state.peekValue();
                assertThat(state.getConstraint(sv, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
                counter[0]++;
            }
            return state;
        }
    };
    JavaCheckVerifier.verifyNoIssue("src/test/files/se/BinaryTreeExecution.java", check);
    assertThat(counter[0]).isEqualTo(17);
}
Also used : SECheck(org.sonar.java.se.checks.SECheck) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) 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) Test(org.junit.Test)

Example 5 with SECheck

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

the class ExceptionalCheckBasedYieldTest method exceptionType_cannot_be_changed.

@Test
public void exceptionType_cannot_be_changed() {
    thrown.expect(UnsupportedOperationException.class);
    final Class<? extends SECheck> seCheckClass = new SECheck() {
    }.getClass();
    String exceptionType = "someException";
    ExceptionalCheckBasedYield yield = new ExceptionalCheckBasedYield(SV_CAUSING_EXCEPTION, exceptionType, seCheckClass, null, mockMethodBehavior());
    yield.setExceptionType("anotherException");
}
Also used : SECheck(org.sonar.java.se.checks.SECheck) Test(org.junit.Test)

Aggregations

SECheck (org.sonar.java.se.checks.SECheck)12 Test (org.junit.Test)11 SETestUtils.getMethodBehavior (org.sonar.java.se.SETestUtils.getMethodBehavior)4 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)4 Tree (org.sonar.plugins.java.api.tree.Tree)4 ArrayList (java.util.ArrayList)3 SETestUtils.createSymbolicExecutionVisitor (org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor)3 SETestUtils.mockMethodBehavior (org.sonar.java.se.SETestUtils.mockMethodBehavior)3 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)3 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)3 File (java.io.File)2 CFG (org.sonar.java.cfg.CFG)2 SemanticModel (org.sonar.java.resolve.SemanticModel)2 SymbolicExecutionVisitor (org.sonar.java.se.SymbolicExecutionVisitor)2 ImmutableList (com.google.common.collect.ImmutableList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)1 SonarComponents (org.sonar.java.SonarComponents)1