import com.intellij.psi.PsiFile; //导入方法依赖的package包/类
private void removeRedundantAnnotationsInPackage(Project project, PsiFile[] files, PsiAnnotation annotation) {
if (!removeRedundantAnnotations)
{
return;
}
List redundantAnnotations = nullable
? NullableNotNullManager.getInstance(project).getNullables()
: NullableNotNullManager.getInstance(project).getNotNulls();
Set targetsForDefaultAnnotation = targetTypesForDefault(annotation);
if (targetsForDefaultAnnotation == null) {
return;
}
for (PsiFile file : files) {
if (file instanceof PsiJavaFile) {
JavaElementVisitor visitor = new JavaElementVisitor() {
@Override
public void visitMethod(@Nonnull PsiMethod method) {
removeRedundantAnnotations(method, redundantAnnotations, targetsForDefaultAnnotation);
}
@Override
public void visitParameter(@Nonnull PsiParameter parameter) {
removeRedundantAnnotations(parameter, redundantAnnotations, targetsForDefaultAnnotation);
}
@Override
public void visitField(@Nonnull PsiField field) {
removeRedundantAnnotations(field, redundantAnnotations, targetsForDefaultAnnotation);
}
@Override
public void visitLocalVariable(@Nonnull PsiLocalVariable variable) {
super.visitLocalVariable(variable);
}
};
PsiRecursiveElementWalkingVisitor recursiveVisitor = new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(@Nonnull PsiElement element) {
element.accept(visitor);
super.visitElement(element);
}
};
file.accept(recursiveVisitor);
JavaCodeStyleManager.getInstance(project).optimizeImports(file);
}
}
}