Search in sources :

Example 1 with ImportScanner

use of spoon.reflect.visitor.ImportScanner 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 ImportScanner

use of spoon.reflect.visitor.ImportScanner 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 ImportScanner

use of spoon.reflect.visitor.ImportScanner in project spoon by INRIA.

the class ImportTest method testSpoonWithImports.

@Test
public void testSpoonWithImports() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/java/spoon/test/imports/testclasses", "--output-type", "nooutput", "--with-imports" });
    final CtClass<ImportTest> aClass = launcher.getFactory().Class().get(ChildClass.class);
    final CtClass<ImportTest> anotherClass = launcher.getFactory().Class().get(ClientClass.class);
    final CtClass<ImportTest> classWithInvocation = launcher.getFactory().Class().get(ClassWithInvocation.class);
    ImportScanner importScanner = new ImportScannerImpl();
    importScanner.computeImports(aClass);
    assertEquals(2, importScanner.getAllImports().size());
    importScanner = new ImportScannerImpl();
    importScanner.computeImports(anotherClass);
    // ClientClass needs 2 imports: ChildClass, PublicInterface2
    assertEquals(2, importScanner.getAllImports().size());
    // check that printer did not used the package protected class like "SuperClass.InnerClassProtected"
    assertTrue(anotherClass.toString().indexOf("InnerClass extends ChildClass.InnerClassProtected") > 0);
    importScanner = new ImportScannerImpl();
    importScanner.computeImports(classWithInvocation);
    // java.lang imports are also computed
    assertEquals("Spoon ignores the arguments of CtInvocations", 3, importScanner.getAllImports().size());
}
Also used : ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) Launcher(spoon.Launcher) ImportScanner(spoon.reflect.visitor.ImportScanner) Test(org.junit.Test)

Example 4 with ImportScanner

use of spoon.reflect.visitor.ImportScanner 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 ImportScanner

use of spoon.reflect.visitor.ImportScanner in project dspot by STAMP-project.

the class AmplificationHelper method getImport.

@Deprecated
public static Set<CtType> getImport(CtType type) {
    if (!AmplificationHelper.importByClass.containsKey(type)) {
        ImportScanner importScanner = new ImportScannerImpl();
        try {
            importScanner.computeImports(type);
            Set<CtType> set = importScanner.getAllImports().stream().map(CtImport::getReference).filter(Objects::nonNull).filter(ctElement -> ctElement instanceof CtType).map(ctElement -> (CtType) ctElement).collect(Collectors.toSet());
            AmplificationHelper.importByClass.put(type, set);
        } catch (Exception e) {
            AmplificationHelper.importByClass.put(type, new HashSet<>(0));
        }
    }
    return AmplificationHelper.importByClass.get(type);
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) IntStream(java.util.stream.IntStream) CtPackageReference(spoon.reflect.reference.CtPackageReference) java.util(java.util) Logger(org.slf4j.Logger) ImportScanner(spoon.reflect.visitor.ImportScanner) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) CtInvocation(spoon.reflect.code.CtInvocation) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) CtComment(spoon.reflect.code.CtComment) LoggerFactory(org.slf4j.LoggerFactory) DecimalFormat(java.text.DecimalFormat) ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) Description(org.junit.runner.Description) CtPackage(spoon.reflect.declaration.CtPackage) Collectors(java.util.stream.Collectors) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtType(spoon.reflect.declaration.CtType) TestListener(fr.inria.stamp.test.listener.TestListener) Query(spoon.reflect.visitor.Query) Minimizer(fr.inria.stamp.minimization.Minimizer) CtImport(spoon.reflect.declaration.CtImport) CtMethod(spoon.reflect.declaration.CtMethod) ImportScannerImpl(spoon.reflect.visitor.ImportScannerImpl) CtImport(spoon.reflect.declaration.CtImport) CtType(spoon.reflect.declaration.CtType) ImportScanner(spoon.reflect.visitor.ImportScanner)

Aggregations

ImportScanner (spoon.reflect.visitor.ImportScanner)10 Test (org.junit.Test)9 CtImport (spoon.reflect.declaration.CtImport)8 Factory (spoon.reflect.factory.Factory)8 ImportScannerImpl (spoon.reflect.visitor.ImportScannerImpl)8 MinimalImportScanner (spoon.reflect.visitor.MinimalImportScanner)5 Launcher (spoon.Launcher)4 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)1 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)1 Minimizer (fr.inria.stamp.minimization.Minimizer)1 TestListener (fr.inria.stamp.test.listener.TestListener)1 DecimalFormat (java.text.DecimalFormat)1 java.util (java.util)1 HashSet (java.util.HashSet)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Description (org.junit.runner.Description)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1