Search in sources :

Example 11 with CompilationUnit

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

the class ImportBuilderTest method testWithNoImport.

@Test
public void testWithNoImport() {
    // contract: when the source code has no import, none is created when building model
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/imports/testclasses/A.java");
    spoon.getEnvironment().setAutoImports(true);
    spoon.buildModel();
    CtClass classA = spoon.getFactory().Class().get(A.class);
    CompilationUnit unitA = spoon.getFactory().CompilationUnit().getMap().get(classA.getPosition().getFile().getPath());
    assertTrue(unitA.getImports().isEmpty());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 12 with CompilationUnit

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

the class ImportBuilderTest method testWithStaticStarredImportFromInterface.

@Test
public void testWithStaticStarredImportFromInterface() {
    // contract: when a starred import is used with a target package, all classes of the package should be imported
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/StarredImport.java");
    spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/fullpack/");
    spoon.getEnvironment().setAutoImports(true);
    spoon.buildModel();
    CtClass classA = spoon.getFactory().Class().get(StarredImport.class);
    CompilationUnit unitA = spoon.getFactory().CompilationUnit().getMap().get(classA.getPosition().getFile().getPath());
    Collection<CtImport> imports = unitA.getImports();
    assertEquals(1, imports.size());
    Iterator<CtImport> iterator = imports.iterator();
    CtImport ctImport = iterator.next();
    assertEquals(CtImportKind.ALL_TYPES, ctImport.getImportKind());
    assertTrue(ctImport.getReference() instanceof CtPackageReference);
    CtPackageReference ref = (CtPackageReference) ctImport.getReference();
    assertEquals("spoon.test.jdtimportbuilder.testclasses.fullpack", ref.getQualifiedName());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) CtImport(spoon.reflect.declaration.CtImport) CtPackageReference(spoon.reflect.reference.CtPackageReference) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 13 with CompilationUnit

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

the class ImportBuilderTest method testWithImportFromItf.

@Test
public void testWithImportFromItf() {
    // contract: When using starred static import of an interface, it imports a starred type
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/resources/jdtimportbuilder/");
    spoon.getEnvironment().setAutoImports(true);
    spoon.getEnvironment().setShouldCompile(true);
    spoon.setSourceOutputDirectory("./target/spoon-jdtimport-itfimport");
    spoon.run();
    CtClass classStatic = spoon.getFactory().Class().get("jdtimportbuilder.ItfImport");
    CompilationUnit unitStatic = spoon.getFactory().CompilationUnit().getMap().get(classStatic.getPosition().getFile().getPath());
    Collection<CtImport> imports = unitStatic.getImports();
    assertEquals(1, imports.size());
    CtImport ctImport = imports.iterator().next();
    assertEquals(CtImportKind.ALL_STATIC_MEMBERS, ctImport.getImportKind());
    assertEquals("import static jdtimportbuilder.itf.DumbItf.*;\n", ctImport.toString());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) CtImport(spoon.reflect.declaration.CtImport) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 14 with CompilationUnit

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

the class ImportBuilderTest method testWithSimpleImportNoAutoimport.

@Test
public void testWithSimpleImportNoAutoimport() {
    // contract: when the source has one import, nothing is imported when not in autoimport
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/imports/testclasses/ClassWithInvocation.java");
    spoon.getEnvironment().setAutoImports(false);
    spoon.buildModel();
    CtClass classA = spoon.getFactory().Class().get(ClassWithInvocation.class);
    CompilationUnit unitA = spoon.getFactory().CompilationUnit().getMap().get(classA.getPosition().getFile().getPath());
    assertTrue(unitA.getImports().isEmpty());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 15 with CompilationUnit

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

the class ImportBuilderTest method testWithStaticInheritedImport.

@Test
public void testWithStaticInheritedImport() {
    // contract: When using starred static import of a type, it imports a starred type
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/StaticImportWithInheritance.java");
    spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/staticimport");
    spoon.getEnvironment().setAutoImports(true);
    spoon.getEnvironment().setShouldCompile(true);
    spoon.setSourceOutputDirectory("./target/spoon-jdtimport-inheritedstatic");
    spoon.run();
    CtClass classStatic = spoon.getFactory().Class().get(StaticImportWithInheritance.class);
    CompilationUnit unitStatic = spoon.getFactory().CompilationUnit().getMap().get(classStatic.getPosition().getFile().getPath());
    Collection<CtImport> imports = unitStatic.getImports();
    assertEquals(1, imports.size());
    CtImport ctImport = imports.iterator().next();
    assertEquals(CtImportKind.ALL_STATIC_MEMBERS, ctImport.getImportKind());
    assertEquals("import static spoon.test.jdtimportbuilder.testclasses.staticimport.DependencySubClass.*;\n", ctImport.toString());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) CtImport(spoon.reflect.declaration.CtImport) Launcher(spoon.Launcher) 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