Search in sources :

Example 21 with CompilationUnit

use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.

the class TestCompilationUnit method testGetUnitTypeWorksWithDeclaredPackage.

@Test
public void testGetUnitTypeWorksWithDeclaredPackage() {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/pkg/package-info.java");
    launcher.buildModel();
    CtPackage ctPackage = launcher.getFactory().Package().get("spoon.test.pkg");
    CompilationUnit compilationUnit = ctPackage.getPosition().getCompilationUnit();
    assertEquals(CompilationUnit.UNIT_TYPE.PACKAGE_DECLARATION, compilationUnit.getUnitType());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) Test(org.junit.Test)

Example 22 with CompilationUnit

use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.

the class TestCompilationUnit method testGetUnitTypeWorksWithCreatedObjects.

@Test
public void testGetUnitTypeWorksWithCreatedObjects() {
    final Launcher launcher = new Launcher();
    CtPackage myPackage = launcher.getFactory().Package().getOrCreate("my.package");
    CompilationUnit cu = launcher.getFactory().createCompilationUnit();
    assertEquals(CompilationUnit.UNIT_TYPE.UNKNOWN, cu.getUnitType());
    cu.setDeclaredPackage(myPackage);
    assertEquals(CompilationUnit.UNIT_TYPE.PACKAGE_DECLARATION, cu.getUnitType());
    cu.setDeclaredTypes(Collections.singletonList(launcher.getFactory().createClass()));
    assertEquals(CompilationUnit.UNIT_TYPE.TYPE_DECLARATION, cu.getUnitType());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) Test(org.junit.Test)

Example 23 with CompilationUnit

use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.

the class TestCompilationUnit method testCompilationUnitDeclaredTypes.

@Test
public void testCompilationUnitDeclaredTypes() throws IOException {
    // contract: the list of declared types should be unmodifiable
    File resource = new File("./src/test/java/spoon/test/model/Foo.java");
    final Launcher launcher = new Launcher();
    launcher.addInputResource(resource.getPath());
    launcher.buildModel();
    CompilationUnit cu = launcher.getFactory().CompilationUnit().getOrCreate(resource.getCanonicalPath());
    assertEquals(3, cu.getDeclaredTypes().size());
    List<CtType<?>> typeList = cu.getDeclaredTypes();
    try {
        typeList.remove(0);
        fail();
    } catch (UnsupportedOperationException e) {
    // do nothing
    }
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) File(java.io.File) Test(org.junit.Test)

Example 24 with CompilationUnit

use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.

the class TestCompilationUnit method testAddDeclaredTypeInCU.

@Test
public void testAddDeclaredTypeInCU() throws IOException {
    // contract: when a type is added to a CU, it should also be pretty printed in cu mode
    File resource = new File("./src/test/java/spoon/test/model/Foo.java");
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "compilationunits" });
    launcher.addInputResource(resource.getPath());
    launcher.setSourceOutputDirectory("./target/cu-onemoretype");
    launcher.buildModel();
    CompilationUnit cu = launcher.getFactory().CompilationUnit().getOrCreate(resource.getCanonicalPath());
    assertEquals(3, cu.getDeclaredTypes().size());
    CtType typeBla = launcher.getFactory().Class().create("spoon.test.model.Bla");
    cu.addDeclaredType(typeBla);
    assertEquals(4, cu.getDeclaredTypes().size());
    launcher.prettyprint();
    File output = new File("./target/cu-onemoretype/spoon/test/model/Foo.java");
    List<String> lines = Files.readAllLines(output.toPath());
    String fullContent = StringUtils.join(lines, "\n");
    assertTrue(fullContent.contains("public class Foo"));
    assertTrue(fullContent.contains("class Bar"));
    assertTrue(fullContent.contains("class Baz"));
    assertTrue(fullContent.contains("class Bla"));
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) File(java.io.File) Test(org.junit.Test)

Example 25 with CompilationUnit

use of spoon.reflect.cu.CompilationUnit in project spoon by INRIA.

the class TestCompilationUnit method testIsoEncodingIsSupported.

@Test
public void testIsoEncodingIsSupported() throws Exception {
    File resource = new File("./src/test/resources/noclasspath/IsoEncoding.java");
    String content = new String(Files.readAllBytes(resource.toPath()), "ISO-8859-1");
    Launcher launcher = new Launcher();
    launcher.addInputResource(resource.getPath());
    launcher.getEnvironment().setNoClasspath(true);
    launcher.getEnvironment().setEncoding(Charset.forName("ISO-8859-1"));
    launcher.buildModel();
    CompilationUnit cu = launcher.getFactory().CompilationUnit().getOrCreate(resource.getPath());
    assertEquals(content, cu.getOriginalSourceCode());
}
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