use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class GenericsTest method testTypeParameterReference.
@Test
public void testTypeParameterReference() throws Exception {
Factory factory = build(ClassThatBindsAGenericType.class, ClassThatDefinesANewTypeArgument.class);
CtClass<?> classThatBindsAGenericType = factory.Class().get(ClassThatBindsAGenericType.class);
CtClass<?> classThatDefinesANewTypeArgument = factory.Class().get(ClassThatDefinesANewTypeArgument.class);
CtTypeReference<?> tr1 = classThatBindsAGenericType.getSuperclass();
CtTypeReference<?> trExtends = tr1.getActualTypeArguments().get(0);
CtTypeParameter tr2 = classThatDefinesANewTypeArgument.getFormalCtTypeParameters().get(0);
CtTypeReference<?> tr3 = classThatDefinesANewTypeArgument.getMethodsByName("foo").get(0).getParameters().get(0).getReference().getType();
// an bound type is not an TypeParameterRefernce
assertTrue(!(trExtends instanceof CtTypeParameterReference));
// a used type parameter T is a CtTypeParameterReference
assertTrue(tr3 instanceof CtTypeParameterReference);
assertEquals("File", trExtends.getSimpleName());
assertEquals(java.io.File.class, trExtends.getActualClass());
assertEquals("T", tr2.getSimpleName());
assertEquals("T", tr3.getSimpleName());
}
use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class GenericsTest method testMethodTypingContext.
@Test
public void testMethodTypingContext() throws Exception {
Factory factory = build(new File("src/test/java/spoon/test/generics/testclasses"));
CtClass<?> ctClassWeddingLunch = factory.Class().get(WeddingLunch.class);
CtMethod<?> trWeddingLunch_eatMe = ctClassWeddingLunch.filterChildren(new NamedElementFilter<>(CtMethod.class, "eatMe")).first();
MethodTypingContext methodSTH = new MethodTypingContext().setMethod(trWeddingLunch_eatMe);
// contract: the method typing context provides its scope
assertSame(trWeddingLunch_eatMe, methodSTH.getAdaptationScope());
CtClass<?> ctClassLunch = factory.Class().get(Lunch.class);
CtMethod<?> trLunch_eatMe = ctClassLunch.filterChildren(new NamedElementFilter<>(CtMethod.class, "eatMe")).first();
CtInvocation<?> invokeReserve = factory.Class().get(CelebrationLunch.class).filterChildren(new TypeFilter<>(CtInvocation.class)).select((CtInvocation i) -> "reserve".equals(i.getExecutable().getSimpleName())).first();
MethodTypingContext methodReserveTC = new MethodTypingContext().setInvocation(invokeReserve);
// contract: the method typing context provides its scope
assertSame(invokeReserve.getExecutable().getDeclaration(), methodReserveTC.getAdaptationScope());
// check that MethodTypingContext made from invocation knows actual type arguments of method and all declaring types
// 1) check method actual type argument
CtMethod<?> methodReserve = (CtMethod<?>) invokeReserve.getExecutable().getDeclaration();
CtTypeParameter methodReserve_S = methodReserve.getFormalCtTypeParameters().get(0);
assertEquals("S", methodReserve_S.getSimpleName());
assertEquals("spoon.test.generics.testclasses.Tacos", methodReserveTC.adaptType(methodReserve_S).getQualifiedName());
// 2) check actual type arguments of declaring type `Section`
CtClass classSection = (CtClass) methodReserve.getDeclaringType();
assertEquals("spoon.test.generics.testclasses.CelebrationLunch$WeddingLunch$Section", classSection.getQualifiedName());
CtTypeParameter classSection_Y = classSection.getFormalCtTypeParameters().get(0);
assertEquals("Y", classSection_Y.getSimpleName());
assertEquals("spoon.test.generics.testclasses.Paella", methodReserveTC.adaptType(classSection_Y).getQualifiedName());
// 3) check actual type arguments of declaring type `WeddingLunch`
CtClass classWeddingLunch = (CtClass) classSection.getDeclaringType();
assertEquals("spoon.test.generics.testclasses.CelebrationLunch$WeddingLunch", classWeddingLunch.getQualifiedName());
CtTypeParameter classWeddingLunch_X = classWeddingLunch.getFormalCtTypeParameters().get(0);
assertEquals("X", classWeddingLunch_X.getSimpleName());
assertEquals("spoon.test.generics.testclasses.Mole", methodReserveTC.adaptType(classWeddingLunch_X).getQualifiedName());
// 4) check actual type arguments of declaring type `CelebrationLunch`
CtClass classCelebrationLunch = (CtClass) classWeddingLunch.getDeclaringType();
assertEquals("spoon.test.generics.testclasses.CelebrationLunch", classCelebrationLunch.getQualifiedName());
CtTypeParameter classCelebrationLunch_K = classCelebrationLunch.getFormalCtTypeParameters().get(0);
CtTypeParameter classCelebrationLunch_L = classCelebrationLunch.getFormalCtTypeParameters().get(1);
CtTypeParameter classCelebrationLunch_M = classCelebrationLunch.getFormalCtTypeParameters().get(2);
assertEquals("K", classCelebrationLunch_K.getSimpleName());
assertEquals("L", classCelebrationLunch_L.getSimpleName());
assertEquals("M", classCelebrationLunch_M.getSimpleName());
assertEquals("spoon.test.generics.testclasses.Tacos", methodReserveTC.adaptType(classCelebrationLunch_K).getQualifiedName());
assertEquals("spoon.test.generics.testclasses.Paella", methodReserveTC.adaptType(classCelebrationLunch_L).getQualifiedName());
assertEquals("spoon.test.generics.testclasses.Mole", methodReserveTC.adaptType(classCelebrationLunch_M).getQualifiedName());
// method->Section->WeddingLunch->CelebrationLunch
GenericTypeAdapter celebrationLunchTC = methodReserveTC.getEnclosingGenericTypeAdapter().getEnclosingGenericTypeAdapter().getEnclosingGenericTypeAdapter();
assertEquals("java.lang.Integer", celebrationLunchTC.adaptType(classCelebrationLunch_K).getQualifiedName());
assertEquals("java.lang.Long", celebrationLunchTC.adaptType(classCelebrationLunch_L).getQualifiedName());
assertEquals("java.lang.Double", celebrationLunchTC.adaptType(classCelebrationLunch_M).getQualifiedName());
}
use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class GenericsTest method testModelBuildingTree.
@Test
public void testModelBuildingTree() throws Exception {
CtClass<?> type = build("spoon.test.generics", "Tree");
assertEquals("Tree", type.getSimpleName());
// New type parameter declaration.
CtTypeParameter typeParameter = type.getFormalCtTypeParameters().get(0);
assertEquals("V", typeParameter.getSimpleName());
assertEquals("[java.io.Serializable, java.lang.Comparable<V>]", typeParameter.getSuperclass().asCtIntersectionTypeReference().getBounds().toString());
CtMethod<?> node5 = type.getElements(new NamedElementFilter<>(CtMethod.class, "node5")).get(0);
assertEquals("this.<java.lang.Class<? extends java.lang.Throwable>>foo()", node5.getBody().getStatement(0).toString());
}
use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class ElementPrinterHelper method writeFormalTypeParameters.
/**
* Writes formal type parameters given in parameter.
*
* @param ctFormalTypeDeclarer
* Reference with formal type arguments.
*/
public void writeFormalTypeParameters(CtFormalTypeDeclarer ctFormalTypeDeclarer) {
final Collection<CtTypeParameter> parameters = ctFormalTypeDeclarer.getFormalCtTypeParameters();
if (parameters == null) {
return;
}
if (parameters.size() > 0) {
try (ListPrinter lp = createListPrinter(false, "<", false, false, ",", true, false, ">")) {
for (CtTypeParameter parameter : parameters) {
lp.printSeparatorIfAppropriate();
prettyPrinter.scan(parameter);
}
}
}
}
use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.
the class CtTypeImpl method setFormalCtTypeParameters.
@Override
public <C extends CtFormalTypeDeclarer> C setFormalCtTypeParameters(List<CtTypeParameter> formalTypeParameters) {
getFactory().getEnvironment().getModelChangeListener().onListDeleteAll(this, TYPE_PARAMETER, formalCtTypeParameters, new ArrayList<>(formalCtTypeParameters));
if (formalTypeParameters == null || formalTypeParameters.isEmpty()) {
this.formalCtTypeParameters = CtElementImpl.emptyList();
return (C) this;
}
if (this.formalCtTypeParameters == CtElementImpl.<CtTypeParameter>emptyList()) {
this.formalCtTypeParameters = new ArrayList<>(TYPE_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
}
this.formalCtTypeParameters.clear();
for (CtTypeParameter formalTypeParameter : formalTypeParameters) {
addFormalCtTypeParameter(formalTypeParameter);
}
return (C) this;
}
Aggregations