Search in sources :

Example 11 with CtType

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

the class FilterTest method testFunctionQueryStep.

@Test
public void testFunctionQueryStep() 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();
    CtQuery query = launcher.getFactory().Package().getRootPackage().filterChildren((CtClass<?> c) -> {
        return true;
    }).name("filter CtClass only").map((CtClass<?> c) -> c.getSuperInterfaces()).name("super interfaces").map((CtTypeReference<?> iface) -> iface.getTypeDeclaration()).map((CtType<?> iface) -> iface.getAllMethods()).name("allMethods if interface").map((CtMethod<?> method) -> method.getSimpleName().equals("make")).map((CtMethod<?> m) -> m.getType()).map((CtTypeReference<?> t) -> t.getTypeDeclaration());
    ((CtQueryImpl) query).logging(true);
    query.forEach((CtInterface<?> c) -> {
        assertEquals("ITostada", c.getSimpleName());
        context.count++;
    });
    assertTrue(context.count > 0);
}
Also used : CtInterface(spoon.reflect.declaration.CtInterface) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtQuery(spoon.reflect.visitor.chain.CtQuery) Launcher(spoon.Launcher) CtQueryImpl(spoon.reflect.visitor.chain.CtQueryImpl) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 12 with CtType

use of spoon.reflect.declaration.CtType 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 13 with CtType

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

the class ImportTest method testStaticMethodWithDifferentClassSameNameCollision.

@Test
public void testStaticMethodWithDifferentClassSameNameCollision() {
    // contract: when using static method, if there is a collision between class name AND between method names,
    // we can only use the fully qualified name of the class to call the static method
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setAutoImports(true);
    String outputDir = "./target/spooned-apache";
    launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/staticcollision/");
    launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/enums/");
    launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/enum2/");
    launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/LangTestSuite.java");
    launcher.setSourceOutputDirectory(outputDir);
    launcher.getEnvironment().setComplianceLevel(3);
    launcher.run();
    PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
    CtType element = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.apachetestsuite.staticcollision.AllLangTestSuite");
    List<CtType<?>> toPrint = new ArrayList<>();
    toPrint.add(element);
    prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
    String output = prettyPrinter.getResult();
    assertTrue("The file should not contain a static import ", !output.contains("import static spoon.test.imports.testclasses2.apachetestsuite.enum2.EnumTestSuite.suite;"));
    assertTrue("The call to the last EnumTestSuite should be in FQN", output.contains("suite.addTest(spoon.test.imports.testclasses2.apachetestsuite.enum2.EnumTestSuite.suite());"));
    canBeBuilt(outputDir, 3);
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 14 with CtType

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

the class ImportTest method testImportWithGenerics.

@Test
public void testImportWithGenerics() throws IOException {
    // contract: in noclasspath autoimport, we should be able to use generic type
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/import-with-generics/TestWithGenerics.java");
    launcher.getEnvironment().setAutoImports(true);
    launcher.getEnvironment().setShouldCompile(true);
    launcher.getEnvironment().setNoClasspath(true);
    launcher.setSourceOutputDirectory("./target/import-with-generics");
    launcher.run();
    PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
    CtType element = launcher.getFactory().Class().get("spoon.test.imports.testclasses.TestWithGenerics");
    List<CtType<?>> toPrint = new ArrayList<>();
    toPrint.add(element);
    prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
    String output = prettyPrinter.getResult();
    assertTrue(output.contains("import spoon.test.imports.testclasses.withgenerics.Target;"));
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 15 with CtType

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

the class ImportTest method testStaticMethodWithDifferentClassSameNameJava3NoCollision.

@Test
public void testStaticMethodWithDifferentClassSameNameJava3NoCollision() {
    // contract: when there is a collision between class names when using static method, we could not create a static import
    // as it is not compliant with java < 1.5, so we should use fully qualified name of the class
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setAutoImports(true);
    String outputDir = "./target/spooned-staticjava3";
    launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/staticjava3/");
    launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/enums/");
    launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/enum2/");
    launcher.addInputResource("./src/test/resources/spoon/test/imports/testclasses2/apachetestsuite/LangTestSuite.java");
    launcher.setSourceOutputDirectory(outputDir);
    launcher.getEnvironment().setComplianceLevel(3);
    launcher.run();
    PrettyPrinter prettyPrinter = launcher.createPrettyPrinter();
    CtType element = launcher.getFactory().Class().get("spoon.test.imports.testclasses2.apachetestsuite.staticjava3.AllLangTestJava3");
    List<CtType<?>> toPrint = new ArrayList<>();
    toPrint.add(element);
    prettyPrinter.calculate(element.getPosition().getCompilationUnit(), toPrint);
    String output = prettyPrinter.getResult();
    assertFalse("The file should not contain a static import ", output.contains("import static"));
    assertTrue("The call to the last EnumTestSuite should be in FQN", output.contains("suite.addTest(spoon.test.imports.testclasses2.apachetestsuite.enums.EnumTestSuite.suite());"));
    canBeBuilt(outputDir, 3);
}
Also used : PrettyPrinter(spoon.reflect.visitor.PrettyPrinter) DefaultJavaPrettyPrinter(spoon.reflect.visitor.DefaultJavaPrettyPrinter) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) Launcher(spoon.Launcher) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Aggregations

CtType (spoon.reflect.declaration.CtType)134 Test (org.junit.Test)67 Launcher (spoon.Launcher)60 ArrayList (java.util.ArrayList)42 CtMethod (spoon.reflect.declaration.CtMethod)38 CtTypeReference (spoon.reflect.reference.CtTypeReference)30 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)20 File (java.io.File)19 Factory (spoon.reflect.factory.Factory)19 PrettyPrinter (spoon.reflect.visitor.PrettyPrinter)19 List (java.util.List)18 Collectors (java.util.stream.Collectors)17 CtField (spoon.reflect.declaration.CtField)17 CtElement (spoon.reflect.declaration.CtElement)16 CtPackage (spoon.reflect.declaration.CtPackage)16 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)14 IOException (java.io.IOException)12 SpoonException (spoon.SpoonException)12 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)11 Set (java.util.Set)11