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;
}
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");
}
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");
}
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);
}
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");
}
Aggregations