Search in sources :

Example 26 with Type

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

the class SymbolTableTest method extended_type_variables.

@Test
public void extended_type_variables() throws Exception {
    String javaLangObject = "java.lang.Object";
    Result result = Result.createFor("Generics");
    Type MyClass = result.symbol("MyClass", 100).type();
    Type i = result.symbol("I").type();
    Type j = result.symbol("J").type();
    JavaSymbol.TypeJavaSymbol w = (JavaSymbol.TypeJavaSymbol) result.symbol("W", 103);
    assertThat(w.superClass().is(javaLangObject)).isTrue();
    assertThat(w.interfaces()).isEmpty();
    JavaSymbol.TypeJavaSymbol x = (JavaSymbol.TypeJavaSymbol) result.symbol("X", 103);
    assertThat(x.superClass()).isSameAs(MyClass);
    assertThat(x.interfaces()).isEmpty();
    JavaSymbol.TypeJavaSymbol y = (JavaSymbol.TypeJavaSymbol) result.symbol("Y", 103);
    assertThat(y.superClass().is(javaLangObject)).isTrue();
    assertThat(y.interfaces()).containsExactly(i);
    JavaSymbol.TypeJavaSymbol z = (JavaSymbol.TypeJavaSymbol) result.symbol("Z", 103);
    assertThat(z.superClass()).isSameAs(MyClass);
    assertThat(z.interfaces()).containsExactly(i, j);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Test(org.junit.Test)

Example 27 with Type

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

the class SymbolTableTest method ConstructorWithInference.

@Test
public void ConstructorWithInference() throws Exception {
    Result result = Result.createFor("ConstructorWithInference");
    JavaSymbol.TypeJavaSymbol classSymbol = (JavaSymbol.TypeJavaSymbol) result.symbol("A");
    List<JavaSymbol> constructors = classSymbol.members.lookup("<init>");
    JavaSymbol noParamConstructor = constructors.get(0);
    JavaSymbol parametrizedConstructor = constructors.get(1);
    JavaSymbol wildcardConstructor = constructors.get(2);
    Type aObjectType = result.symbol("aObject").type();
    Type aStringType = result.symbol("aString").type();
    IdentifierTree constStringNoArg = result.referenceTree(14, 9);
    assertThat(constStringNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constStringNoArg)).isSameAs(aStringType);
    IdentifierTree constDiamondNoArg = result.referenceTree(16, 9);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constDiamondNoArg)).isSameAs(aObjectType);
    constDiamondNoArg = result.referenceTree(17, 9);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constDiamondNoArg)).isSameAs(aObjectType);
    assertThat(result.reference(17, 15)).isSameAs(result.symbol("foo", 10));
    constDiamondNoArg = result.referenceTree(18, 13);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constDiamondNoArg)).isSameAs(aObjectType);
    constDiamondNoArg = result.referenceTree(20, 25);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constDiamondNoArg)).isSameAs(aStringType);
    constDiamondNoArg = result.referenceTree(21, 18);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constDiamondNoArg)).isSameAs(aObjectType);
    constDiamondNoArg = result.referenceTree(23, 13);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constDiamondNoArg)).isSameAs(aStringType);
    assertThat(result.reference(23, 5)).isSameAs(result.symbol("bar", 11));
    IdentifierTree constDiamondObject = result.referenceTree(25, 9);
    assertThat(constDiamondObject.symbol()).isSameAs(parametrizedConstructor);
    assertThat(getNewClassTreeType(constDiamondObject)).isSameAs(aObjectType);
    IdentifierTree constDiamondString = result.referenceTree(26, 9);
    assertThat(constDiamondString.symbol()).isSameAs(parametrizedConstructor);
    assertThat(getNewClassTreeType(constDiamondString)).isSameAs(aStringType);
    IdentifierTree constWildcardDiamond = result.referenceTree(28, 9);
    assertThat(constWildcardDiamond.symbol()).isSameAs(wildcardConstructor);
    assertThat(getNewClassTreeType(constWildcardDiamond)).isSameAs(aStringType);
    IdentifierTree returnConstDiamondNoArg = result.referenceTree(30, 16);
    assertThat(returnConstDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(returnConstDiamondNoArg)).isSameAs(aStringType);
    // inference allowing to deduce only partially the type parameters
    classSymbol = (JavaSymbol.TypeJavaSymbol) result.symbol("B");
    noParamConstructor = classSymbol.members.lookup("<init>").get(0);
    ParametrizedTypeJavaType type;
    constDiamondNoArg = result.referenceTree(42, 13);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    type = (ParametrizedTypeJavaType) getNewClassTreeType(constDiamondNoArg);
    assertThat(type.erasure()).isSameAs(classSymbol.type().erasure());
    assertThat(type.substitution(type.typeParameters().get(0)).is("java.lang.String")).isTrue();
    assertThat(type.substitution(type.typeParameters().get(1)).is("java.lang.Object")).isTrue();
    assertThat(type.substitution(type.typeParameters().get(2)).is("java.lang.Object")).isTrue();
    assertThat(result.reference(42, 5)).isSameAs(result.symbol("qix", 46));
    constDiamondNoArg = result.referenceTree(43, 13);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    type = (ParametrizedTypeJavaType) getNewClassTreeType(constDiamondNoArg);
    assertThat(type.erasure()).isSameAs(classSymbol.type().erasure());
    assertThat(type.substitution(type.typeParameters().get(0)).is("java.lang.Object")).isTrue();
    assertThat(type.substitution(type.typeParameters().get(1)).is("java.lang.String")).isTrue();
    assertThat(type.substitution(type.typeParameters().get(2)).is("java.lang.Integer")).isTrue();
    assertThat(result.reference(43, 5)).isSameAs(result.symbol("bar", 47));
    // erasure of bounded type parameters
    classSymbol = (JavaSymbol.TypeJavaSymbol) result.symbol("D");
    noParamConstructor = classSymbol.members.lookup("<init>").get(0);
    constDiamondNoArg = result.referenceTree(60, 9);
    assertThat(constDiamondNoArg.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constDiamondNoArg)).isSameAs(result.symbol("dC").type());
    assertThat(result.reference(60, 15)).isSameAs(result.symbol("foo", 55));
    // enclosing expression
    classSymbol = (JavaSymbol.TypeJavaSymbol) result.symbol("F");
    constructors = classSymbol.members.lookup("<init>");
    noParamConstructor = constructors.get(0);
    parametrizedConstructor = constructors.get(1);
    IdentifierTree constructorCall = result.referenceTree(75, 15);
    assertThat(constructorCall.symbol()).isSameAs(noParamConstructor);
    assertThat(getNewClassTreeType(constructorCall)).isSameAs(result.symbol("fString").type());
    assertThat(result.reference(75, 5)).isSameAs(result.symbol("foo", 72));
    constructorCall = result.referenceTree(76, 15);
    assertThat(constructorCall.symbol()).isSameAs(parametrizedConstructor);
    assertThat(getNewClassTreeType(constructorCall)).isSameAs(result.symbol("fString").type());
    assertThat(result.reference(76, 5)).isSameAs(result.symbol("foo", 72));
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Example 28 with Type

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

the class TypeAndReferenceSolverTest method parametrized_method_return_type_is_array.

@Test
public void parametrized_method_return_type_is_array() {
    Type arrayType = returnTypeOf("<T> T[] foo(T t) { return null; }", "this.<String>foo(\"hello\");");
    assertThat(arrayType.isArray()).isTrue();
    assertThat(((ArrayJavaType) arrayType).elementType.is("java.lang.String")).isTrue();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Test(org.junit.Test)

Example 29 with Type

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

the class ExceptionUtilsTest method test_is_unchecked_exception.

@Test
public void test_is_unchecked_exception() {
    assertThat(ExceptionUtils.isUncheckedException(null)).isFalse();
    assertThat(ExceptionUtils.isUncheckedException(Symbols.unknownType)).isFalse();
    SemanticModel semanticModel = SETestUtils.getSemanticModel("src/test/java/org/sonar/java/se/ExceptionUtilsTest.java");
    Type ex = semanticModel.getClassType("java.lang.IllegalArgumentException");
    assertThat(ExceptionUtils.isUncheckedException(ex)).isTrue();
    ex = semanticModel.getClassType("java.lang.Exception");
    assertThat(ExceptionUtils.isUncheckedException(ex)).isTrue();
    ex = semanticModel.getClassType("java.lang.Throwable");
    assertThat(ExceptionUtils.isUncheckedException(ex)).isTrue();
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) SemanticModel(org.sonar.java.resolve.SemanticModel) Test(org.junit.Test)

Example 30 with Type

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

the class TypeSubstitutionSolverTest method substitutionFromSuperType_with_multiple_variables.

@Test
public void substitutionFromSuperType_with_multiple_variables() {
    Result result = Result.createForJavaFile("src/test/files/sym/TypeSubstitutionSolver");
    Type stringType = result.symbol("string").type();
    Type integerType = result.symbol("integer").type();
    ParametrizedTypeJavaType jStringInteger = (ParametrizedTypeJavaType) result.symbol("jStringInteger").type();
    ParametrizedTypeJavaType cXY = (ParametrizedTypeJavaType) result.symbol("cXY").type();
    TypeVariableJavaType x = cXY.typeParameters().get(0);
    TypeVariableJavaType y = cXY.typeParameters().get(1);
    TypeSubstitution substitution = TypeSubstitutionSolver.substitutionFromSuperType(cXY, jStringInteger);
    assertThat(substitution.size()).isEqualTo(2);
    assertThat(substitution.typeVariables()).containsExactly(x, y);
    assertThat(substitution.substitutedType(x)).isSameAs(stringType);
    assertThat(substitution.substitutedType(y)).isSameAs(integerType);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) 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