use of spoon.test.generics.testclasses.Tacos in project spoon by INRIA.
the class GenericsTest method testIsGenericsMethod.
@Test
public void testIsGenericsMethod() throws Exception {
CtType<Tacos> aTacos = buildNoClasspath(Tacos.class).Type().get(Tacos.class);
CtTypeParameter typeParameter = aTacos.getFormalCtTypeParameters().get(0);
assertTrue(typeParameter.isGenerics());
assertTrue(typeParameter.getReference().isGenerics());
CtTypeReference ctTypeReference = aTacos.getSuperInterfaces().toArray(new CtTypeReference[aTacos.getSuperInterfaces().size()])[0];
assertFalse(aTacos.isGenerics());
// this is a generic type reference spoon.test.generics.testclasses.ITacos<V>
assertEquals("spoon.test.generics.testclasses.ITacos<V>", ctTypeReference.toString());
assertTrue(ctTypeReference.isGenerics());
}
use of spoon.test.generics.testclasses.Tacos in project spoon by INRIA.
the class GenericsTest method testMethodTypingContextAdaptMethod.
@Test
public void testMethodTypingContextAdaptMethod() throws Exception {
// core contracts of MethodTypingContext#adaptMethod
Factory factory = build(new File("src/test/java/spoon/test/generics/testclasses"));
CtClass<?> ctClassLunch = factory.Class().get(Lunch.class);
// represents <C> void eatMe(A paramA, B paramB, C paramC){}
CtMethod<?> trLunch_eatMe = ctClassLunch.filterChildren(new NamedElementFilter<>(CtMethod.class, "eatMe")).first();
CtClass<?> ctClassWeddingLunch = factory.Class().get(WeddingLunch.class);
ClassTypingContext ctcWeddingLunch = new ClassTypingContext(ctClassWeddingLunch);
// we all analyze new methods
final MethodTypingContext methodSTH = new MethodTypingContext().setClassTypingContext(ctcWeddingLunch);
// contract: method can be adapted only using MethodTypingContext
methodSTH.setMethod(trLunch_eatMe);
CtMethod<?> adaptedLunchEatMe = (CtMethod<?>) methodSTH.getAdaptationScope();
// contract: adapting of method declared in different scope, returns new method
assertTrue(adaptedLunchEatMe != trLunch_eatMe);
// check that new method is adapted correctly
// is declared in correct class
assertSame(ctClassWeddingLunch, adaptedLunchEatMe.getDeclaringType());
// is not member of the same class (WeddingLunch)
for (CtTypeMember typeMember : ctClassWeddingLunch.getTypeMembers()) {
assertFalse(adaptedLunchEatMe == typeMember);
}
// the name is the same
assertEquals("eatMe", adaptedLunchEatMe.getSimpleName());
// it has the same number of of formal type parameters
assertEquals(1, adaptedLunchEatMe.getFormalCtTypeParameters().size());
assertEquals("C", adaptedLunchEatMe.getFormalCtTypeParameters().get(0).getQualifiedName());
// parameters are correct
assertEquals(3, adaptedLunchEatMe.getParameters().size());
// "A paramA" becomes "X paramA" becomes Lunch%A corresponds to X in WeddingLunch
assertEquals("X", adaptedLunchEatMe.getParameters().get(0).getType().getQualifiedName());
// B paramB becomes Tacos becomes Lunch%B corresponds to Tacos in WeddingLunch (class WeddingLunch<X> extends CelebrationLunch<Tacos, Paella, X>)
assertEquals(Tacos.class.getName(), adaptedLunchEatMe.getParameters().get(1).getType().getQualifiedName());
// "C paramC" stays "C paramC"
assertEquals("C", adaptedLunchEatMe.getParameters().get(2).getType().getQualifiedName());
// contract: adapting of adapted method returns input method
methodSTH.setMethod(adaptedLunchEatMe);
assertSame(adaptedLunchEatMe, methodSTH.getAdaptationScope());
}
use of spoon.test.generics.testclasses.Tacos in project spoon by INRIA.
the class GenericsTest method testGenericsInQualifiedNameInConstructorCall.
@Test
public void testGenericsInQualifiedNameInConstructorCall() throws Exception {
final Launcher launcher = new Launcher();
launcher.run(new String[] { "-i", "./src/test/java/spoon/test/generics/testclasses/", "-o", "./target/spooned/" });
final CtClass<Tacos> aTacos = launcher.getFactory().Class().get(Tacos.class);
final CtType<?> burritos = aTacos.getNestedType("Burritos");
SortedList<CtConstructorCall> elements = new SortedList<CtConstructorCall>(new CtLineElementComparator());
elements.addAll(burritos.getElements(new TypeFilter<>(CtConstructorCall.class)));
assertEquals(3, elements.size());
// Constructor call.
assertEquals(0, elements.get(1).getExecutable().getType().getActualTypeArguments().size());
assertNotNull(elements.get(1).getType().getDeclaringType());
assertEquals("new Pozole()", elements.get(1).toString());
assertEquals(2, elements.get(0).getExecutable().getType().getActualTypeArguments().size());
assertNotNull(elements.get(0).getType().getDeclaringType());
assertEquals("new Burritos<K, V>()", elements.get(0).toString());
// New class.
assertEquals(2, elements.get(2).getExecutable().getType().getActualTypeArguments().size());
assertNotNull(elements.get(2).getType().getDeclaringType());
assertEquals("new Burritos<K, V>() {}", elements.get(2).toString());
}
use of spoon.test.generics.testclasses.Tacos 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());
}
Aggregations