Search in sources :

Example 6 with CtModule

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

the class DefaultCoreFactory method createModule.

public CtModule createModule() {
    CtModule module = new CtModuleImpl();
    module.setFactory(getMainFactory());
    this.getMainFactory().Module().getUnnamedModule().addModule(module);
    return module;
}
Also used : CtModuleImpl(spoon.support.reflect.declaration.CtModuleImpl) CtModule(spoon.reflect.declaration.CtModule)

Example 7 with CtModule

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

the class ParentFunction method apply.

@Override
public void apply(CtElement input, CtConsumer<Object> outputConsumer) {
    if (input == null) {
        return;
    }
    if (includingSelf) {
        outputConsumer.accept(input);
    }
    CtElement parent = input;
    CtModule topLevel = input.getFactory().getModel().getUnnamedModule();
    while (parent != null && parent != topLevel && query.isTerminated() == false && parent.isParentInitialized()) {
        parent = parent.getParent();
        outputConsumer.accept(parent);
    }
}
Also used : CtElement(spoon.reflect.declaration.CtElement) CtModule(spoon.reflect.declaration.CtModule)

Example 8 with CtModule

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

the class APITest method testOutputDestinationHandler.

@Test
public void testOutputDestinationHandler() throws IOException {
    // contract: files are created in the directory determined by the output destination handler
    final File outputDest = Files.createTempDirectory("spoon").toFile();
    final OutputDestinationHandler outputDestinationHandler = new OutputDestinationHandler() {

        @Override
        public Path getOutputPath(CtModule module, CtPackage pack, CtType type) {
            String path = "";
            if (module != null) {
                path += module.getSimpleName() + "_";
            }
            if (pack != null) {
                path += pack.getQualifiedName() + "_";
            }
            if (type != null) {
                path += type.getSimpleName() + ".java";
            }
            return new File(outputDest, path).toPath();
        }

        @Override
        public File getDefaultOutputDirectory() {
            return outputDest;
        }
    };
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/api/testclasses/Bar.java");
    launcher.getEnvironment().setOutputDestinationHandler(outputDestinationHandler);
    launcher.run();
    File generatedFile = new File(outputDest, "unnamed module_spoon.test.api.testclasses_Bar.java");
    assertTrue(generatedFile.exists());
}
Also used : CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) File(java.io.File) OutputDestinationHandler(spoon.support.OutputDestinationHandler) CtModule(spoon.reflect.declaration.CtModule) Test(org.junit.Test)

Example 9 with CtModule

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

the class APITest method testOutputDestinationHandlerWithCUFactory.

@Test
public void testOutputDestinationHandlerWithCUFactory() throws IOException {
    // contract: when creating a new CU, its destination is consistent with output destination handler
    final File outputDest = Files.createTempDirectory("spoon").toFile();
    final OutputDestinationHandler outputDestinationHandler = new OutputDestinationHandler() {

        @Override
        public Path getOutputPath(CtModule module, CtPackage pack, CtType type) {
            String path = "";
            if (module != null) {
                path += module.getSimpleName() + "_";
                if (pack == null && type == null) {
                    path += "module-info.java";
                }
            }
            if (pack != null) {
                path += pack.getQualifiedName() + "_";
                if (type == null) {
                    path += "package-info.java";
                }
            }
            if (type != null) {
                path += type.getSimpleName() + ".java";
            }
            return new File(outputDest, path).toPath();
        }

        @Override
        public File getDefaultOutputDirectory() {
            return outputDest;
        }
    };
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setComplianceLevel(9);
    launcher.getEnvironment().setOutputDestinationHandler(outputDestinationHandler);
    Factory factory = launcher.getFactory();
    CtModule module = factory.Module().getOrCreate("simplemodule");
    CompilationUnit cuModule = factory.CompilationUnit().getOrCreate(module);
    CtPackage ctPackage = factory.Package().getOrCreate("my.beautiful.pack");
    module.setRootPackage(factory.Package().get("my"));
    CtType ctType = factory.Class().create("my.beautiful.pack.SuperClass");
    CompilationUnit cuClass = factory.CompilationUnit().getOrCreate(ctType);
    CompilationUnit cuPackage = factory.CompilationUnit().getOrCreate(ctPackage);
    File moduleFile = new File(outputDest.getCanonicalPath(), "simplemodule_module-info.java");
    File packageFile = new File(outputDest.getCanonicalPath(), "simplemodule_my.beautiful.pack_package-info.java");
    File classFile = new File(outputDest.getCanonicalPath(), "simplemodule_my.beautiful.pack_SuperClass.java");
    assertEquals(moduleFile, cuModule.getFile());
    assertEquals(packageFile, cuPackage.getFile());
    assertEquals(classFile, cuClass.getFile());
    Set<String> units = launcher.getFactory().CompilationUnit().getMap().keySet();
    assertEquals(3, units.size());
    assertTrue("Module file not contained (" + moduleFile.getCanonicalPath() + "). \nContent: " + StringUtils.join(units, "\n"), units.contains(moduleFile.getCanonicalPath()));
    assertTrue("Package file not contained (" + packageFile.getCanonicalPath() + "). \nContent: " + StringUtils.join(units, "\n"), units.contains(packageFile.getCanonicalPath()));
    assertTrue("Class file not contained (" + classFile.getCanonicalPath() + "). \nContent: " + StringUtils.join(units, "\n"), units.contains(classFile.getCanonicalPath()));
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtPackage(spoon.reflect.declaration.CtPackage) File(java.io.File) OutputDestinationHandler(spoon.support.OutputDestinationHandler) CtModule(spoon.reflect.declaration.CtModule) Test(org.junit.Test)

Example 10 with CtModule

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

the class TestModule method testDirectiveOrders.

@Test
public void testDirectiveOrders() {
    // contract: module directive should be ordered the same way as in the original file
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.getEnvironment().setComplianceLevel(9);
    launcher.addInputResource(MODULE_RESOURCES_PATH + "/module_with_comments/module-info.java");
    launcher.buildModel();
    assertEquals(2, launcher.getModel().getAllModules().size());
    CtModule module = launcher.getFactory().Module().getModule("module_with_comments");
    assertNotNull(module);
    List<CtModuleDirective> moduleDirectives = module.getModuleDirectives();
    assertEquals(3, moduleDirectives.size());
    assertTrue(moduleDirectives.get(0) instanceof CtModuleRequirement);
    assertTrue(moduleDirectives.get(1) instanceof CtProvidedService);
    assertTrue(moduleDirectives.get(2) instanceof CtUsedService);
}
Also used : CtProvidedService(spoon.reflect.declaration.CtProvidedService) Launcher(spoon.Launcher) CtModuleRequirement(spoon.reflect.declaration.CtModuleRequirement) CtModule(spoon.reflect.declaration.CtModule) CtModuleDirective(spoon.reflect.declaration.CtModuleDirective) CtUsedService(spoon.reflect.declaration.CtUsedService) Test(org.junit.Test)

Aggregations

CtModule (spoon.reflect.declaration.CtModule)18 Test (org.junit.Test)9 Launcher (spoon.Launcher)9 File (java.io.File)5 CtModuleRequirement (spoon.reflect.declaration.CtModuleRequirement)4 CtPackage (spoon.reflect.declaration.CtPackage)4 CtProvidedService (spoon.reflect.declaration.CtProvidedService)4 CtType (spoon.reflect.declaration.CtType)4 CtUsedService (spoon.reflect.declaration.CtUsedService)4 CompilationUnit (spoon.reflect.cu.CompilationUnit)3 CtElement (spoon.reflect.declaration.CtElement)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 SpoonException (spoon.SpoonException)2 CtPackageExport (spoon.reflect.declaration.CtPackageExport)2 CtModuleReference (spoon.reflect.reference.CtModuleReference)2 OutputDestinationHandler (spoon.support.OutputDestinationHandler)2 Iterator (java.util.Iterator)1 ExportsStatement (org.eclipse.jdt.internal.compiler.ast.ExportsStatement)1 OpensStatement (org.eclipse.jdt.internal.compiler.ast.OpensStatement)1