Search in sources :

Example 21 with Type

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

the class LeastUpperBoundTest method lub_with_hierarchy_of_supertypes2.

@Test
public void lub_with_hierarchy_of_supertypes2() {
    List<Type> typesFromInput = declaredTypes("class A extends Exception {}", "class B extends Throwable {}", "class C extends B {}");
    Type a = typesFromInput.get(0);
    Type c = typesFromInput.get(2);
    Type lub = leastUpperBound(a, c);
    assertThat(lub.is("java.lang.Throwable")).isTrue();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Test(org.junit.Test)

Example 22 with Type

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

the class LeastUpperBoundTest method lub_approximation_with_complexe_inheritance.

@Test
public void lub_approximation_with_complexe_inheritance() {
    List<Type> typesFromInput = declaredTypes("class A extends Exception implements I1, I2 {}", "class B extends Exception implements I2, I1 {}", "interface I1 {}", "interface I2 {}");
    Type a = typesFromInput.get(0);
    Type b = typesFromInput.get(1);
    Type lub = leastUpperBound(a, b);
    // should be <Exception & I1 & I2>
    assertThat(lub.is("java.lang.Exception")).isTrue();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Test(org.junit.Test)

Example 23 with Type

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

the class LeastUpperBoundTest method lub_select_best_return_first_classes_then_interfaces_ordered_alphabetically.

@Test
public void lub_select_best_return_first_classes_then_interfaces_ordered_alphabetically() {
    List<Type> typesFromInput = declaredTypes("class A {}", "class B {}", "interface I1 {}", "interface I2 {}");
    Type a = typesFromInput.get(0);
    Type b = typesFromInput.get(1);
    Type i1 = typesFromInput.get(2);
    Type i2 = typesFromInput.get(3);
    Type best = LeastUpperBound.best(Lists.newArrayList(i1, a, b, i2));
    assertThat(best.is("A")).isTrue();
    best = LeastUpperBound.best(Lists.newArrayList(i2, i1));
    assertThat(best.is("I1")).isTrue();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Test(org.junit.Test)

Example 24 with Type

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

the class SymbolTableTest method parameterized_method_type.

@Test
public void parameterized_method_type() throws Exception {
    Result result = Result.createFor("Generics");
    MethodTree method3 = (MethodTree) result.symbol("method3").declaration();
    VariableTree variable = (VariableTree) method3.block().body().get(0);
    assertThat(variable.initializer().symbolType().symbol().name()).isEqualTo("String");
    MethodTree method4 = (MethodTree) result.symbol("method4").declaration();
    variable = (VariableTree) method4.block().body().get(0);
    Type symbolType = variable.initializer().symbolType();
    assertThat(symbolType).isInstanceOf(ParametrizedTypeJavaType.class);
    ParametrizedTypeJavaType ptt = (ParametrizedTypeJavaType) symbolType;
    assertThat(ptt.typeSubstitution.substitutedTypes().iterator().next().getSymbol().getName()).isEqualTo("String");
    assertThat(result.reference(58, 25)).isSameAs(result.symbol("method_of_e"));
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Test(org.junit.Test)

Example 25 with Type

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

the class SymbolTableTest method Lambdas.

@Test
public void Lambdas() throws Exception {
    Result result = Result.createFor("Lambdas");
    JavaSymbol barMethod = result.symbol("bar");
    assertThat(barMethod.usages()).hasSize(1);
    MethodTree methodTree = (MethodTree) barMethod.declaration();
    Type Ftype = result.symbol("F").type();
    assertThat(((ReturnStatementTree) methodTree.block().body().get(0)).expression().symbolType()).isSameAs(Ftype);
    JavaSymbol qixMethod = result.symbol("qix");
    LambdaExpressionTree lamdba = ((LambdaExpressionTree) ((ReturnStatementTree) ((MethodTree) qixMethod.declaration()).block().body().get(0)).expression());
    assertThat(((ReturnStatementTree) ((BlockTree) lamdba.body()).body().get(0)).expression().symbolType()).isSameAs(result.symbol("F2").type());
    JavaSymbol fieldSymbol = result.symbol("field");
    assertThat(((VariableTree) fieldSymbol.declaration()).initializer().symbolType()).isSameAs(fieldSymbol.type());
    assertThat(((AssignmentExpressionTree) fieldSymbol.usages().get(0).parent()).expression().symbolType()).isSameAs(fieldSymbol.type());
    JavaSymbol bSymbol = result.symbol("b");
    assertThat(((NewClassTree) ((VariableTree) bSymbol.declaration()).initializer()).arguments().get(0).symbolType()).isSameAs(Ftype);
    JavaSymbol condMethod = result.symbol("cond");
    ConditionalExpressionTree conditionalExpression = (ConditionalExpressionTree) ((ReturnStatementTree) ((MethodTree) condMethod.declaration()).block().body().get(0)).expression();
    assertThat(conditionalExpression.symbolType()).isSameAs(Ftype);
    JavaSymbol parenthMethod = result.symbol("parenth");
    ParenthesizedTree parenthesizedTree = (ParenthesizedTree) ((ReturnStatementTree) ((MethodTree) parenthMethod.declaration()).block().body().get(0)).expression();
    assertThat(parenthesizedTree.symbolType()).isSameAs(Ftype);
    assertThat(result.symbol("s", 33).type().is("java.lang.String")).isTrue();
    JavaSymbol sym = result.symbol("o");
    assertThat(sym.type.is("java.lang.Object")).isTrue();
    assertThat(result.reference(8, 16)).isEqualTo(result.symbol("v", 8));
    assertThat(result.reference(9, 16)).isEqualTo(result.symbol("v", 9));
    JavaSymbol operations = result.symbol("operations");
    MethodInvocationTree mit = (MethodInvocationTree) operations.usages().get(0).parent().parent();
    assertThat(((ParametrizedTypeJavaType) operations.type).typeSubstitution.substitutedTypes().get(0)).isSameAs(mit.arguments().get(0).symbolType());
    JavaSymbol myStringParam = result.symbol("myStringParam");
    Symbol.MethodSymbol stringParamMethod = (Symbol.MethodSymbol) result.symbol("stringParamMethod");
    assertThat(stringParamMethod.usages()).hasSize(1);
    assertThat(myStringParam.type.is("java.lang.String")).isTrue();
    assertThat(result.symbol("s1").type.is("java.lang.String")).as(result.symbol("s1").type.name()).isTrue();
    assertThat(result.symbol("s2").type.is("java.lang.String")).isTrue();
    assertThat(result.symbol("foo", 95).usages()).hasSize(1);
    assertThat(result.symbol("x", 103).type.is("java.lang.Integer")).as(result.symbol("x", 103).type.name()).isTrue();
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) Type(org.sonar.plugins.java.api.semantic.Type) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) Test(org.junit.Test)

Aggregations

Type (org.sonar.plugins.java.api.semantic.Type)164 Test (org.junit.Test)67 Symbol (org.sonar.plugins.java.api.semantic.Symbol)23 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)23 MethodJavaSymbol (org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol)18 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)18 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)17 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)17 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)17 JavaType (org.sonar.java.resolve.JavaType)16 Tree (org.sonar.plugins.java.api.tree.Tree)15 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)13 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)12 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)11 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)10 ArrayList (java.util.ArrayList)9 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)9 VisibleForTesting (com.google.common.annotations.VisibleForTesting)8 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)8 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)8