mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-06 03:21:12 +07:00
utility methods to specify explicit lambda parameter types
This commit is contained in:
@@ -82,13 +82,8 @@ public class LambdaRefactoringUtil {
|
||||
final PsiType functionalInterfaceType = referenceExpression.getFunctionalInterfaceType();
|
||||
boolean needToSpecifyFormalTypes = !ignoreCast && !isInferredSameTypeAfterConversion(lambdaExpression, referenceExpression, functionalInterfaceType);
|
||||
if (needToSpecifyFormalTypes) {
|
||||
String typedParamList = createLambdaParameterListWithFormalTypes(functionalInterfaceType, lambdaExpression, false);
|
||||
if (typedParamList != null) {
|
||||
PsiParameterList paramListWithFormalTypes = elementFactory.createMethodFromText("void foo" + typedParamList, lambdaExpression).getParameterList();
|
||||
JavaCodeStyleManager.getInstance(lambdaExpression.getProject())
|
||||
.shortenClassReferences(lambdaExpression.getParameterList().replace(paramListWithFormalTypes));
|
||||
}
|
||||
else {
|
||||
PsiParameterList typedParamList = specifyLambdaParameterTypes(functionalInterfaceType, lambdaExpression);
|
||||
if (typedParamList == null) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -329,6 +324,24 @@ public class LambdaRefactoringUtil {
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiParameterList specifyLambdaParameterTypes(PsiLambdaExpression lambdaExpression) {
|
||||
return specifyLambdaParameterTypes(lambdaExpression.getFunctionalInterfaceType(), lambdaExpression);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiParameterList specifyLambdaParameterTypes(PsiType functionalInterfaceType,
|
||||
PsiLambdaExpression lambdaExpression) {
|
||||
String typedParamList = createLambdaParameterListWithFormalTypes(functionalInterfaceType, lambdaExpression, false);
|
||||
if (typedParamList != null) {
|
||||
PsiParameterList paramListWithFormalTypes = JavaPsiFacade.getElementFactory(lambdaExpression.getProject())
|
||||
.createMethodFromText("void foo" + typedParamList, lambdaExpression).getParameterList();
|
||||
return (PsiParameterList)JavaCodeStyleManager.getInstance(lambdaExpression.getProject())
|
||||
.shortenClassReferences(lambdaExpression.getParameterList().replace(paramListWithFormalTypes));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void simplifyToExpressionLambda(@NotNull final PsiLambdaExpression lambdaExpression) {
|
||||
final PsiElement body = lambdaExpression.getBody();
|
||||
final PsiExpression singleExpression = RedundantLambdaCodeBlockInspection.isCodeBlockRedundant(body);
|
||||
|
||||
@@ -16,10 +16,8 @@
|
||||
package com.siyeh.ig.style;
|
||||
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.refactoring.util.LambdaRefactoringUtil;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
@@ -30,7 +28,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class LambdaParameterTypeCanBeSpecifiedInspection extends BaseInspection {
|
||||
private static final Logger LOG = Logger.getInstance("#" + LambdaParameterTypeCanBeSpecifiedInspection.class.getName());
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@@ -56,13 +53,6 @@ public class LambdaParameterTypeCanBeSpecifiedInspection extends BaseInspection
|
||||
return new InferLambdaParameterTypeFix(infos);
|
||||
}
|
||||
|
||||
private static void doFix(@NotNull Project project, @NotNull PsiLambdaExpression lambdaExpression) {
|
||||
final PsiType functionalInterfaceType = lambdaExpression.getFunctionalInterfaceType();
|
||||
final String buf = LambdaRefactoringUtil.createLambdaParameterListWithFormalTypes(functionalInterfaceType, lambdaExpression, false);
|
||||
final PsiMethod methodFromText = JavaPsiFacade.getElementFactory(project).createMethodFromText("void foo" + buf, lambdaExpression);
|
||||
JavaCodeStyleManager.getInstance(project).shortenClassReferences(lambdaExpression.getParameterList().replace(methodFromText.getParameterList()));
|
||||
}
|
||||
|
||||
|
||||
private static class InferLambdaParameterTypeVisitor extends BaseInspectionVisitor {
|
||||
@Override
|
||||
@@ -113,7 +103,7 @@ public class LambdaParameterTypeCanBeSpecifiedInspection extends BaseInspection
|
||||
protected void doFix(Project project, ProblemDescriptor descriptor) {
|
||||
final PsiElement element = descriptor.getPsiElement();
|
||||
if (element instanceof PsiLambdaExpression) {
|
||||
LambdaParameterTypeCanBeSpecifiedInspection.doFix(project, (PsiLambdaExpression)element);
|
||||
LambdaRefactoringUtil.specifyLambdaParameterTypes((PsiLambdaExpression)element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user