Search in sources :

Example 16 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class BytecodeCompleterTest method forward_type_parameter_in_classes.

@Test
public void forward_type_parameter_in_classes() throws Exception {
    Symbol.TypeSymbol clazz = bytecodeCompleter.getClassSymbol("org.sonar.java.resolve.targets.ForwardParameterInClass");
    assertThat(clazz.type()).isNotNull();
    Collection<Symbol> symbols = clazz.lookupSymbols("bar");
    assertThat(symbols).hasSize(1);
    Collection<JavaSymbol> typeParameters = ((JavaSymbol.TypeJavaSymbol) clazz).typeParameters().scopeSymbols();
    assertThat(typeParameters).hasSize(2);
    JavaSymbol xSymbol = ((JavaSymbol.TypeJavaSymbol) clazz).typeParameters().lookup("X").iterator().next();
    JavaSymbol ySymbol = ((JavaSymbol.TypeJavaSymbol) clazz).typeParameters().lookup("Y").iterator().next();
    assertThat(((TypeVariableJavaType) xSymbol.type).bounds).hasSize(1);
    JavaType bound = ((TypeVariableJavaType) xSymbol.type).bounds.get(0);
    assertThat(((ParametrizedTypeJavaType) bound).typeParameters()).hasSize(1);
    assertThat(((ParametrizedTypeJavaType) bound).substitution(((ParametrizedTypeJavaType) bound).typeParameters().get(0))).isSameAs(ySymbol.type);
}
Also used : TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) Test(org.junit.Test)

Example 17 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class BytecodeCompleterTest method innerClassWithDollarName.

@Test
public void innerClassWithDollarName() throws Exception {
    JavaSymbol.TypeJavaSymbol classSymbol = bytecodeCompleter.getClassSymbol("org/sonar/java/resolve/targets/UseDollarNames");
    // Complete superclass which has an inner class named A$Bq
    JavaType superclass = classSymbol.getSuperclass();
    JavaType superSuperClass = superclass.getSymbol().getSuperclass();
    assertThat(superSuperClass.fullyQualifiedName()).isEqualTo("java.lang.Object");
    Collection<Symbol> symbols = superclass.getSymbol().memberSymbols();
    for (Symbol symbol : symbols) {
        if (symbol.isTypeSymbol()) {
            assertThat(symbol.name()).isEqualTo("A$B");
            Collection<Symbol> members = ((Symbol.TypeSymbol) symbol).lookupSymbols("C$D");
            assertThat(members).isNotEmpty();
        }
    }
}
Also used : TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) Test(org.junit.Test)

Example 18 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class SymbolTableTest method enum_protected_constructor_should_not_be_resolved.

@Test
public void enum_protected_constructor_should_not_be_resolved() {
    Result result = Result.createFor("EnumConstructor");
    Symbol constructorRef = result.referenceTree(2, 3).symbol();
    assertThat(constructorRef.name()).isEqualTo("<init>");
    assertThat(constructorRef.owner().type().is("java.lang.Enum")).overridingErrorMessage("Wrongly resolving unaccessible protected enum constructor").isFalse();
    assertThat(constructorRef.owner().type().is("EnumConstructor")).isTrue();
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) Test(org.junit.Test)

Example 19 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class SymbolTableTest method annotations_on_fields.

@Test
public void annotations_on_fields() throws Exception {
    Result result = Result.createFor("AnnotationOnFields");
    JavaSymbol.TypeSymbol app = (JavaSymbol.TypeSymbol) result.symbol("App");
    for (Symbol sym : app.memberSymbols()) {
        if (!sym.isMethodSymbol() && !(sym.name().equals("super") || sym.name().equals("this"))) {
            assertThat(sym.metadata().isAnnotatedWith("java.lang.Deprecated")).isTrue();
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) Test(org.junit.Test)

Example 20 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class SymbolTableTest method symbolNotFound.

@Test
public void symbolNotFound() throws Exception {
    Result result = Result.createFor("SymbolsNotFound");
    MethodTree methodDeclaration = (MethodTree) result.symbol("method").declaration();
    ExpressionStatementTree expression = (ExpressionStatementTree) methodDeclaration.block().body().get(0);
    MethodInvocationTree methodInvocation = (MethodInvocationTree) expression.expression();
    Symbol symbolNotFound = methodInvocation.symbol();
    assertThat(symbolNotFound).isNotNull();
    assertThat(symbolNotFound.name()).isNull();
    assertThat(symbolNotFound.owner()).isSameAs(Symbols.unknownSymbol);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Test(org.junit.Test)

Aggregations

Symbol (org.sonar.plugins.java.api.semantic.Symbol)140 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)47 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)41 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)33 Tree (org.sonar.plugins.java.api.tree.Tree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)28 JavaSymbol (org.sonar.java.resolve.JavaSymbol)27 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)26 Type (org.sonar.plugins.java.api.semantic.Type)24 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)24 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)23 List (java.util.List)19 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)17 Collectors (java.util.stream.Collectors)14 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)13 Set (java.util.Set)12 ImmutableList (com.google.common.collect.ImmutableList)11 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)11