use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class BytecodeCFGBuilderTest method getBytecodeCFG.
public static BytecodeCFG getBytecodeCFG(String methodName, String filename) {
SquidClassLoader squidClassLoader = new SquidClassLoader(Lists.newArrayList(new File("target/test-classes"), new File("target/classes")));
File file = new File(filename);
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
SemanticModel.createFor(tree, squidClassLoader);
List<Tree> classMembers = ((ClassTree) tree.types().get(0)).members();
Symbol.MethodSymbol symbol = classMembers.stream().filter(m -> m instanceof MethodTree).map(m -> ((MethodTree) m).symbol()).filter(s -> methodName.equals(s.name())).findFirst().orElseThrow(IllegalStateException::new);
return SETestUtils.bytecodeCFG(((JavaSymbol.MethodJavaSymbol) symbol).completeSignature(), squidClassLoader);
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class VariableReadExtractorTest method buildMethodTree.
private static MethodTree buildMethodTree(String methodCode) {
CompilationUnitTree cut = (CompilationUnitTree) PARSER.parse("class A { int field1; int field2; " + methodCode + " }");
SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
return (MethodTree) ((ClassTree) cut.types().get(0)).members().get(2);
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class SyntacticEquivalenceTest method getAssertion.
private AbstractBooleanAssert<?> getAssertion(List<String> statement1, List<String> statement2) {
CompilationUnitTree compilationUnitTree = compilationUnitTree("class A { void method1() { " + Joiner.on(";").join(statement1) + ";} " + "void method2(){ " + Joiner.on(";").join(statement2) + ";} }");
ClassTree classTree = ((ClassTree) compilationUnitTree.types().get(0));
assertThat(classTree.members()).hasSize(2);
return assertThat(SyntacticEquivalence.areEquivalent(((MethodTree) classTree.members().get(0)).block().body(), ((MethodTree) classTree.members().get(1)).block().body()));
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class MethodTreeImplTest method hiding_of_static_methods.
@Test
public void hiding_of_static_methods() {
CompilationUnitTree cut = createTree("class A { static void foo() {} } class B extends A { void foo(){} } ");
ClassTree clazz = (ClassTree) cut.types().get(1);
MethodTreeImpl methodTree = (MethodTreeImpl) clazz.members().get(0);
assertThat(methodTree.isOverriding()).isFalse();
}
use of org.sonar.plugins.java.api.tree.ClassTree in project sonar-java by SonarSource.
the class MethodTreeImplTest method override_with_generic_parameters_should_be_detected.
@Test
public void override_with_generic_parameters_should_be_detected() throws Exception {
CompilationUnitTree cut = createTree("public class ReferenceQueue<T> {\n" + "\n" + " private static class Null extends ReferenceQueue {\n" + " boolean enqueue(Reference r) {\n" + " return false;\n" + " }\n" + " }\n" + " boolean enqueue(Reference<? extends T> r) {}}" + "public abstract class Reference<T> {}");
ClassTree innerClass = (ClassTree) cut.types().get(0);
MethodTreeImpl methodInterface = (MethodTreeImpl) ((ClassTree) innerClass.members().get(0)).members().get(0);
assertThat(methodInterface.isOverriding()).isTrue();
}
Aggregations