use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class ImportBuilderTest method testWithSimpleImport.
@Test
public void testWithSimpleImport() {
// contract: when the source has one import, the same import is created as a reference in auto-import mode
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/imports/testclasses/ClassWithInvocation.java");
spoon.getEnvironment().setAutoImports(true);
spoon.buildModel();
CtClass classA = spoon.getFactory().Class().get(ClassWithInvocation.class);
CompilationUnit unitA = spoon.getFactory().CompilationUnit().getMap().get(classA.getPosition().getFile().getPath());
Collection<CtImport> imports = unitA.getImports();
assertEquals(1, imports.size());
CtImport ref = imports.iterator().next();
assertEquals("import spoon.test.annotation.testclasses.GlobalAnnotation;\n", ref.toString());
assertTrue(ref.getReference() instanceof CtTypeReference);
CtTypeReference refType = (CtTypeReference) ref.getReference();
assertEquals("spoon.test.annotation.testclasses.GlobalAnnotation", refType.getQualifiedName());
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class JavaOutputProcessor method createJavaFile.
/**
* Creates the Java file associated to the given element. Splits top-level
* classes in different files (even if they are in the same file in the
* original sources).
*/
public void createJavaFile(CtType<?> element) {
Path typePath = getElementPath(element);
getEnvironment().debugMessage("printing " + element.getQualifiedName() + " to " + typePath);
// we only create a file for top-level classes
if (!element.isTopLevel()) {
throw new IllegalArgumentException();
}
CompilationUnit cu = this.getFactory().CompilationUnit().getOrCreate(element);
List<CtType<?>> toBePrinted = new ArrayList<>();
toBePrinted.add(element);
printer.calculate(cu, toBePrinted);
PrintStream stream = null;
// print type
try {
File file = typePath.toFile();
file.createNewFile();
if (!printedFiles.contains(file)) {
printedFiles.add(file);
}
stream = new PrintStream(file);
stream.print(printer.getResult());
for (CtType<?> t : toBePrinted) {
lineNumberMappings.put(t.getQualifiedName(), printer.getLineNumberMapping());
}
stream.close();
} catch (IOException e) {
Launcher.LOGGER.error(e.getMessage(), e);
} finally {
if (stream != null) {
stream.close();
}
}
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class GetBinaryFilesTest method testNestedTypes.
@Test
public void testNestedTypes() throws IOException {
final String input = "./src/test/java/spoon/test/imports/testclasses/internal/PublicInterface2.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(3, binaries.size());
assertEquals("PublicInterface2.class", binaries.get(0).getName());
assertEquals("PublicInterface2$NestedInterface.class", binaries.get(1).getName());
assertEquals("PublicInterface2$NestedClass.class", binaries.get(2).getName());
assertTrue(binaries.get(0).isFile());
assertTrue(binaries.get(1).isFile());
assertTrue(binaries.get(2).isFile());
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class GetBinaryFilesTest method testExistingButNotBuiltBinary.
@Test
public void testExistingButNotBuiltBinary() throws IOException {
tmpFolder.newFolder("compilation");
tmpFolder.newFile("compilation/IBar$Test.class");
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());
final File[] files = binaries.get(0).getParentFile().listFiles();
assertNotNull(files);
assertEquals(2, files.length);
assertTrue(files[0].getName().equals("IBar$Test.class") || files[1].getName().equals("IBar$Test.class"));
}
use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.
the class GetBinaryFilesTest method testMultiClassInSingleFile.
@Test
public void testMultiClassInSingleFile() throws IOException {
final String input = "./src/test/resources/compilation/compilation-tests/";
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(2, cus.size());
final List<File> ibarBinaries = cus.get(new File(input, "IBar.java").getCanonicalFile().getAbsolutePath()).getBinaryFiles();
assertEquals(1, ibarBinaries.size());
assertEquals("IBar.class", ibarBinaries.get(0).getName());
assertTrue(ibarBinaries.get(0).isFile());
final List<File> barBinaries = cus.get(new File(input, "Bar.java").getCanonicalFile().getAbsolutePath()).getBinaryFiles();
assertEquals(2, barBinaries.size());
assertEquals("Bar.class", barBinaries.get(0).getName());
assertEquals("FooEx.class", barBinaries.get(1).getName());
assertTrue(barBinaries.get(0).isFile());
assertTrue(barBinaries.get(1).isFile());
}
Aggregations