Search in sources :

Example 31 with CtTypeParameter

use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.

the class CtTypeTest method testIsSubTypeOfonTypeParameters.

@Test
public void testIsSubTypeOfonTypeParameters() throws Exception {
    CtType<X> xCtType = buildClass(X.class);
    Factory factory = xCtType.getFactory();
    CtType<?> oCtType = factory.Type().get("spoon.test.ctType.testclasses.O");
    CtType<?> pCtType = factory.Type().get("spoon.test.ctType.testclasses.P");
    CtTypeReference<?> objectCtTypeRef = factory.Type().OBJECT;
    List<CtTypeParameter> oTypeParameters = oCtType.getFormalCtTypeParameters();
    assertTrue(oTypeParameters.size() == 1);
    List<CtTypeParameter> pTypeParameters = pCtType.getFormalCtTypeParameters();
    assertTrue(pTypeParameters.size() == 2);
    CtType<?> O_A_CtType = oTypeParameters.get(0);
    CtType<?> P_D_CtType = pTypeParameters.get(0);
    CtType<?> P_F_CtType = pTypeParameters.get(1);
    CtMethod<?> O_FooMethod = oCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();
    CtMethod<?> P_FooMethod = pCtType.filterChildren(new NamedElementFilter<>(CtMethod.class, "foo")).first();
    CtType<?> O_B_CtType = O_FooMethod.getType().getDeclaration();
    CtType<?> P_E_CtType = P_FooMethod.getType().getDeclaration();
    assertTrue(O_B_CtType.isSubtypeOf(xCtType.getReference()));
    assertTrue(O_B_CtType.isSubtypeOf(O_A_CtType.getReference()));
    assertTrue(P_E_CtType.isSubtypeOf(xCtType.getReference()));
    assertTrue(P_E_CtType.isSubtypeOf(P_D_CtType.getReference()));
    assertTrue(P_E_CtType.isSubtypeOf(O_A_CtType.getReference()));
    assertTrue(P_D_CtType.isSubtypeOf(O_A_CtType.getReference()));
    assertTrue(P_E_CtType.isSubtypeOf(O_B_CtType.getReference()));
    assertTrue(P_E_CtType.isSubtypeOf(objectCtTypeRef));
    assertTrue(P_F_CtType.isSubtypeOf(objectCtTypeRef));
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) X(spoon.test.ctType.testclasses.X) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 32 with CtTypeParameter

use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.

the class TypeReferenceTest method testToStringEqualityBetweenTwoGenericTypeDifferent.

@Test
public void testToStringEqualityBetweenTwoGenericTypeDifferent() throws Exception {
    // contract: generic type references with different bounds should not be considered equals
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--noclasspath" });
    launcher.addInputResource("src/test/java/spoon/test/reference/TypeReferenceTest.java");
    launcher.run();
    Factory factory = launcher.getFactory();
    // New type parameter declaration.
    final CtTypeParameter firstTypeParam = factory.Type().get(A.Tacos.class).getFormalCtTypeParameters().get(0);
    final CtTypeParameter secondTypeParam = factory.Type().get(B.Tacos.class).getFormalCtTypeParameters().get(0);
    assertNotEquals(firstTypeParam.toString(), secondTypeParam.toString());
    assertNotEquals(firstTypeParam, secondTypeParam);
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) Launcher(spoon.Launcher) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 33 with CtTypeParameter

use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.

the class AnnotationTest method testUsageOfTypeAnnotationWithGenericTypesInStatements.

@Test
public void testUsageOfTypeAnnotationWithGenericTypesInStatements() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsAppliedOnAnyTypeInAClass.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    final CtClass<?> ctClass = (CtClass<?>) factory.Type().get("spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass");
    final CtMethod<?> method = ctClass.getMethodsByName("m4").get(0);
    // New type parameter declaration.
    final List<CtTypeParameter> typeParameters = method.getFormalCtTypeParameters();
    assertEquals("Method has 1 generic parameter", 1, typeParameters.size());
    assertEquals("Method with an type annotation must be well printed", "@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T", typeParameters.get(0).toString());
    final CtBlock<?> body = method.getBody();
    final String expectedFirstStatement = "java.util.List<@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T> list = new java.util.ArrayList<>()";
    final CtStatement firstStatement = body.getStatement(0);
    assertEquals("Type annotation on generic parameter declared in the method", expectedFirstStatement, firstStatement.toString());
    final CtConstructorCall firstConstructorCall = firstStatement.getElements(new TypeFilter<CtConstructorCall>(CtConstructorCall.class)).get(0);
    final CtTypeReference<?> firstTypeReference = firstConstructorCall.getType().getActualTypeArguments().get(0);
    assertTrue(firstTypeReference.isImplicit());
    assertEquals("T", firstTypeReference.getSimpleName());
    final String expectedSecondStatement = "java.util.List<@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "?> list2 = new java.util.ArrayList<>()";
    final CtStatement secondStatement = body.getStatement(1);
    assertEquals("Wildcard with an type annotation must be well printed", expectedSecondStatement, secondStatement.toString());
    final CtConstructorCall secondConstructorCall = secondStatement.getElements(new TypeFilter<CtConstructorCall>(CtConstructorCall.class)).get(0);
    final CtTypeReference<?> secondTypeReference = secondConstructorCall.getType().getActualTypeArguments().get(0);
    assertTrue(secondTypeReference.isImplicit());
    assertEquals("Object", secondTypeReference.getSimpleName());
    final String expectedThirdStatement = "java.util.List<spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "BasicAnnotation> list3 = new java.util.ArrayList<spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "BasicAnnotation>()";
    assertEquals("Type in generic parameter with an type annotation must be well printed", expectedThirdStatement, body.getStatement(2).toString());
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtClass(spoon.reflect.declaration.CtClass) CtStatement(spoon.reflect.code.CtStatement) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 34 with CtTypeParameter

use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.

the class ConstructorTest method testTypeAnnotationWithConstructorsOnFormalType.

@Test
public void testTypeAnnotationWithConstructorsOnFormalType() throws Exception {
    final CtConstructor<?> aConstructor = aClass.getConstructor(factory.Type().OBJECT);
    assertEquals(1, aConstructor.getFormalCtTypeParameters().size());
    // New type parameter declaration.
    CtTypeParameter typeParameter = aConstructor.getFormalCtTypeParameters().get(0);
    assertEquals("T", typeParameter.getSimpleName());
    assertEquals(1, typeParameter.getAnnotations().size());
    assertIntersectionTypeInConstructor(typeParameter.getSuperclass());
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) Test(org.junit.Test)

Example 35 with CtTypeParameter

use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.

the class JavaReflectionTreeBuilder method visitTypeParameter.

@Override
public <T extends GenericDeclaration> void visitTypeParameter(TypeVariable<T> parameter) {
    final CtTypeParameter typeParameter = factory.Core().createTypeParameter();
    typeParameter.setSimpleName(parameter.getName());
    enter(new TypeRuntimeBuilderContext(typeParameter));
    super.visitTypeParameter(parameter);
    exit();
    contexts.peek().addFormalType(typeParameter);
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) TypeRuntimeBuilderContext(spoon.support.visitor.java.internal.TypeRuntimeBuilderContext)

Aggregations

CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)43 Test (org.junit.Test)25 MainTest (spoon.test.main.MainTest)12 Factory (spoon.reflect.factory.Factory)10 CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)10 Launcher (spoon.Launcher)9 CtClass (spoon.reflect.declaration.CtClass)9 CtTypeReference (spoon.reflect.reference.CtTypeReference)8 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)7 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)7 ArrayList (java.util.ArrayList)6 CtMethod (spoon.reflect.declaration.CtMethod)6 CtType (spoon.reflect.declaration.CtType)5 LikeCtClass (spoon.test.generics.testclasses2.LikeCtClass)4 SpoonException (spoon.SpoonException)3 CtFormalTypeDeclarer (spoon.reflect.declaration.CtFormalTypeDeclarer)3 CtLambda (spoon.reflect.code.CtLambda)2 CtElement (spoon.reflect.declaration.CtElement)2 CtExecutable (spoon.reflect.declaration.CtExecutable)2 CtInterface (spoon.reflect.declaration.CtInterface)2