use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class GenericsTest method testGetDeclarationOfTypeParameterReference.
@Test
public void testGetDeclarationOfTypeParameterReference() {
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/ExtendedPaella.java");
launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses/Paella.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
CtClass extendedPaella = factory.getModel().getElements(new NamedElementFilter<>(CtClass.class, "ExtendedPaella")).get(0);
List<CtTypeParameter> typeParameterList = extendedPaella.getFormalCtTypeParameters();
assertEquals(1, typeParameterList.size());
CtMethod totoMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "toto")).get(0);
CtTypeReference returnTypeToto = totoMethod.getType();
CtTypeReference paramToto = ((CtParameter) totoMethod.getParameters().get(0)).getType();
CtType declaration = returnTypeToto.getDeclaration();
assertSame(typeParameterList.get(0), declaration);
assertSame(typeParameterList.get(0), paramToto.getDeclaration());
CtMethod machinMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "machin")).get(0);
CtTypeReference returnTypeMachin = machinMethod.getType();
List<CtTypeParameter> formalCtTypeParameters = machinMethod.getFormalCtTypeParameters();
assertEquals(1, formalCtTypeParameters.size());
CtType declarationMachin = returnTypeMachin.getDeclaration();
assertNotSame(typeParameterList.get(0), declarationMachin);
assertSame(formalCtTypeParameters.get(0), declarationMachin);
CtClass innerPaella = factory.getModel().getElements(new NamedElementFilter<>(CtClass.class, "InnerPaella")).get(0);
List<CtTypeParameter> innerTypeParametersList = innerPaella.getFormalCtTypeParameters();
assertEquals(typeParameterList.get(0), innerTypeParametersList.get(0).getSuperclass().getDeclaration());
CtMethod innerMachinMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "innerMachin")).get(0);
CtTypeReference returnTypeInnerMachin = innerMachinMethod.getType();
CtTypeReference paramInnerMachinType = ((CtParameter) innerMachinMethod.getParameters().get(0)).getType();
List<CtTypeParameter> innerMachinFormalCtType = innerMachinMethod.getFormalCtTypeParameters();
assertSame(typeParameterList.get(0), returnTypeInnerMachin.getDeclaration());
assertSame(innerMachinFormalCtType.get(0), paramInnerMachinType.getDeclaration());
CtMethod innerTotoMethod = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "innerToto")).get(0);
CtTypeReference returnInnerToto = innerTotoMethod.getType();
CtTypeReference paramInnerToto = ((CtParameter) innerTotoMethod.getParameters().get(0)).getType();
List<CtTypeParameter> innerTotoFormatCtType = innerTotoMethod.getFormalCtTypeParameters();
assertSame(innerTotoFormatCtType.get(0), paramInnerToto.getDeclaration());
assertSame(innerTypeParametersList.get(0), returnInnerToto.getDeclaration());
}
use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class GenericsTest method testModelBuildingSimilarSignatureMethods.
@Test
public void testModelBuildingSimilarSignatureMethods() throws Exception {
CtClass<?> type = build("spoon.test.generics", "SimilarSignatureMethodes");
List<CtNamedElement> methods = type.getElements(new NamedElementFilter<>(CtNamedElement.class, "methode"));
assertEquals(2, methods.size());
CtTypeParameter typeParameter = ((CtMethod<?>) methods.get(0)).getFormalCtTypeParameters().get(0);
assertEquals("E", typeParameter.getSimpleName());
CtParameter<?> param = ((CtMethod<?>) methods.get(0)).getParameters().get(0);
assertEquals("E", param.getType().toString());
}
use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class GenericsTest method testTypeParameterReferenceAsActualTypeArgument.
@Test
public void testTypeParameterReferenceAsActualTypeArgument() throws Exception {
CtType<Tacos> aTacos = buildNoClasspath(ClassThatDefinesANewTypeArgument.class).Type().get(ClassThatDefinesANewTypeArgument.class);
CtTypeReference<?> typeRef = aTacos.getReference();
assertSame(aTacos, typeRef.getDeclaration());
CtTypeParameter typeParam = aTacos.getFormalCtTypeParameters().get(0);
CtTypeParameterReference typeParamRef = typeParam.getReference();
assertSame(typeParam, typeParamRef.getDeclaration());
assertEquals("spoon.test.generics.ClassThatDefinesANewTypeArgument", typeRef.toString());
// creating a reference to "ClassThatDefinesANewTypeArgument<T>"
// this assignment changes parent of typeParamRef to TYPEREF
typeRef.addActualTypeArgument(typeParamRef);
assertEquals("spoon.test.generics.ClassThatDefinesANewTypeArgument<T>", typeRef.toString());
// this does not change the declaration
assertSame(aTacos, typeRef.getDeclaration());
// stored typeParamRef is same like the added one, no clone - OK
assertSame(typeParamRef, typeRef.getActualTypeArguments().get(0));
// typeParamRef has got new parent
assertSame(typeRef, typeParamRef.getParent());
// null because without context
assertEquals(null, typeParamRef.getDeclaration());
assertEquals(typeParam, typeParamRef.getTypeParameterDeclaration());
typeParamRef.setSimpleName("Y");
assertEquals(typeParam, typeParamRef.getTypeParameterDeclaration());
}
use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class GenericsTest method testTypeAdapted.
@Test
public void testTypeAdapted() throws Exception {
// contract: one can get the actual value of a generic type in a given context
CtClass<?> ctModel = (CtClass<?>) ModelUtils.buildClass(ErasureModelA.class);
CtTypeParameter tpA = ctModel.getFormalCtTypeParameters().get(0);
CtTypeParameter tpB = ctModel.getFormalCtTypeParameters().get(1);
CtTypeParameter tpC = ctModel.getFormalCtTypeParameters().get(2);
CtTypeParameter tpD = ctModel.getFormalCtTypeParameters().get(3);
CtClass<?> ctModelB = ctModel.filterChildren(new NamedElementFilter<>(CtClass.class, "ModelB")).first();
ClassTypingContext sth = new ClassTypingContext(ctModelB);
// in ModelB, "A" is "A2"
assertEquals("A2", sth.adaptType(tpA).getQualifiedName());
// in ModelB, "B" is "B2"
assertEquals("B2", sth.adaptType(tpB).getQualifiedName());
// and so on and so forth
assertEquals("C2", sth.adaptType(tpC).getQualifiedName());
assertEquals("D2", sth.adaptType(tpD).getQualifiedName());
CtClass<?> ctModelC = ctModel.filterChildren(new NamedElementFilter<>(CtClass.class, "ModelC")).first();
ClassTypingContext sthC = new ClassTypingContext(ctModelC);
assertEquals("java.lang.Integer", sthC.adaptType(tpA).getQualifiedName());
assertEquals("java.lang.RuntimeException", sthC.adaptType(tpB).getQualifiedName());
assertEquals("java.lang.IllegalArgumentException", sthC.adaptType(tpC).getQualifiedName());
assertEquals("java.util.List", sthC.adaptType(tpD).getQualifiedName());
}
use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class GenericsTest method testModelBuildingGenericConstructor.
@Test
public void testModelBuildingGenericConstructor() throws Exception {
CtClass<?> type = build("spoon.test.generics", "GenericConstructor");
assertEquals("GenericConstructor", type.getSimpleName());
CtTypeParameter typeParameter = type.getElements(new TypeFilter<CtConstructor<?>>(CtConstructor.class)).get(0).getFormalCtTypeParameters().get(0);
assertEquals("E", typeParameter.getSimpleName());
}
Aggregations