Search in sources :

Example 1 with OutputDestinationHandler

use of spoon.support.OutputDestinationHandler 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 2 with OutputDestinationHandler

use of spoon.support.OutputDestinationHandler 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)

Aggregations

File (java.io.File)2 Test (org.junit.Test)2 Launcher (spoon.Launcher)2 CtModule (spoon.reflect.declaration.CtModule)2 CtPackage (spoon.reflect.declaration.CtPackage)2 CtType (spoon.reflect.declaration.CtType)2 OutputDestinationHandler (spoon.support.OutputDestinationHandler)2 CompilationUnit (spoon.reflect.cu.CompilationUnit)1 Factory (spoon.reflect.factory.Factory)1