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