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