Search in sources :

Example 86 with CtClass

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));
}
Also used : LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) ClassTypingContext(spoon.support.visitor.ClassTypingContext) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 87 with CtClass

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());
}
Also used : LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) ClassTypingContext(spoon.support.visitor.ClassTypingContext) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) ErasureModelA(spoon.test.ctType.testclasses.ErasureModelA) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 88 with CtClass

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));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtConsumableFunction(spoon.reflect.visitor.chain.CtConsumableFunction) Launcher(spoon.Launcher) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) Test(org.junit.Test)

Example 89 with CtClass

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));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) Test(org.junit.Test)

Example 90 with CtClass

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);
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtInterface(spoon.reflect.declaration.CtInterface) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

CtClass (spoon.reflect.declaration.CtClass)168 Test (org.junit.Test)151 Launcher (spoon.Launcher)102 Factory (spoon.reflect.factory.Factory)84 CtMethod (spoon.reflect.declaration.CtMethod)42 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)22 CtAnnotation (spoon.reflect.declaration.CtAnnotation)19 SpoonModelBuilder (spoon.SpoonModelBuilder)17 CtInvocation (spoon.reflect.code.CtInvocation)16 File (java.io.File)15 CtTypeReference (spoon.reflect.reference.CtTypeReference)15 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)15 Annotation (java.lang.annotation.Annotation)14 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)14 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)14 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)14 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)14 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)14 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)14 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)14