Search in sources :

Example 91 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class FilterTest method testQueryInQuery.

@Test
public void testQueryInQuery() throws Exception {
    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();
    class Context {

        int count = 0;
    }
    Context context = new Context();
    CtClass<?> cls = launcher.getFactory().Class().get(Tacos.class);
    // first query
    CtQuery allChildPublicClasses = launcher.getFactory().Query().createQuery().filterChildren((CtClass clazz) -> clazz.hasModifier(ModifierKind.PUBLIC));
    // second query,involving the first query
    CtQuery q = launcher.getFactory().Package().getRootPackage().map((CtElement in) -> allChildPublicClasses.setInput(in).list());
    // now the assertions
    q.forEach((CtElement clazz) -> {
        context.count++;
        assertTrue(clazz instanceof CtClass);
        assertTrue(((CtClass<?>) clazz).hasModifier(ModifierKind.PUBLIC));
    });
    assertEquals(6, context.count);
    // reset
    context.count = 0;
    // again second query, but now with CtConsumableFunction
    CtQuery q2 = launcher.getFactory().Package().getRootPackage().map((CtElement in, CtConsumer<Object> out) -> allChildPublicClasses.setInput(in).forEach(out));
    // now the assertions
    q2.forEach((CtElement clazz) -> {
        context.count++;
        assertTrue(clazz instanceof CtClass);
        assertTrue(((CtClass<?>) clazz).hasModifier(ModifierKind.PUBLIC));
    });
    assertEquals(6, context.count);
    // reset
    context.count = 0;
    // again second query, but with low-level circuitry thanks to cast
    CtQuery q3 = launcher.getFactory().Package().getRootPackage().map((in, out) -> ((CtQueryImpl) allChildPublicClasses).evaluate(in, out));
    // now the assertions
    q3.forEach((CtElement clazz) -> {
        context.count++;
        assertTrue(clazz instanceof CtClass);
        assertTrue(((CtClass<?>) clazz).hasModifier(ModifierKind.PUBLIC));
    });
    assertEquals(6, context.count);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtElement(spoon.reflect.declaration.CtElement) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) CtConsumer(spoon.reflect.visitor.chain.CtConsumer) Test(org.junit.Test)

Example 92 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class FilterTest method testQueryStepScannWithConsumer.

@Test
public void testQueryStepScannWithConsumer() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    class Context {

        int counter = 0;
    }
    Context context = new Context();
    CtQuery l_qv = launcher.getFactory().getModel().filterChildren(new TypeFilter<>(CtClass.class));
    assertEquals(0, context.counter);
    l_qv.forEach(cls -> {
        assertTrue(cls instanceof CtClass);
        context.counter++;
    });
    assertTrue(context.counter > 0);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 93 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class FilterTest method testReflectionBasedTypeFilter.

@Test
public void testReflectionBasedTypeFilter() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    // First collect all classes using tested TypeFilter
    List<CtClass<?>> allClasses = launcher.getFactory().Package().getRootPackage().getElements(new TypeFilter<CtClass<?>>(CtClass.class));
    assertTrue(allClasses.size() > 0);
    allClasses.forEach(result -> {
        assertTrue(result instanceof CtClass);
    });
    // then do it using Filter whose type is computed by reflection
    List<CtClass<?>> allClasses2 = launcher.getFactory().Package().getRootPackage().getElements(new Filter<CtClass<?>>() {

        @Override
        public boolean matches(CtClass<?> element) {
            return true;
        }
    });
    assertArrayEquals(allClasses.toArray(), allClasses2.toArray());
    // then do it using Filter implemented by lambda expression
    List<CtClass<?>> allClasses3 = launcher.getFactory().Package().getRootPackage().getElements((CtClass<?> element) -> true);
    assertArrayEquals(allClasses.toArray(), allClasses3.toArray());
    // last try AbstractFilter constructor without class parameter
    final CtClass<Tacos> aTacos = launcher.getFactory().Class().get(Tacos.class);
    final CtInvocation<?> invSize = aTacos.getElements(new AbstractFilter<CtInvocation<?>>() {

        /*no class is needed here*/
        @Override
        public boolean matches(CtInvocation<?> element) {
            if (element.getExecutable() == null) {
                return false;
            }
            return "size".equals(element.getExecutable().getSimpleName()) && super.matches(element);
        }
    }).get(0);
    assertNotNull(invSize);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) Tacos(spoon.test.filters.testclasses.Tacos) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 94 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class FieldAccessTest method testFieldAccessAutoExplicit.

@Test
public void testFieldAccessAutoExplicit() throws Exception {
    CtClass mouse = (CtClass) ModelUtils.buildClass(Mouse.class);
    CtMethod method = mouse.filterChildren((CtMethod m) -> "meth1".equals(m.getSimpleName())).first();
    CtFieldReference ageFR = method.filterChildren((CtFieldReference fr) -> "age".equals(fr.getSimpleName())).first();
    // first is the field printed with implicit "this."
    assertEquals("age", ageFR.getParent().toString());
    // add local variable declaration which hides the field declaration
    method.getBody().insertBegin((CtStatement) mouse.getFactory().createCodeSnippetStatement("int age = 1").compile());
    // now the field access must use explicit "this."
    assertEquals("this.age", ageFR.getParent().toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtFieldReference(spoon.reflect.reference.CtFieldReference) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 95 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class CtBodyHolderTest method testConstructor.

@Test
public void testConstructor() throws Exception {
    Factory factory = build(ClassWithBodies.class, CWBStatementTemplate.class);
    CtClass<?> cwbClass = (CtClass<?>) factory.Type().get(ClassWithBodies.class);
    assertEquals(1, cwbClass.getConstructors().size());
    CtConstructor<?> constructor = cwbClass.getConstructor();
    checkCtBody(constructor, "constructor_body", 1);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) ClassWithBodies(spoon.test.ctBodyHolder.testclasses.ClassWithBodies) Factory(spoon.reflect.factory.Factory) 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