use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class GenericsTest method testIsSameSignatureWithReferencedGenerics.
@Test
public void testIsSameSignatureWithReferencedGenerics() {
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/generics/testclasses2/SameSignature3.java");
launcher.buildModel();
CtClass ctClass = launcher.getFactory().Class().get(SameSignature3.class);
CtMethod classMethod = (CtMethod) ctClass.getMethodsByName("visitCtConditional").get(0);
CtType<?> iface = launcher.getFactory().Type().get("spoon.test.generics.testclasses2.ISameSignature3");
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));
}
use of spoon.reflect.declaration.CtClass 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.CtClass in project spoon by INRIA.
the class FilterTest method testElementMapConsumableFunction.
// now testing map(CtConsumableFunction)
@Test
public void testElementMapConsumableFunction() throws Exception {
// contract: a method map(CtConsumableFunction) is provided
// a simple consumer.accept() is equivalent to a single return in a CtFunction
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);
// long version
class aFunction implements CtConsumableFunction<CtClass> {
@Override
public void apply(CtClass c, CtConsumer out) {
// equivalent to a single return
out.accept(c.getParent());
}
}
assertEquals(cls.getParent(), cls.map(new aFunction()).list().get(0));
// now the same with Java8 one-liner
assertEquals(cls.getParent(), cls.map((CtClass<?> c, CtConsumer<Object> out) -> out.accept(c.getParent())).list().get(0));
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class FilterTest method testReuseOfQuery.
@Test
public void testReuseOfQuery() throws Exception {
// contract: a query created from an existing element can be reused on other 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);
// by default the query starts with "cls" as input
CtQuery q = cls.map((CtClass c) -> c.getSimpleName());
// high-level assert
assertEquals(cls.getSimpleName(), q.list().get(0));
// low-level assert on implementation
assertEquals(1, ((CtQueryImpl) q).getInputs().size());
assertSame(cls, ((CtQueryImpl) q).getInputs().get(0));
// now changing the input of query to cls2
q.setInput(cls2);
// the input is still cls2
assertEquals(cls2.getSimpleName(), q.list().get(0));
assertEquals(1, ((CtQueryImpl) q).getInputs().size());
assertSame(cls2, ((CtQueryImpl) q).getInputs().get(0));
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class FilterTest method testInvalidQueryStep.
@Test
public void testInvalidQueryStep() throws Exception {
// contract: with default policy an exception is thrown is the input type of a query step
// does not correspond to the output type of the previous step
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();
try {
launcher.getFactory().Package().getRootPackage().filterChildren((CtClass<?> c) -> {
return true;
}).name("step1").map((CtMethod<?> m) -> m).name("invalidStep2").forEach((CtInterface<?> c) -> {
fail();
});
fail();
} catch (ClassCastException e) {
assertTrue(e.getMessage().indexOf("spoon.support.reflect.declaration.CtClassImpl cannot be cast to spoon.reflect.declaration.CtMethod") >= 0);
}
}
Aggregations