use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class MethodMatcherTest method should_fail_if_name_is_not_defined.
@Test
public void should_fail_if_name_is_not_defined() throws Exception {
MethodMatcher matcher = MethodMatcher.create().withoutParameter();
MethodTree tree = methodTreeMock("toString", mock(Symbol.TypeSymbol.class));
exception.expect(IllegalStateException.class);
matcher.matches(tree);
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method class_method.
@Test
public void class_method() {
MethodTree tree = (MethodTree) firstTypeMember("class T { public int m(int p[][]){} }");
assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
assertThat(tree.parameters()).hasSize(1);
assertThat(tree.parameters().get(0).type()).isInstanceOf(ArrayTypeTree.class);
assertThatChildrenIteratorHasSize(tree, 8);
assertThat(((ArrayTypeTree) tree.parameters().get(0).type()).type()).isInstanceOf(ArrayTypeTree.class);
tree = (MethodTree) firstTypeMember("class T { public <T> int m(@Annotate int p1, int... p2) throws Exception1, Exception2 {} }");
assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
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(0).modifiers().annotations()).hasSize(1);
assertThat(tree.parameters().get(1).type()).isInstanceOf(ArrayTypeTree.class);
assertThat(tree.parameters().get(1).endToken()).isNull();
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) firstTypeMember("class T { public void m(int p) throws Exception1, Exception2 {} }");
assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(tree.typeParameters()).isEmpty();
assertThat(tree.returnType()).isNotNull();
assertThat(tree.simpleName().name()).isEqualTo("m");
assertThat(tree.parameters()).hasSize(1);
assertThat(tree.parameters().get(0).endToken()).isNull();
assertThat(tree.throwsClauses()).hasSize(2);
assertThat(tree.block()).isNotNull();
assertThat(tree.defaultValue()).isNull();
assertThatChildrenIteratorHasSize(tree, 10);
tree = (MethodTree) firstTypeMember("class T { public int[] m(int p1, int... p2)[] throws Exception1, Exception2 {} }");
assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).modifier()).isEqualTo(Modifier.PUBLIC);
assertThat(tree.returnType()).isNotNull();
assertThat(tree.returnType().is(Tree.Kind.ARRAY_TYPE)).isTrue();
assertThat(((ArrayTypeTree) tree.returnType()).type().is(Kind.ARRAY_TYPE)).isTrue();
assertThat(((ArrayTypeTree) ((ArrayTypeTree) tree.returnType()).type()).type().is(Kind.PRIMITIVE_TYPE)).isTrue();
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()).isNotNull();
assertThat(tree.defaultValue()).isNull();
assertThatChildrenIteratorHasSize(tree, 11);
tree = (MethodTree) firstTypeMember("class T { public int m()[] { return null; } }");
assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
assertThat(tree.parameters()).isEmpty();
assertThat(tree.returnType().is(Kind.ARRAY_TYPE)).isTrue();
assertThat(tree.returnType().is(Kind.PRIMITIVE_TYPE)).isFalse();
assertThatChildrenIteratorHasSize(tree, 7);
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method annotation_method.
@Test
public void annotation_method() {
MethodTree tree = (MethodTree) firstTypeMember("@interface T { int m() default 0; }");
assertThat(tree.is(Tree.Kind.METHOD)).isTrue();
assertThat(tree.returnType()).isNotNull();
assertThat(tree.simpleName().name()).isEqualTo("m");
assertThat(tree.parameters()).isEmpty();
assertThat(tree.throwsClauses()).isEmpty();
assertThat(tree.block()).isNull();
assertThat(tree.defaultValue()).isNotNull();
assertThatChildrenIteratorHasSize(tree, 9);
tree = (MethodTree) firstTypeMember("@interface plop{ public String method(); }");
assertThat(tree.modifiers().modifiers()).hasSize(1);
assertThat(tree.modifiers().modifiers().get(0).keyword().text()).isEqualTo("public");
assertThatChildrenIteratorHasSize(tree, 7);
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method array_formal_parameter.
@Test
public void array_formal_parameter() {
MethodTree method;
VariableTree variable;
ArrayTypeTree arrayTypeTree, childArrayTypeTree;
method = (MethodTree) firstTypeMember("interface T { void m(int[] a); }");
variable = method.parameters().get(0);
// 1+2, as empty modifiers are always included
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBrackets(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
method = (MethodTree) firstTypeMember("interface T { void m(int... a); }");
variable = method.parameters().get(0);
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasEllipsis(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 2);
method = (MethodTree) firstTypeMember("interface T { void m(int @Foo ... a); }");
variable = method.parameters().get(0);
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasEllipsisAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
method = (MethodTree) firstTypeMember("interface T { void m(int @Foo ... a[]); }");
variable = method.parameters().get(0);
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
assertThatArrayTypeHasBrackets(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasEllipsisAndAnnotations(childArrayTypeTree, 1);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.ellipsisToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
method = (MethodTree) firstTypeMember("interface T { void m(int @Foo [] a); }");
assertThatChildrenIteratorHasSize(variable, 3);
variable = method.parameters().get(0);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
method = (MethodTree) firstTypeMember("interface T { void m(int @Foo @bar [] a); }");
variable = method.parameters().get(0);
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
assertThat(arrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 2);
assertThatChildrenIteratorHasSize(arrayTypeTree, 5);
method = (MethodTree) firstTypeMember("interface T { void m(int[] @Foo [] a); }");
variable = method.parameters().get(0);
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
method = (MethodTree) firstTypeMember("interface T { void m(int[] a[]); }");
variable = method.parameters().get(0);
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
assertThatArrayTypeHasBrackets(arrayTypeTree);
assertThatChildrenIteratorHasSize(arrayTypeTree, 3);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
method = (MethodTree) firstTypeMember("interface T { void m(int[] a @Foo []); }");
variable = method.parameters().get(0);
assertThatChildrenIteratorHasSize(variable, 3);
assertThat(variable.type()).isInstanceOf(ArrayTypeTree.class);
arrayTypeTree = (ArrayTypeTree) variable.type();
assertThat(arrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBracketsAndAnnotations(arrayTypeTree, 1);
assertThatChildrenIteratorHasSize(arrayTypeTree, 4);
childArrayTypeTree = (ArrayTypeTree) arrayTypeTree.type();
assertThat(childArrayTypeTree).isInstanceOf(ArrayTypeTree.class);
assertThatArrayTypeHasBrackets(childArrayTypeTree);
assertThatChildrenIteratorHasSize(childArrayTypeTree, 3);
assertThat(childArrayTypeTree.openBracketToken().column() < arrayTypeTree.openBracketToken().column()).isTrue();
assertThat(childArrayTypeTree.type()).isInstanceOf(PrimitiveTypeTree.class);
}
use of org.sonar.plugins.java.api.tree.MethodTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method blocks.
/*
* 14. Blocks and Statements
*/
/**
* 14.2. Blocks
*/
@Test
public void blocks() {
BlockTree tree = ((MethodTree) firstTypeMember("class T { void m() { ; ; } }")).block();
assertThat(tree.is(Tree.Kind.BLOCK)).isTrue();
assertThat(tree.openBraceToken().text()).isEqualTo("{");
assertThat(tree.body()).hasSize(2);
assertThat(tree.closeBraceToken().text()).isEqualTo("}");
assertThatChildrenIteratorHasSize(tree, 4);
}
Aggregations