Search in sources :

Example 1 with SemanticModel

use of org.sonar.java.resolve.SemanticModel in project sonar-java by SonarSource.

the class VisitorsBridgeForTests method createScannerContext.

@Override
protected JavaFileScannerContext createScannerContext(CompilationUnitTree tree, SemanticModel semanticModel, SonarComponents sonarComponents, boolean failedParsing) {
    SemanticModel model = enableSemantic ? semanticModel : null;
    testContext = new TestJavaFileScannerContext(tree, currentFile, model, sonarComponents, javaVersion, failedParsing);
    return testContext;
}
Also used : SemanticModel(org.sonar.java.resolve.SemanticModel)

Example 2 with SemanticModel

use of org.sonar.java.resolve.SemanticModel in project sonar-java by SonarSource.

the class VisitorsBridgeTest method log_only_50_elements.

@Test
public void log_only_50_elements() throws Exception {
    DecimalFormat formatter = new DecimalFormat("00");
    IntFunction<String> classNotFoundName = i -> "NotFound" + formatter.format(i);
    VisitorsBridge visitorsBridge = new VisitorsBridge(Collections.singletonList((JavaFileScanner) context -> {
        assertThat(context.getSemanticModel()).isNotNull();
        ((SemanticModel) context.getSemanticModel()).classesNotFound().addAll(IntStream.range(0, 60).mapToObj(classNotFoundName).collect(Collectors.toList()));
    }), Lists.newArrayList(), null);
    checkFile("Foo.java", "class Foo {}", visitorsBridge);
    visitorsBridge.endOfAnalysis();
    assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly("Classes not found during the analysis : [" + IntStream.range(0, 50).mapToObj(classNotFoundName).sorted().collect(Collectors.joining(", ")) + ", ...]");
}
Also used : IntStream(java.util.stream.IntStream) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ArrayList(java.util.ArrayList) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) RecognitionException(com.sonar.sslr.api.RecognitionException) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) SonarComponents(org.sonar.java.SonarComponents) Nullable(javax.annotation.Nullable) IntFunction(java.util.function.IntFunction) JavaParser(org.sonar.java.ast.parser.JavaParser) Kind(org.sonar.plugins.java.api.tree.Tree.Kind) DecimalFormat(java.text.DecimalFormat) Test(org.junit.Test) Tree(org.sonar.plugins.java.api.tree.Tree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) File(java.io.File) Fail(org.assertj.core.api.Fail) List(java.util.List) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) SemanticModel(org.sonar.java.resolve.SemanticModel) Collections(java.util.Collections) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) DecimalFormat(java.text.DecimalFormat) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) Test(org.junit.Test)

Example 3 with SemanticModel

use of org.sonar.java.resolve.SemanticModel in project sonar-java by SonarSource.

the class BehaviorCacheTest method clear_stack_when_taking_exceptional_path_from_method_invocation.

@Test
public void clear_stack_when_taking_exceptional_path_from_method_invocation() throws Exception {
    Pair<SymbolicExecutionVisitor, SemanticModel> sevAndSemantic = createSymbolicExecutionVisitorAndSemantic("src/test/files/se/CleanStackWhenRaisingException.java");
    SymbolicExecutionVisitor sev = sevAndSemantic.a;
    SemanticModel semanticModel = sevAndSemantic.b;
    MethodBehavior behavior = getMethodBehavior(sev, "foo");
    assertThat(behavior.yields()).hasSize(4);
    behavior.happyPathYields().forEach(y -> assertThat(y.resultConstraint()).isNull());
    assertThat(behavior.happyPathYields().count()).isEqualTo(1);
    List<ExceptionalYield> exceptionalYields = behavior.exceptionalPathYields().collect(Collectors.toList());
    assertThat(exceptionalYields).hasSize(3);
    assertThat(exceptionalYields.stream().filter(y -> y.exceptionType(semanticModel).isUnknown())).hasSize(1);
}
Also used : ExceptionalYield(org.sonar.java.se.xproc.ExceptionalYield) SemanticModel(org.sonar.java.resolve.SemanticModel) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) Test(org.junit.Test)

Example 4 with SemanticModel

use of org.sonar.java.resolve.SemanticModel in project sonar-java by SonarSource.

the class ExceptionUtilsTest method test_is_unchecked_exception.

@Test
public void test_is_unchecked_exception() {
    assertThat(ExceptionUtils.isUncheckedException(null)).isFalse();
    assertThat(ExceptionUtils.isUncheckedException(Symbols.unknownType)).isFalse();
    SemanticModel semanticModel = SETestUtils.getSemanticModel("src/test/java/org/sonar/java/se/ExceptionUtilsTest.java");
    Type ex = semanticModel.getClassType("java.lang.IllegalArgumentException");
    assertThat(ExceptionUtils.isUncheckedException(ex)).isTrue();
    ex = semanticModel.getClassType("java.lang.Exception");
    assertThat(ExceptionUtils.isUncheckedException(ex)).isTrue();
    ex = semanticModel.getClassType("java.lang.Throwable");
    assertThat(ExceptionUtils.isUncheckedException(ex)).isTrue();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) SemanticModel(org.sonar.java.resolve.SemanticModel) Test(org.junit.Test)

Example 5 with SemanticModel

use of org.sonar.java.resolve.SemanticModel in project sonar-java by SonarSource.

the class NullableAnnotationUtilsTest method getSemanticModel.

private static SemanticModel getSemanticModel(String fileName, List<File> classPath) {
    CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse(new File(fileName));
    SemanticModel semanticModel = SemanticModel.createFor(cut, new SquidClassLoader(classPath));
    return semanticModel;
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) SemanticModel(org.sonar.java.resolve.SemanticModel) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Aggregations

SemanticModel (org.sonar.java.resolve.SemanticModel)19 Test (org.junit.Test)13 SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)9 ArrayList (java.util.ArrayList)8 List (java.util.List)8 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)8 File (java.io.File)7 Collectors (java.util.stream.Collectors)7 SETestUtils.createSymbolicExecutionVisitor (org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor)7 SETestUtils.getMethodBehavior (org.sonar.java.se.SETestUtils.getMethodBehavior)7 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)7 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)6 SETestUtils.mockMethodBehavior (org.sonar.java.se.SETestUtils.mockMethodBehavior)5 SymbolicExecutionVisitor (org.sonar.java.se.SymbolicExecutionVisitor)5 BehaviorCache (org.sonar.java.se.xproc.BehaviorCache)5 MethodBehavior (org.sonar.java.se.xproc.MethodBehavior)5 ImmutableList (com.google.common.collect.ImmutableList)4 Lists (com.google.common.collect.Lists)4 Set (java.util.Set)4 Nullable (javax.annotation.Nullable)4