Search in sources :

Example 16 with SquidClassLoader

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

the class BytecodeEGWalkerTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    List<File> files = Lists.newArrayList(new File("target/test-classes"), new File("target/classes"));
    files.addAll(FileUtils.listFiles(new File("target/test-jars"), new String[] { "jar" }, false));
    squidClassLoader = new SquidClassLoader(files);
    semanticModel = SemanticModel.createFor((CompilationUnitTree) JavaParser.createParser().parse("class A {}"), squidClassLoader);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) BeforeClass(org.junit.BeforeClass)

Example 17 with SquidClassLoader

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

the class GenericsTest method treeOf.

private static CompilationUnitTree treeOf(File file) {
    CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse(file);
    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 18 with SquidClassLoader

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

the class LambdaBlockReturnVisitorTest method test.

@Test
public void test() throws Exception {
    CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A {\n" + "        java.util.function.Consumer<String> c = s -> {\n" + "          if(s.length()>0) {\n" + "            return;\n" + "          }\n" + "          System.out.println(s);\n" + "        };\n" + "\n" + "        java.util.function.Function<String, String> f = s -> {\n" + "          if(s.length() > 0) {\n" + "            return s.replace('a', 'b');\n" + "          }\n" + "          return unknownSymbol;\n" + "        };\n" + "      }");
    SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
    List<VariableTree> vars = ((ClassTree) cut.types().get(0)).members().stream().map(m -> (VariableTree) m).collect(Collectors.toList());
    LambdaBlockReturnVisitor visitor = new LambdaBlockReturnVisitor();
    ((LambdaExpressionTree) vars.get(0).initializer()).body().accept(visitor);
    assertThat(visitor.types).isEmpty();
    visitor = new LambdaBlockReturnVisitor();
    ((LambdaExpressionTree) vars.get(1).initializer()).body().accept(visitor);
    assertThat(visitor.types).hasSize(1);
}
Also used : JavaParser(org.sonar.java.ast.parser.JavaParser) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ActionParser(com.sonar.sslr.api.typed.ActionParser) List(java.util.List) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Collections(java.util.Collections) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Test(org.junit.Test)

Example 19 with SquidClassLoader

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

the class Result method createForJavaFile.

public static Result createForJavaFile(String filePath) {
    File file = new File(filePath + ".java");
    CompilationUnitTree compilationUnitTree = (CompilationUnitTree) parser.parse(file);
    SemanticModel semanticModel = SemanticModel.createFor(compilationUnitTree, new SquidClassLoader(Lists.newArrayList(new File("target/test-classes"), new File("target/classes"))));
    UsageVisitor usageVisitor = new UsageVisitor();
    compilationUnitTree.accept(usageVisitor);
    return new Result(semanticModel, usageVisitor.symbolsUsed);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) File(java.io.File) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 20 with SquidClassLoader

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

the class ClassTreeImplTest 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)

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