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;
}
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);
}
}
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());
}
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()));
}
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);
}
Aggregations