Search in sources :

Example 6 with SquidClassLoader

use of org.sonar.java.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.

the class BytecodeCompleterTest method test_loading_java9_class.

@Test
public void test_loading_java9_class() throws Exception {
    BytecodeCompleter bytecodeCompleter = new BytecodeCompleter(new SquidClassLoader(Collections.singletonList(new File("src/test/files/bytecode/java9/bin"))), new ParametrizedTypeCache());
    new Symbols(bytecodeCompleter);
    TypeJavaSymbol classSymbol = (TypeJavaSymbol) bytecodeCompleter.loadClass("org.test.Hello9");
    classSymbol.complete();
    assertThat(classSymbol.getFullyQualifiedName()).isEqualTo("org.test.Hello9");
    assertThat(classSymbol.memberSymbols()).hasSize(2);
}
Also used : TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

Example 7 with SquidClassLoader

use of org.sonar.java.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.

the class BytecodeCompleterTest method test_loading_java9_iface.

@Test
public void test_loading_java9_iface() throws Exception {
    BytecodeCompleter bytecodeCompleter = new BytecodeCompleter(new SquidClassLoader(Collections.singletonList(new File("src/test/files/bytecode/java9/bin"))), new ParametrizedTypeCache());
    new Symbols(bytecodeCompleter);
    TypeJavaSymbol iface = (TypeJavaSymbol) bytecodeCompleter.loadClass("org.test.IfaceTest");
    iface.complete();
    assertThat(iface.getFullyQualifiedName()).isEqualTo("org.test.IfaceTest");
    assertThat(iface.memberSymbols()).hasSize(3);
    assertThat(iface.isInterface()).isTrue();
    JavaSymbol.MethodJavaSymbol privateMethod = (JavaSymbol.MethodJavaSymbol) Iterables.getOnlyElement(iface.lookupSymbols("privateMethod"));
    assertThat(privateMethod.flags()).isEqualTo(Flags.PRIVATE);
    JavaSymbol.MethodJavaSymbol defaultMethod = (JavaSymbol.MethodJavaSymbol) Iterables.getOnlyElement(iface.lookupSymbols("defaultMethod"));
    assertThat(defaultMethod.flags()).isEqualTo(Flags.DEFAULT | Flags.PUBLIC);
    JavaSymbol.MethodJavaSymbol staticMethod = (JavaSymbol.MethodJavaSymbol) Iterables.getOnlyElement(iface.lookupSymbols("staticMethod"));
    assertThat(staticMethod.flags()).isEqualTo(Flags.STATIC | Flags.PUBLIC);
}
Also used : TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

Example 8 with SquidClassLoader

use of org.sonar.java.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.

the class LeastUpperBoundTest method treeOf.

private static CompilationUnitTree treeOf(String... lines) {
    StringBuilder builder = new StringBuilder();
    for (String line : lines) {
        builder.append(line).append(System.lineSeparator());
    }
    CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse(builder.toString());
    SemanticModel.createFor(cut, new SquidClassLoader(Lists.newArrayList(new File("target/test-classes"), new File("target/classes"))));
    return cut;
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 9 with SquidClassLoader

use of org.sonar.java.bytecode.loader.SquidClassLoader 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)

Example 10 with SquidClassLoader

use of org.sonar.java.bytecode.loader.SquidClassLoader in project sonar-java by SonarSource.

the class MethodYieldTest method constraints_on_varargs.

@Test
public void constraints_on_varargs() throws Exception {
    ActionParser<Tree> p = JavaParser.createParser();
    CompilationUnitTree cut = (CompilationUnitTree) p.parse(new File("src/test/files/se/VarArgsYields.java"));
    SemanticModel semanticModel = SemanticModel.createFor(cut, new SquidClassLoader(new ArrayList<>()));
    SymbolicExecutionVisitor sev = new SymbolicExecutionVisitor(Lists.newArrayList(new SECheck[] {}), new BehaviorCache(new SquidClassLoader(new ArrayList<>())));
    JavaFileScannerContext context = mock(JavaFileScannerContext.class);
    when(context.getTree()).thenReturn(cut);
    when(context.getSemanticModel()).thenReturn(semanticModel);
    sev.scanFile(context);
    MethodSymbol methodSymbol = ((MethodTree) ((ClassTree) cut.types().get(0)).members().get(0)).symbol();
    MethodBehavior mb = getMethodBehavior(sev, "varArgMethod");
    List<MethodYield> yields = mb.yields();
    assertThat(yields).hasSize(5);
    assertThat(mb.exceptionalPathYields()).hasSize(4);
    MethodYield yield = mb.happyPathYields().findFirst().get();
    // check that we have NOT_NULL constraint on the first argument
    assertThat(yield.parametersConstraints.get(0).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    // check that we have NOT_NULL constraint on the variadic argument
    assertThat(yield.parametersConstraints.get(1).get(ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    List<IdentifierTree> usages = methodSymbol.usages();
    assertThat(usages).hasSize(6);
    List<List<Type>> arguments = usages.stream().map(MethodYieldTest::getMethodIncoationArgumentsTypes).collect(Collectors.toList());
    ProgramState ps = ProgramState.EMPTY_STATE;
    ProgramState psResult;
    SymbolicValue svFirstArg = new SymbolicValue();
    SymbolicValue svVarArg1 = new SymbolicValue();
    SymbolicValue svVarArg2 = new SymbolicValue();
    SymbolicValue svResult = new SymbolicValue();
    // apply constraint NotNull to parameter
    Collection<ProgramState> arrayOfA = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg, svVarArg1), arguments.get(0), ps, () -> svResult).collect(Collectors.toList());
    assertThat(arrayOfA).hasSize(1);
    psResult = arrayOfA.iterator().next();
    assertThat(psResult.getConstraint(svFirstArg, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    assertThat(psResult.getConstraint(svVarArg1, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    // apply constraint NotNull to parameter (B[] is a subtype of A[])
    Collection<ProgramState> arrayOfB = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg, svVarArg1), arguments.get(1), ps, () -> svResult).collect(Collectors.toList());
    assertThat(arrayOfB).hasSize(1);
    psResult = arrayOfB.iterator().next();
    assertThat(psResult.getConstraint(svFirstArg, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    assertThat(psResult.getConstraint(svVarArg1, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    // no constraint, as 'a' is not an array
    Collection<ProgramState> objectA = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg, svVarArg1), arguments.get(2), ps, () -> svResult).collect(Collectors.toList());
    assertThat(objectA).hasSize(1);
    psResult = objectA.iterator().next();
    assertThat(psResult.getConstraint(svFirstArg, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    assertThat(psResult.getConstraint(svVarArg1, ObjectConstraint.class)).isNull();
    // no constraint, as 'a' and 'b' can not receive the constraint of the array
    Collection<ProgramState> objectsAandB = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg, svVarArg1, svVarArg2), arguments.get(3), ps, () -> svResult).collect(Collectors.toList());
    assertThat(objectsAandB).hasSize(1);
    psResult = objectsAandB.iterator().next();
    assertThat(psResult.getConstraint(svFirstArg, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    assertThat(psResult.getConstraint(svVarArg1, ObjectConstraint.class)).isNull();
    assertThat(psResult.getConstraint(svVarArg2, ObjectConstraint.class)).isNull();
    // no param, we only learn something about the argument which is not variadic
    Collection<ProgramState> noParam = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg), arguments.get(4), ps, () -> svResult).collect(Collectors.toList());
    assertThat(noParam).hasSize(1);
    psResult = noParam.iterator().next();
    assertThat(psResult.getConstraint(svFirstArg, ObjectConstraint.class)).isEqualTo(ObjectConstraint.NOT_NULL);
    // null param, contradiction, no resulting program state
    ps = ProgramState.EMPTY_STATE.addConstraint(svFirstArg, ObjectConstraint.NULL);
    Collection<ProgramState> nullParam = yield.statesAfterInvocation(Lists.newArrayList(svFirstArg, svVarArg1), arguments.get(5), ps, () -> svResult).collect(Collectors.toList());
    assertThat(nullParam).isEmpty();
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayList(java.util.ArrayList) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ProgramState(org.sonar.java.se.ProgramState) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) SECheck(org.sonar.java.se.checks.SECheck) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) SemanticModel(org.sonar.java.resolve.SemanticModel) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) SymbolicExecutionVisitor(org.sonar.java.se.SymbolicExecutionVisitor) MethodSymbol(org.sonar.plugins.java.api.semantic.Symbol.MethodSymbol) SETestUtils.mockMethodBehavior(org.sonar.java.se.SETestUtils.mockMethodBehavior) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) File(java.io.File) Test(org.junit.Test)

Aggregations

SquidClassLoader (org.sonar.java.bytecode.loader.SquidClassLoader)53 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)37 File (java.io.File)24 Test (org.junit.Test)18 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)12 ArrayList (java.util.ArrayList)11 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)10 SemanticModel (org.sonar.java.resolve.SemanticModel)9 Tree (org.sonar.plugins.java.api.tree.Tree)9 List (java.util.List)7 Collectors (java.util.stream.Collectors)6 BeforeClass (org.junit.BeforeClass)6 BehaviorCache (org.sonar.java.se.xproc.BehaviorCache)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Opcodes (org.objectweb.asm.Opcodes)5 JavaParser (org.sonar.java.ast.parser.JavaParser)5 IOException (java.io.IOException)4 Collections (java.util.Collections)4 Before (org.junit.Before)4 Label (org.objectweb.asm.Label)4