Search in sources :

Example 6 with ClassTreeImpl

use of org.sonar.java.model.declaration.ClassTreeImpl in project sonar-java by SonarSource.

the class TypeAndReferenceSolverTest method annotation_on_method.

@Test
public void annotation_on_method() {
    CompilationUnitTree compilationUnit = treeOf("@interface MyAnnotation { } class Class { @MyAnnotation void method() { } }");
    ClassTreeImpl annotation = (ClassTreeImpl) compilationUnit.types().get(0);
    ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(1);
    MethodTreeImpl method = (MethodTreeImpl) clazz.members().get(0);
    List<AnnotationInstance> annotations = ((JavaSymbol.MethodJavaSymbol) method.symbol()).metadata().annotations();
    assertThat(annotations.size()).isEqualTo(1);
    assertThat(annotations.get(0).symbol().type().is(annotation.symbol().name())).isTrue();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) MethodTreeImpl(org.sonar.java.model.declaration.MethodTreeImpl) AnnotationInstance(org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance) Test(org.junit.Test)

Example 7 with ClassTreeImpl

use of org.sonar.java.model.declaration.ClassTreeImpl in project sonar-java by SonarSource.

the class TypeAndReferenceSolverTest method annotation_on_type.

@Test
public void annotation_on_type() {
    CompilationUnitTree compilationUnit = treeOf("@interface MyAnnotation { } @MyAnnotation class Class { }");
    ClassTreeImpl annotation = (ClassTreeImpl) compilationUnit.types().get(0);
    ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(1);
    List<AnnotationInstance> annotations = ((JavaSymbol.TypeJavaSymbol) clazz.symbol()).metadata().annotations();
    assertThat(annotations.size()).isEqualTo(1);
    assertThat(annotations.get(0).symbol().type().is(annotation.symbol().name())).isTrue();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) AnnotationInstance(org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance) Test(org.junit.Test)

Example 8 with ClassTreeImpl

use of org.sonar.java.model.declaration.ClassTreeImpl in project sonar-java by SonarSource.

the class TypeAndReferenceSolverTest method parametrized_constructor.

@Test
public void parametrized_constructor() {
    CompilationUnitTree compilationUnit = treeOf("class A { <T> A(T t) {} void test() { new A(\"hello\"); } }");
    MethodTree testMethod = (MethodTree) ((ClassTreeImpl) compilationUnit.types().get(0)).members().get(1);
    NewClassTree newClassTree = (NewClassTree) ((ExpressionStatementTree) testMethod.block().body().get(0)).expression();
    Type identifierType = newClassTree.identifier().symbolType();
    assertThat(identifierType).isInstanceOf(MethodJavaType.class);
    assertThat(((MethodJavaType) identifierType).argTypes.get(0).is("java.lang.String")).isTrue();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) Type(org.sonar.plugins.java.api.semantic.Type) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) Test(org.junit.Test)

Example 9 with ClassTreeImpl

use of org.sonar.java.model.declaration.ClassTreeImpl in project sonar-java by SonarSource.

the class TypeAndReferenceSolverTest method returnTypeOf.

private Type returnTypeOf(String method, String invocation) {
    CompilationUnitTree compilationUnit = treeOf("class A { " + method + " void test() { " + invocation + " } }");
    MethodTree testMethod = (MethodTree) ((ClassTreeImpl) compilationUnit.types().get(0)).members().get(1);
    ExpressionTree methodInvocation = ((ExpressionStatementTree) testMethod.block().body().get(0)).expression();
    return methodInvocation.symbolType();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree)

Example 10 with ClassTreeImpl

use of org.sonar.java.model.declaration.ClassTreeImpl in project sonar-java by SonarSource.

the class TypeAndReferenceSolverTest method annotation_on_method_parameter.

@Test
public void annotation_on_method_parameter() {
    CompilationUnitTree compilationUnit = treeOf("@interface MyAnnotation { } class Class { void method(@MyAnnotation int a) { } }");
    ClassTreeImpl annotation = (ClassTreeImpl) compilationUnit.types().get(0);
    ClassTreeImpl clazz = (ClassTreeImpl) compilationUnit.types().get(1);
    MethodTreeImpl method = (MethodTreeImpl) clazz.members().get(0);
    VariableTreeImpl parameter = (VariableTreeImpl) method.parameters().get(0);
    List<AnnotationInstance> annotations = parameter.getSymbol().metadata().annotations();
    assertThat(annotations.size()).isEqualTo(1);
    assertThat(annotations.get(0).symbol().type().is(annotation.symbol().name())).isTrue();
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) MethodTreeImpl(org.sonar.java.model.declaration.MethodTreeImpl) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl) AnnotationInstance(org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance) Test(org.junit.Test)

Aggregations

ClassTreeImpl (org.sonar.java.model.declaration.ClassTreeImpl)12 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)8 Test (org.junit.Test)6 AnnotationInstance (org.sonar.plugins.java.api.semantic.SymbolMetadata.AnnotationInstance)4 VariableTreeImpl (org.sonar.java.model.declaration.VariableTreeImpl)3 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)3 ImmutableList (com.google.common.collect.ImmutableList)2 JavaTree (org.sonar.java.model.JavaTree)2 MethodTreeImpl (org.sonar.java.model.declaration.MethodTreeImpl)2 NewClassTreeImpl (org.sonar.java.model.expression.NewClassTreeImpl)2 Type (org.sonar.plugins.java.api.semantic.Type)2 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)2 TypeParameterTree (org.sonar.plugins.java.api.tree.TypeParameterTree)2 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)1 EnumConstantTreeImpl (org.sonar.java.model.declaration.EnumConstantTreeImpl)1 ModifiersTreeImpl (org.sonar.java.model.declaration.ModifiersTreeImpl)1 IdentifierTreeImpl (org.sonar.java.model.expression.IdentifierTreeImpl)1