Search in sources :

Example 31 with CompilationUnit

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

the class ImportBuilderTest method testInternalImportWhenNoClasspath.

@Test
public void testInternalImportWhenNoClasspath() {
    // contract: in no-classpath anything which is not loaded cannot be imported, even if original source code has imports
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/resources/noclasspath/Attachment.java");
    spoon.getEnvironment().setAutoImports(true);
    spoon.getEnvironment().setNoClasspath(true);
    spoon.buildModel();
    CtClass classA = spoon.getFactory().Class().get("it.feio.android.omninotes.models.Attachment");
    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 32 with CompilationUnit

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

the class ImportBuilderTest method testSimpleStaticImport.

@Test
public void testSimpleStaticImport() {
    // contract: simple static import are imported correctly
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/jdtimportbuilder/testclasses/StaticImport.java");
    spoon.getEnvironment().setAutoImports(true);
    spoon.buildModel();
    CtClass classA = spoon.getFactory().Class().get(StaticImport.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();
    assertTrue(ref.getReference() instanceof CtFieldReference);
    assertEquals("spoon.test.jdtimportbuilder.testclasses.staticimport.Dependency#ANY", ((CtFieldReference) ref.getReference()).getQualifiedName());
}
Also used : CompilationUnit(spoon.reflect.cu.CompilationUnit) CtClass(spoon.reflect.declaration.CtClass) CtImport(spoon.reflect.declaration.CtImport) Launcher(spoon.Launcher) CtFieldReference(spoon.reflect.reference.CtFieldReference) 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