org.openrewrite.java.RemoveUnusedImports Maven / Gradle / Ivy
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java;
import org.openrewrite.Tree;
import org.openrewrite.java.tree.*;
import java.util.*;
import java.util.stream.Stream;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singleton;
import static java.util.stream.Collectors.toSet;
/**
* Assumes imports are ordered. Only meant to be used by {@link OrderImports}.
*/
class RemoveUnusedImports extends JavaIsoRefactorVisitor {
private final int classCountToUseStarImport;
private final int nameCountToUseStarImport;
RemoveUnusedImports(int classCountToUseStarImport, int nameCountToUseStarImport) {
this.classCountToUseStarImport = classCountToUseStarImport;
this.nameCountToUseStarImport = nameCountToUseStarImport;
}
@Override
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu) {
Map> methodsByTypeName = new StaticMethodsByType().visit(cu);
Map> typesByPackage = new TypesByPackage().visit(cu);
boolean changed = false;
// Whenever an import statement is found to be used it should be added to this list
// At the end this list will contain only imports which are actually used
final List importsWithUsage = new ArrayList<>();
for (J.Import anImport : cu.getImports()) {
if (anImport.isStatic()) {
Set methods = methodsByTypeName.get(anImport.getTypeName());
if (methods == null) {
changed = true;
continue;
}
if ("*".equals(anImport.getQualid().getSimpleName())) {
if (methods.size() < nameCountToUseStarImport) {
methods.stream().sorted().forEach(method ->
importsWithUsage.add(anImport.withQualid(anImport.getQualid().withName(anImport.getQualid().getName().withName(method))))
);
changed = true;
} else {
importsWithUsage.add(anImport);
}
} else {
importsWithUsage.add(anImport);
}
} else {
Set types = typesByPackage.get(anImport.getPackageName());
if (types == null) {
changed = true;
continue;
}
if ("*".equals(anImport.getQualid().getSimpleName())) {
if (types.size() < classCountToUseStarImport) {
types.stream().map(JavaType.FullyQualified::getClassName).sorted().forEach(typeClassName ->
importsWithUsage.add(anImport.withQualid(anImport.getQualid().withName(anImport.getQualid().getName()
.withName(typeClassName))))
);
changed = true;
} else {
importsWithUsage.add(anImport);
}
} else {
importsWithUsage.add(anImport);
}
}
}
return changed ? cu.withImports(importsWithUsage) : cu;
}
static class TypesByPackage extends AbstractJavaSourceVisitor