use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class FilterTest method testReuseOfBaseQuery.
@Test
public void testReuseOfBaseQuery() throws Exception {
// contract: an empty query can be used on several inputs
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
CtClass<?> cls = launcher.getFactory().Class().get(Tacos.class);
CtClass<?> cls2 = launcher.getFactory().Class().get(Tostada.class);
// here is the query
CtQuery q = launcher.getFactory().Query().createQuery().map((CtClass c) -> c.getSimpleName());
// using it on a first input
assertEquals("Tacos", q.setInput(cls).list().get(0));
// using it on a second input
assertEquals("Tostada", q.setInput(cls2).list().get(0));
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class GenericsTest method testClassContextOnInnerClass.
@Test
public void testClassContextOnInnerClass() throws Exception {
CtClass<?> classBanana = (CtClass<?>) buildClass(Banana.class);
CtClass<?> classVitamins = classBanana.getNestedType("Vitamins");
CtTypeReference<?> refList_T = classVitamins.getSuperclass();
// contract: generic types defined in enclocing classe (Banana<T>) are resolved from inner class hierarchy (Vitamins->List<T>) too.
assertSame(classBanana.getFormalCtTypeParameters().get(0), new ClassTypingContext(classVitamins).adaptType(refList_T.getActualTypeArguments().get(0)).getDeclaration());
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class GenericsTest method testAccessToGenerics.
@Test
public void testAccessToGenerics() throws Exception {
Launcher spoon = new Launcher();
Factory factory = spoon.createFactory();
SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/generics/Foo.java", "./src/test/java/spoon/test/generics/Bar.java"));
compiler.build();
CtClass<?> foo = (CtClass<?>) factory.Type().get(Foo.class);
CtInterface<?> bar = (CtInterface<?>) factory.Type().get(Bar.class);
final CtNewClass<?> newAnonymousBar = foo.getElements(new AbstractFilter<CtNewClass<?>>(CtNewClass.class) {
@Override
public boolean matches(CtNewClass<?> element) {
return element.getAnonymousClass() != null && element.getAnonymousClass().isAnonymous();
}
}).get(0);
final List<CtTypeParameter> barTypeParamGenerics = bar.getFormalCtTypeParameters();
final CtTypeReference<?> anonymousBar = newAnonymousBar.getType();
assertEquals("Name of the first generic parameter in Bar interface must to be I.", "I", barTypeParamGenerics.get(0).getSimpleName());
assertEquals("Name of the first generic parameter in Bar usage must to be K.", "K", anonymousBar.getActualTypeArguments().get(0).getSimpleName());
assertEquals("Name of the second generic parameter in Bar interface must to be O.", "O", barTypeParamGenerics.get(1).getSimpleName());
assertEquals("Name of the second generic parameter in Bar usage must to be V.", "V", anonymousBar.getActualTypeArguments().get(1).getSimpleName());
}
use of spoon.reflect.declaration.CtClass 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.CtClass in project spoon by INRIA.
the class GenericsTest method testIsSameSignatureWithMethodGenerics.
@Test
public void testIsSameSignatureWithMethodGenerics() {
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses2/SameSignature2.java");
launcher.buildModel();
CtClass ctClass = launcher.getFactory().Class().get(SameSignature2.class);
CtMethod classMethod = (CtMethod) ctClass.getMethodsByName("visitCtConditional").get(0);
CtType<?> iface = launcher.getFactory().Type().get("spoon.test.generics.testclasses2.ISameSignature");
CtMethod ifaceMethod = (CtMethod) iface.getMethodsByName("visitCtConditional").get(0);
ClassTypingContext ctcSub = new ClassTypingContext(ctClass.getReference());
assertTrue(ctcSub.isOverriding(classMethod, ifaceMethod));
assertTrue(ctcSub.isOverriding(ifaceMethod, classMethod));
assertTrue(ctcSub.isSubSignature(classMethod, ifaceMethod));
assertTrue(ctcSub.isSubSignature(ifaceMethod, classMethod));
assertTrue(ctcSub.isSameSignature(classMethod, ifaceMethod));
assertTrue(ctcSub.isSameSignature(ifaceMethod, classMethod));
}
Aggregations