use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class BytecodeCFGBuilderTest method test_all_instructions_are_part_of_CFG.
@Test
public void test_all_instructions_are_part_of_CFG() throws Exception {
SquidClassLoader squidClassLoader = new SquidClassLoader(Lists.newArrayList(new File("target/test-classes"), new File("target/classes")));
File file = new File("src/test/java/org/sonar/java/bytecode/cfg/testdata/CFGTestData.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
SemanticModel.createFor(tree, squidClassLoader);
Symbol.TypeSymbol testClazz = ((ClassTree) tree.types().get(0)).symbol();
ClassReader cr = new ClassReader(squidClassLoader.getResourceAsStream(Convert.bytecodeName(CFGTestData.class.getCanonicalName()) + ".class"));
ClassNode classNode = new ClassNode(Opcodes.ASM5);
cr.accept(classNode, 0);
for (MethodNode method : classNode.methods) {
Multiset<String> opcodes = Arrays.stream(method.instructions.toArray()).map(AbstractInsnNode::getOpcode).filter(opcode -> opcode != -1).map(opcode -> Printer.OPCODES[opcode]).collect(Collectors.toCollection(HashMultiset::create));
Symbol methodSymbol = Iterables.getOnlyElement(testClazz.lookupSymbols(method.name));
BytecodeCFG bytecodeCFG = SETestUtils.bytecodeCFG(((JavaSymbol.MethodJavaSymbol) methodSymbol).completeSignature(), squidClassLoader);
Multiset<String> cfgOpcodes = cfgOpcodes(bytecodeCFG);
assertThat(cfgOpcodes).isEqualTo(opcodes);
}
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree 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);
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree in project sonar-java by SonarSource.
the class GenericsTest method declaredTypesFromFile.
private static List<Type> declaredTypesFromFile(String path) {
CompilationUnitTree tree = treeOf(new File(path));
List<Type> results = Lists.newLinkedList();
for (Tree classTree : tree.types()) {
Type type = ((ClassTree) classTree).symbol().type();
results.add(type);
}
return results;
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree 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;
}
use of org.sonar.plugins.java.api.tree.CompilationUnitTree 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);
}
Aggregations