use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class GenericsTest method testCtTypeReference_getSuperclass.
@Test
public void testCtTypeReference_getSuperclass() throws Exception {
Factory factory = build(new File("src/test/java/spoon/test/generics/testclasses"));
CtClass<?> ctClassCelebrationLunch = factory.Class().get(CelebrationLunch.class);
CtTypeReference<?> trWeddingLunch_Mole = ctClassCelebrationLunch.filterChildren(new NamedElementFilter<>(CtNamedElement.class, "disgust")).map((CtTypedElement te) -> {
return te.getType();
}).first();
assertEquals("spoon.test.generics.testclasses.CelebrationLunch<java.lang.Integer, java.lang.Long, java.lang.Double>.WeddingLunch<spoon.test.generics.testclasses.Mole>", trWeddingLunch_Mole.toString());
CtType<?> tWeddingLunch_X = trWeddingLunch_Mole.getDeclaration();
CtTypeReference<?> trCelebrationLunch_Tacos_Paella_X = tWeddingLunch_X.getSuperclass();
// current correct behavior of CtType#getSuperclass()
assertEquals("spoon.test.generics.testclasses.CelebrationLunch<" + "spoon.test.generics.testclasses.Tacos, " + "spoon.test.generics.testclasses.Paella, " + "X" + ">", trCelebrationLunch_Tacos_Paella_X.toString());
// current - wrong behavior of CtTypeReference#getSuperclass()
assertEquals("spoon.test.generics.testclasses.CelebrationLunch<" + "spoon.test.generics.testclasses.Tacos, " + "spoon.test.generics.testclasses.Paella, " + "X" + ">", trWeddingLunch_Mole.getSuperclass().toString());
// future suggested behavior of CtTypeReference#getSuperclass() - the 3rd argument is Mole.
// assertEquals("spoon.test.generics.testclasses.CelebrationLunch<"
// + "spoon.test.generics.testclasses.Tacos, "
// + "spoon.test.generics.testclasses.Paella, "
// + "spoon.test.generics.testclasses.Mole"
// + ">",trWeddingLunch_Mole.getSuperclass().toString());
// future suggested behavior of CtTypeReference#getSuperclass()
// assertEquals("spoon.test.generics.testclasses.Lunch<"
// + "spoon.test.generics.testclasses.Mole, "
// + "spoon.test.generics.testclasses.Tacos"
// + ">",trWeddingLunch_Mole.getSuperclass().getSuperclass().toString());
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class GenericsTest method testGenericWithExtendsInDeclaration.
@Test
public void testGenericWithExtendsInDeclaration() throws Exception {
final Factory build = build(Panini.class);
final CtType<Panini> panini = build.Type().get(Panini.class);
final CtMethod<?> apply = panini.getMethodsByName("apply").get(0);
assertEquals(1, apply.getType().getActualTypeArguments().size());
assertEquals("? super java.lang.Object", apply.getType().getActualTypeArguments().get(0).toString());
assertEquals(1, apply.getParameters().get(0).getType().getActualTypeArguments().size());
assertEquals("? extends java.lang.Long", apply.getParameters().get(0).getType().getActualTypeArguments().get(0).toString());
}
use of spoon.reflect.factory.Factory 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.reflect.factory.Factory 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.factory.Factory in project spoon by INRIA.
the class GenericsTest method testWildCardonShadowClass.
@Test
public void testWildCardonShadowClass() throws Exception {
// contract: generics should be treated the same way in shadow classes
// test that apply argument type contains a wildcard
Launcher launcher = new Launcher();
Factory factory = launcher.getFactory();
launcher.addInputResource("src/test/java/spoon/test/generics/testclasses/FakeTpl.java");
launcher.buildModel();
CtInterface<?> fakeTplItf = factory.Interface().get("spoon.test.generics.testclasses.FakeTpl");
checkFakeTpl(fakeTplItf);
// same test with a shadow class
launcher = new Launcher();
factory = launcher.getFactory();
CtInterface<?> fakeTplItf2 = factory.Interface().get(FakeTpl.class);
checkFakeTpl(fakeTplItf2);
}
Aggregations