Search in sources :

Example 16 with CompilationUnit

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());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) CtImport(spoon.reflect.declaration.CtImport) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 17 with CompilationUnit

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();
        }
    }
}
Also used : Path(java.nio.file.Path) CompilationUnit(spoon.reflect.cu.CompilationUnit) PrintStream(java.io.PrintStream) CtType(spoon.reflect.declaration.CtType) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File)

Example 18 with CompilationUnit

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());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) Launcher(spoon.Launcher) File(java.io.File) Test(org.junit.Test)

Example 19 with CompilationUnit

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"));
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) Launcher(spoon.Launcher) File(java.io.File) Test(org.junit.Test)

Example 20 with CompilationUnit

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());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) Launcher(spoon.Launcher) File(java.io.File) Test(org.junit.Test)

Aggregations

CompilationUnit (spoon.reflect.cu.CompilationUnit)32 Test (org.junit.Test)22 Launcher (spoon.Launcher)22 File (java.io.File)16 CtClass (spoon.reflect.declaration.CtClass)9 CtType (spoon.reflect.declaration.CtType)6 CtImport (spoon.reflect.declaration.CtImport)5 IOException (java.io.IOException)4 SpoonException (spoon.SpoonException)4 CtModule (spoon.reflect.declaration.CtModule)3 CtPackage (spoon.reflect.declaration.CtPackage)3 ArrayList (java.util.ArrayList)2 SourcePosition (spoon.reflect.cu.SourcePosition)2 PrintStream (java.io.PrintStream)1 Path (java.nio.file.Path)1 CompilationResult (org.eclipse.jdt.internal.compiler.CompilationResult)1 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)1 AbstractVariableDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration)1 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)1 AnnotationMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration)1