Search in sources :

Example 21 with SquidClassLoader

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

the class OpensDirectiveTreeImplTest method moduleDirective.

private OpensDirectiveTree moduleDirective(String exportsDirective) {
    CompilationUnitTree compilationUnitTree = createTree("module org.foo {\n  " + exportsDirective + "\n}");
    SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
    return (OpensDirectiveTree) compilationUnitTree.moduleDeclaration().moduleDirectives().get(0);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) OpensDirectiveTree(org.sonar.plugins.java.api.tree.OpensDirectiveTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 22 with SquidClassLoader

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

the class MethodInvocationTreeImplTest method createTree.

private CompilationUnitTree createTree(String code) {
    CompilationUnitTree compilationUnitTree = (CompilationUnitTree) p.parse(code);
    SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Collections.emptyList()));
    return compilationUnitTree;
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 23 with SquidClassLoader

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

the class BytecodeEGWalkerExecuteTest method athrow_should_not_be_linked_to_next_label.

@Test
public void athrow_should_not_be_linked_to_next_label() throws Exception {
    CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse("class A {int field;}");
    SquidClassLoader classLoader = new SquidClassLoader(Collections.singletonList(new File("src/test/JsrRet")));
    SemanticModel semanticModel = SemanticModel.createFor(tree, classLoader);
    BehaviorCache behaviorCache = new BehaviorCache(classLoader);
    behaviorCache.setFileContext(null, semanticModel);
    BytecodeEGWalker walker = new BytecodeEGWalker(behaviorCache, semanticModel);
    MethodBehavior methodBehavior = walker.getMethodBehavior("org.apache.commons.io.FileUtils#readFileToString(Ljava/io/File;)Ljava/lang/String;", classLoader);
    assertThat(methodBehavior.happyPathYields().collect(Collectors.toList())).hasSize(1);
    assertThat(methodBehavior.exceptionalPathYields().collect(Collectors.toList())).hasSize(2);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) SemanticModel(org.sonar.java.resolve.SemanticModel) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

Example 24 with SquidClassLoader

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

the class LiveVariablesTest method buildCFG.

private static CFG buildCFG(String methodCode) {
    CompilationUnitTree cut = (CompilationUnitTree) PARSER.parse("class A { int field1; int field2; static int staticField; " + methodCode + " }");
    SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
    MethodTree tree = ((MethodTree) ((ClassTree) cut.types().get(0)).members().get(3));
    return CFG.build(tree);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 25 with SquidClassLoader

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

the class ClassLoaderBuilder method create.

public static SquidClassLoader create(Collection<File> bytecodeFilesOrDirectories) {
    List<File> files = Lists.newArrayList();
    for (File file : bytecodeFilesOrDirectories) {
        if (file.isFile() && file.getPath().endsWith(".class")) {
            LOG.info("SonarQube Squid ClassLoader was expecting a JAR file instead of CLASS file : '" + file.getAbsolutePath() + "'");
        } else {
            files.add(file);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("----- Classpath analyzed by Squid:");
        for (File file : files) {
            LOG.debug(file.getAbsolutePath());
        }
        LOG.debug("-----");
    }
    try {
        return new SquidClassLoader(files);
    } catch (Exception e) {
        throw new IllegalStateException("Can not create ClassLoader", e);
    }
}
Also used : File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

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