Search in sources :

Example 6 with CompilationUnit

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;
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CompilationUnitImpl(spoon.support.reflect.cu.CompilationUnitImpl)

Example 7 with CompilationUnit

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

Example 8 with CompilationUnit

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

Example 9 with CompilationUnit

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

Example 10 with CompilationUnit

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

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