use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class DefaultCoreFactory method createCompilationUnit.
public CompilationUnit createCompilationUnit() {
CompilationUnit cu = new CompilationUnitImpl();
cu.setFactory(getMainFactory());
return cu;
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class GetBinaryFilesTest method testAnonymousClasses.
@Test
public void testAnonymousClasses() throws IOException {
final String input = "./src/test/java/spoon/test/secondaryclasses/testclasses/AnonymousClass.java";
final Launcher launcher = new Launcher();
launcher.addInputResource(input);
launcher.setBinaryOutputDirectory(tmpFolder.getRoot());
launcher.buildModel();
launcher.getModelBuilder().compile(SpoonModelBuilder.InputType.FILES);
final Map<String, CompilationUnit> cus = launcher.getFactory().CompilationUnit().getMap();
assertEquals(1, cus.size());
final List<File> binaries = cus.get(new File(input).getCanonicalFile().getAbsolutePath()).getBinaryFiles();
assertEquals(4, binaries.size());
assertEquals("AnonymousClass.class", binaries.get(0).getName());
assertEquals("AnonymousClass$I.class", binaries.get(1).getName());
assertEquals("AnonymousClass$1.class", binaries.get(2).getName());
assertEquals("AnonymousClass$2.class", binaries.get(3).getName());
assertTrue(binaries.get(0).isFile());
assertTrue(binaries.get(1).isFile());
assertTrue(binaries.get(2).isFile());
assertTrue(binaries.get(3).isFile());
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class GetBinaryFilesTest method testSingleBinary.
@Test
public void testSingleBinary() {
final String input = "./src/test/resources/compilation/compilation-tests/IBar.java";
final Launcher launcher = new Launcher();
launcher.addInputResource(input);
launcher.setBinaryOutputDirectory(tmpFolder.getRoot());
launcher.buildModel();
launcher.getModelBuilder().compile(SpoonModelBuilder.InputType.FILES);
final Map<String, CompilationUnit> cus = launcher.getFactory().CompilationUnit().getMap();
assertEquals(1, cus.size());
final List<File> binaries = cus.values().iterator().next().getBinaryFiles();
assertEquals(1, binaries.size());
assertEquals("IBar.class", binaries.get(0).getName());
assertTrue(binaries.get(0).isFile());
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class TestCompilationUnit method testGetUnitTypeWorksWithDeclaredType.
@Test
public void testGetUnitTypeWorksWithDeclaredType() {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/api/testclasses/Bar.java");
launcher.buildModel();
CtType type = launcher.getFactory().Type().get(Bar.class);
CompilationUnit compilationUnit = type.getPosition().getCompilationUnit();
assertEquals(CompilationUnit.UNIT_TYPE.TYPE_DECLARATION, compilationUnit.getUnitType());
}
use of spoon.reflect.cu.CompilationUnit 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()));
}
Aggregations