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();
}
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();
}
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();
}
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"));
}
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();
}
Aggregations