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());
}
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());
}
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());
}
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);
}
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);
}
Aggregations