Search in sources :

Example 1 with CtImport

use of spoon.reflect.declaration.CtImport in project spoon by INRIA.

the class ImportTest method testImportOfInvocationOfPrivateClass.

@Test
public void testImportOfInvocationOfPrivateClass() throws Exception {
    final Factory factory = getFactory("./src/test/java/spoon/test/imports/testclasses/internal2/Chimichanga.java", "./src/test/java/spoon/test/imports/testclasses/Mole.java");
    ImportScanner importContext = new ImportScannerImpl();
    importContext.computeImports(factory.Class().get(Mole.class));
    Collection<CtImport> imports = importContext.getAllImports();
    assertEquals(1, imports.size());
    assertEquals("import spoon.test.imports.testclasses.internal2.Chimichanga;\n", imports.toArray()[0].toString());
}
Also used : ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) CtImport(spoon.reflect.declaration.CtImport) Factory(spoon.reflect.factory.Factory) ImportScanner(spoon.reflect.visitor.ImportScanner) Mole(spoon.test.imports.testclasses.Mole) Test(org.junit.Test)

Example 2 with CtImport

use of spoon.reflect.declaration.CtImport in project spoon by INRIA.

the class ImportTest method testImportOfInvocationOfStaticMethod.

@Test
public void testImportOfInvocationOfStaticMethod() throws Exception {
    final Factory factory = getFactory("./src/test/java/spoon/test/imports/testclasses/internal2/Menudo.java", "./src/test/java/spoon/test/imports/testclasses/Pozole.java");
    ImportScanner importContext = new ImportScannerImpl();
    importContext.computeImports(factory.Class().get(Pozole.class));
    Collection<CtImport> imports = importContext.getAllImports();
    assertEquals(1, imports.size());
    assertEquals("import spoon.test.imports.testclasses.internal2.Menudo;\n", imports.toArray()[0].toString());
}
Also used : ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) CtImport(spoon.reflect.declaration.CtImport) Pozole(spoon.test.imports.testclasses.Pozole) Factory(spoon.reflect.factory.Factory) ImportScanner(spoon.reflect.visitor.ImportScanner) Test(org.junit.Test)

Example 3 with CtImport

use of spoon.reflect.declaration.CtImport in project spoon by INRIA.

the class ImportTest method testGetImportKindReturnRightValue.

@Test
public void testGetImportKindReturnRightValue() {
    // contract: the importKind is computed based on the reference class type and the boolean isImportAllStaticTypeMembers
    final Launcher spoon = new Launcher();
    CtType aType = spoon.getFactory().Type().get(Reflection.class);
    CtImport ctImport = spoon.getFactory().createImport(aType.getReference());
    assertEquals(CtImportKind.TYPE, ctImport.getImportKind());
    ctImport = spoon.getFactory().createImport(spoon.getFactory().Type().createWildcardStaticTypeMemberReference(aType.getReference()));
    assertEquals(CtImportKind.ALL_STATIC_MEMBERS, ctImport.getImportKind());
    ctImport = spoon.getFactory().createImport(((CtMethod) aType.getAllMethods().iterator().next()).getReference());
    assertEquals(CtImportKind.METHOD, ctImport.getImportKind());
    ctImport = spoon.getFactory().createImport(((CtField) aType.getFields().get(0)).getReference());
    assertEquals(CtImportKind.FIELD, ctImport.getImportKind());
    ctImport = spoon.getFactory().createImport(aType.getPackage().getReference());
    assertEquals(CtImportKind.ALL_TYPES, ctImport.getImportKind());
}
Also used : CtImport(spoon.reflect.declaration.CtImport) CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 4 with CtImport

use of spoon.reflect.declaration.CtImport in project spoon by INRIA.

the class ImportTest method testNotImportExecutableType.

@Test
public void testNotImportExecutableType() throws Exception {
    final Factory factory = getFactory("./src/test/java/spoon/test/imports/testclasses/internal3/Foo.java", "./src/test/java/spoon/test/imports/testclasses/internal3/Bar.java", "./src/test/java/spoon/test/imports/testclasses/NotImportExecutableType.java");
    ImportScanner importContext = new ImportScannerImpl();
    importContext.computeImports(factory.Class().get(NotImportExecutableType.class));
    Collection<CtImport> imports = importContext.getAllImports();
    // java.lang.Object is considered as imported but it will never be output
    assertEquals(3, imports.size());
    Set<String> expectedImports = new HashSet<>(Arrays.asList("spoon.test.imports.testclasses.internal3.Foo", "java.io.File", "java.lang.Object"));
    Set<String> actualImports = imports.stream().map(CtImport::getReference).map(CtReference::toString).collect(Collectors.toSet());
    assertEquals(expectedImports, actualImports);
}
Also used : ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) CtImport(spoon.reflect.declaration.CtImport) NotImportExecutableType(spoon.test.imports.testclasses.NotImportExecutableType) Factory(spoon.reflect.factory.Factory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ImportScanner(spoon.reflect.visitor.ImportScanner) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with CtImport

use of spoon.reflect.declaration.CtImport in project spoon by INRIA.

the class DefaultJavaPrettyPrinter method isImported.

private boolean isImported(CtFieldReference fieldReference) {
    CtImport fieldImport = fieldReference.getFactory().createImport(fieldReference);
    if (this.imports.contains(fieldImport)) {
        return true;
    } else {
        if (fieldReference.getDeclaringType() == null) {
            return false;
        }
        CtTypeReference staticTypeMemberReference = fieldReference.getFactory().Type().createWildcardStaticTypeMemberReference(fieldReference.getDeclaringType());
        CtImport staticClassImport = fieldReference.getFactory().createImport(staticTypeMemberReference);
        return this.imports.contains(staticClassImport);
    }
}
Also used : CtImport(spoon.reflect.declaration.CtImport) CtTypeReference(spoon.reflect.reference.CtTypeReference)

Aggregations

CtImport (spoon.reflect.declaration.CtImport)22 Test (org.junit.Test)15 Launcher (spoon.Launcher)9 ImportScanner (spoon.reflect.visitor.ImportScanner)8 Factory (spoon.reflect.factory.Factory)7 ImportScannerImpl (spoon.reflect.visitor.ImportScannerImpl)6 CompilationUnit (spoon.reflect.cu.CompilationUnit)5 CtClass (spoon.reflect.declaration.CtClass)5 CtTypeReference (spoon.reflect.reference.CtTypeReference)5 CtType (spoon.reflect.declaration.CtType)4 MinimalImportScanner (spoon.reflect.visitor.MinimalImportScanner)4 CtFieldReference (spoon.reflect.reference.CtFieldReference)3 CtPackageReference (spoon.reflect.reference.CtPackageReference)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 CtMethod (spoon.reflect.declaration.CtMethod)2 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)2 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)1 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)1 Minimizer (fr.inria.stamp.minimization.Minimizer)1