public void run(final AstNode root) {
if (!(root instanceof CompilationUnit) || _settings.getForceExplicitImports()) {
return;
}
final CompilationUnit compilationUnit = (CompilationUnit) root;
final AstNodeCollection<ImportDeclaration> imports = compilationUnit.getImports();
if (imports.isEmpty())
return;
final Set<String> newImports = new LinkedHashSet<>();
final List<ImportDeclaration> removedImports = new ArrayList<>();
for (final ImportDeclaration oldImport : imports) {
final Identifier importedType = oldImport.getImportIdentifier();
if (importedType != null && !importedType.isNull()) {
final TypeReference type = oldImport.getUserData(Keys.TYPE_REFERENCE);
if (type != null) {
final String packageName = type.getPackageName();
if (!StringUtilities.isNullOrEmpty(packageName)) {
newImports.add(packageName);
}
removedImports.add(oldImport);
}
}
}
if (removedImports.isEmpty()) {
return;
}
final ImportDeclaration lastRemoved = removedImports.get(removedImports.size() - 1);
for (final String packageName : newImports) {
compilationUnit.insertChildAfter(
lastRemoved,
new ImportDeclaration(PackageReference.parse(packageName)),
CompilationUnit.IMPORT_ROLE
);
}