Search in sources :

Example 46 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FilterTest method testSubInheritanceHierarchyResolver.

@Test
public void testSubInheritanceHierarchyResolver() throws Exception {
    // contract; SubInheritanceHierarchyResolver supports finding subtypes in an incremental manner
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.buildModel();
    SubInheritanceHierarchyResolver resolver = new SubInheritanceHierarchyResolver(launcher.getModel().getRootPackage());
    // contract: by default, nothing is sent to the consumer
    resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {

        @Override
        public void accept(CtType<?> ctType) {
            fail();
        }
    });
    // we add a type
    resolver.addSuperType(launcher.getFactory().Type().createReference(AbstractTostada.class));
    class Counter {

        int counter = 0;
    }
    Counter c = new Counter();
    resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {

        @Override
        public void accept(CtType<?> ctType) {
            c.counter++;
        }
    });
    // there are 5 subtypes of AbstractTostada
    assertEquals(5, c.counter);
    // we add a type already visited
    resolver.addSuperType(launcher.getFactory().Type().createReference(Tostada.class));
    // nothing is sent to the consumer
    resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {

        @Override
        public void accept(CtType<?> ctType) {
            fail();
        }
    });
    // we add a new type
    resolver.addSuperType(launcher.getFactory().Type().createReference(ITostada.class));
    Counter c2 = new Counter();
    resolver.forEachSubTypeInPackage(new CtConsumer<CtType<?>>() {

        @Override
        public void accept(CtType<?> ctType) {
            c2.counter++;
            assertEquals("spoon.test.filters.testclasses.Tacos", ctType.getQualifiedName());
        }
    });
    // only one subtype remains unvisited
    assertEquals(1, c2.counter);
}
Also used : AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) Tostada(spoon.test.filters.testclasses.Tostada) ITostada(spoon.test.filters.testclasses.ITostada) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) SubTostada(spoon.test.filters.testclasses.SubTostada) CtType(spoon.reflect.declaration.CtType) SubInheritanceHierarchyResolver(spoon.support.visitor.SubInheritanceHierarchyResolver) Launcher(spoon.Launcher) ITostada(spoon.test.filters.testclasses.ITostada) Test(org.junit.Test)

Example 47 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class FilterTest method testOverridingMethodFromSubClassOfInterface.

@Test
public void testOverridingMethodFromSubClassOfInterface() throws Exception {
    // contract: When we ask all overriding methods from an overriding method, we must returns all methods
    // below and not above (including the declaration).
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
    launcher.run();
    final CtClass<AbstractTostada> anAbstractTostada = launcher.getFactory().Class().get(AbstractTostada.class);
    List<CtMethod<?>> overridingMethods = Query.getElements(launcher.getFactory(), new OverridingMethodFilter(anAbstractTostada.getMethodsByName("make").get(0)));
    assertEquals(2, overridingMethods.size());
    assertEquals("spoon.test.filters.testclasses.AbstractTostada$1", overridingMethods.get(0).getParent(CtClass.class).getQualifiedName());
    assertEquals(Tostada.class, overridingMethods.get(1).getParent(CtClass.class).getActualClass());
    final CtClass<Tostada> aTostada = launcher.getFactory().Class().get(Tostada.class);
    overridingMethods = Query.getElements(launcher.getFactory(), new OverridingMethodFilter(aTostada.getMethodsByName("make").get(0)));
    assertEquals(1, overridingMethods.size());
    assertEquals("spoon.test.filters.testclasses.AbstractTostada$1", overridingMethods.get(0).getParent(CtClass.class).getQualifiedName());
}
Also used : AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) Tostada(spoon.test.filters.testclasses.Tostada) ITostada(spoon.test.filters.testclasses.ITostada) AbstractTostada(spoon.test.filters.testclasses.AbstractTostada) SubTostada(spoon.test.filters.testclasses.SubTostada) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) OverridingMethodFilter(spoon.reflect.visitor.filter.OverridingMethodFilter) Test(org.junit.Test)

Example 48 with Launcher

use of spoon.Launcher 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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtInterface(spoon.reflect.declaration.CtInterface) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 49 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class GenericsTest method testInvocationGenerics.

@Test
public void testInvocationGenerics() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/java/spoon/test/generics/testclasses/", "-o", "./target/spooned/" });
    final CtClass<?> aTacos = launcher.getFactory().Class().get(Tacos.class);
    final CtConstructor<?> defaultConstructor = aTacos.getConstructor();
    final CtInvocation<?> explicitConstructorCall = (CtInvocation<?>) defaultConstructor.getBody().getStatement(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
    assertEquals(1, explicitConstructorCall.getExecutable().getActualTypeArguments().size());
    assertEquals("<java.lang.String>this(1)", explicitConstructorCall.toString());
    final CtMethod<?> m = aTacos.getMethodsByName("m2").get(0);
    final CtInvocation invocation1 = m.getBody().getStatement(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
    assertEquals(1, invocation1.getExecutable().getActualTypeArguments().size());
    assertEquals("this.<java.lang.String>makeTacos(null)", invocation1.toString());
    final CtInvocation invocation2 = m.getBody().getStatement(1).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
    assertEquals(0, invocation2.getExecutable().getActualTypeArguments().size());
    assertEquals("this.makeTacos(null)", invocation2.toString());
    canBeBuilt("./target/spooned/spoon/test/generics/testclasses/", 8);
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 50 with Launcher

use of spoon.Launcher in project spoon by INRIA.

the class GenericsTest method testMethodsWithGenericsWhoExtendsObject.

@Test
public void testMethodsWithGenericsWhoExtendsObject() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/java/spoon/test/generics/testclasses/", "-o", "./target/spooned/" });
    final CtClass<?> aTacos = launcher.getFactory().Class().get(Tacos.class);
    final CtMethod<?> m = aTacos.getMethodsByName("m4").get(0);
    final CtInvocation<?> invocation1 = m.getBody().getStatement(0).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
    assertEquals(2, invocation1.getExecutable().getActualTypeArguments().size());
    assertEquals("spoon.test.generics.testclasses.Tacos.<V, C>makeTacos()", invocation1.toString());
    final CtInvocation<?> invocation2 = m.getBody().getStatement(1).getElements(new TypeFilter<>(CtInvocation.class)).get(0);
    assertEquals(0, invocation2.getExecutable().getActualTypeArguments().size());
    assertEquals("spoon.test.generics.testclasses.Tacos.makeTacos()", invocation2.toString());
}
Also used : Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Aggregations

Launcher (spoon.Launcher)524 Test (org.junit.Test)486 Factory (spoon.reflect.factory.Factory)163 CtClass (spoon.reflect.declaration.CtClass)111 CtMethod (spoon.reflect.declaration.CtMethod)79 File (java.io.File)75 CtType (spoon.reflect.declaration.CtType)66 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)60 CtTypeReference (spoon.reflect.reference.CtTypeReference)48 SpoonModelBuilder (spoon.SpoonModelBuilder)44 ArrayList (java.util.ArrayList)43 CtAnnotation (spoon.reflect.declaration.CtAnnotation)38 CtInvocation (spoon.reflect.code.CtInvocation)32 CtElement (spoon.reflect.declaration.CtElement)27 CtPackage (spoon.reflect.declaration.CtPackage)27 FileSystemFile (spoon.support.compiler.FileSystemFile)26 CtStatement (spoon.reflect.code.CtStatement)25 CtField (spoon.reflect.declaration.CtField)24 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)24 SpoonAPI (spoon.SpoonAPI)22