use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class UndocumentedApiCheck method isAccessor.
private boolean isAccessor(Tree tree) {
if (!classTrees.isEmpty() && tree.is(Tree.Kind.METHOD)) {
MethodTree methodTree = (MethodTree) tree;
String name = methodTree.simpleName().name();
return (setterPattern.matcher(name).matches() && methodTree.parameters().size() == 1) || (getterPattern.matcher(name).matches() && methodTree.parameters().isEmpty());
}
return false;
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class JavaParserTest method parent_link_should_be_computed.
@Test
public void parent_link_should_be_computed() {
CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse("class A { void foo() {} }");
ClassTree classTree = (ClassTree) cut.types().get(0);
MethodTree method = (MethodTree) classTree.members().get(0);
assertThat(method.parent()).isSameAs(classTree);
assertThat(classTree.parent()).isSameAs(cut);
assertThat(cut.parent()).isNull();
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method enum_constructor.
@Test
public void enum_constructor() {
MethodTree tree = (MethodTree) firstType("enum T { ; T(int p1, int... p2) throws Exception1, Exception2 {} }").members().get(1);
assertThat(tree.is(Tree.Kind.CONSTRUCTOR)).isTrue();
assertThat(tree.returnType()).isNull();
assertThat(tree.simpleName().name()).isEqualTo("T");
assertThat(tree.parameters()).hasSize(2);
assertThat(tree.parameters().get(0).type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasEllipsis((ArrayTypeTree) tree.parameters().get(1).type());
assertThat(tree.throwsClauses()).hasSize(2);
assertThat(tree.throwsClauses().separators()).hasSize(1);
assertThat(tree.block()).isNotNull();
assertThat(tree.defaultValue()).isNull();
assertThatChildrenIteratorHasSize(tree, 10);
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method enum_method.
@Test
public void enum_method() {
MethodTree tree = (MethodTree) firstType("enum T { ; int m(int p1, int... p2) throws Exception1, Exception2 {} }").members().get(1);
assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
assertThat(tree.returnType()).isNotNull();
assertThat(tree.simpleName().name()).isEqualTo("m");
assertThat(tree.parameters()).hasSize(2);
assertThat(tree.parameters().get(0).type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasEllipsis((ArrayTypeTree) tree.parameters().get(1).type());
assertThat(tree.throwsClauses()).hasSize(2);
assertThat(tree.throwsClauses().separators()).hasSize(1);
assertThat(tree.block()).isNotNull();
assertThat(tree.defaultValue()).isNull();
assertThatChildrenIteratorHasSize(tree, 11);
// void method
tree = (MethodTree) firstType("enum T { ; void m(int p) throws Exception1, Exception2; }").members().get(1);
assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
assertThat(tree.returnType()).isNotNull();
assertThat(tree.simpleName().name()).isEqualTo("m");
assertThat(tree.parameters()).hasSize(1);
assertThat(tree.throwsClauses()).hasSize(2);
assertThat(tree.throwsClauses().separators()).hasSize(1);
assertThat(tree.block()).isNull();
assertThat(tree.defaultValue()).isNull();
assertThatChildrenIteratorHasSize(tree, 10);
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method interface_method.
@Test
public void interface_method() {
MethodTree tree = (MethodTree) firstTypeMember("interface T { <T> int m(int p1, int... p2) throws Exception1, Exception2; }");
assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
assertThat(tree.typeParameters()).isNotEmpty();
assertThat(tree.returnType()).isNotNull();
assertThat(tree.simpleName().name()).isEqualTo("m");
assertThat(tree.parameters()).hasSize(2);
assertThat(tree.parameters().get(0).type()).isInstanceOf(PrimitiveTypeTree.class);
assertThat(tree.parameters().get(1).type()).isInstanceOf(ArrayTypeTree.class);
assertThat(tree.throwsClauses()).hasSize(2);
assertThat(tree.throwsClauses().separators()).hasSize(1);
assertThat(tree.block()).isNull();
assertThat(tree.defaultValue()).isNull();
assertThatChildrenIteratorHasSize(tree, 11);
// void method
tree = (MethodTree) firstTypeMember("interface T { void m(int p) throws Exception1, Exception2; }");
assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
assertThat(tree.typeParameters()).isEmpty();
assertThat(tree.returnType()).isNotNull();
assertThat(tree.simpleName().name()).isEqualTo("m");
assertThat(tree.parameters()).hasSize(1);
assertThat(tree.throwsClauses()).hasSize(2);
assertThat(tree.throwsClauses().separators()).hasSize(1);
assertThat(tree.block()).isNull();
assertThat(tree.defaultValue()).isNull();
assertThatChildrenIteratorHasSize(tree, 10);
}
Aggregations