PsiDiamondTypeUtil#removeRedundantTypeArguments

This commit is contained in:
Tagir Valeev
2016-11-10 10:15:48 +07:00
committed by Tagir Valeev
parent 9848dbbad5
commit 898b985223
2 changed files with 31 additions and 31 deletions
@@ -110,39 +110,10 @@ abstract class MigrateToStreamFix implements LocalQuickFix {
static void simplifyAndFormat(@NotNull Project project, PsiElement result) {
if (result == null) return;
LambdaCanBeMethodReferenceInspection.replaceAllLambdasWithMethodReferences(result);
removeRedundantTypeArguments(project, result);
PsiDiamondTypeUtil.removeRedundantTypeArguments(result);
CodeStyleManager.getInstance(project).reformat(JavaCodeStyleManager.getInstance(project).shortenClassReferences(result));
}
private static void removeRedundantTypeArguments(@NotNull Project project, PsiElement result) {
PsiElement[] typedCalls = PsiTreeUtil.collectElements(result, e -> {
if (!(e instanceof PsiMethodCallExpression)) return false;
PsiMethodCallExpression call = (PsiMethodCallExpression)e;
if (call.getTypeArguments().length == 0) return false;
PsiMethod method = call.resolveMethod();
if (method == null) return false;
PsiClass aClass = method.getContainingClass();
if (aClass == null) return false;
String className = aClass.getQualifiedName();
// We remove only those which related to Stream API calls trying to preserve ones which were originally in code
return className != null && className.startsWith("java.util.stream.");
});
for(PsiElement typedCall : typedCalls) {
PsiMethodCallExpression call = (PsiMethodCallExpression)typedCall;
PsiType[] arguments = call.getTypeArguments();
PsiMethod method = call.resolveMethod();
if(method != null) {
PsiTypeParameter[] parameters = method.getTypeParameters();
if(arguments.length == parameters.length &&
PsiDiamondTypeUtil.areTypeArgumentsRedundant(arguments, call, false, method, parameters)) {
PsiMethodCallExpression expr =
(PsiMethodCallExpression)JavaPsiFacade.getInstance(project).getElementFactory().createExpressionFromText("foo()", null);
call.getTypeArgumentList().replace(expr.getTypeArgumentList());
}
}
}
}
static void restoreComments(PsiLoopStatement loopStatement, PsiStatement body) {
final PsiElement parent = loopStatement.getParent();
for (PsiElement comment : PsiTreeUtil.findChildrenOfType(body, PsiComment.class)) {