mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
AnonymousCanBeLambdaInspection#isLambdaForm, minor cleanup (IDEA-CR-14331)
This commit is contained in:
+7
-13
@@ -195,9 +195,10 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection
|
||||
return canBeConvertedToLambda(aClass, acceptParameterizedFunctionTypes, true, ignoredRuntimeAnnotations);
|
||||
}
|
||||
|
||||
public static boolean isClassAndMethodSuitableForConversion(PsiAnonymousClass aClass,
|
||||
PsiMethod method,
|
||||
Set<String> ignoredRuntimeAnnotations) {
|
||||
public static boolean isLambdaForm(PsiAnonymousClass aClass, Set<String> ignoredRuntimeAnnotations) {
|
||||
PsiMethod[] methods = aClass.getMethods();
|
||||
if(methods.length != 1) return false;
|
||||
PsiMethod method = methods[0];
|
||||
return aClass.getFields().length == 0 &&
|
||||
aClass.getInnerClasses().length == 0 &&
|
||||
aClass.getInitializers().length == 0 &&
|
||||
@@ -222,16 +223,9 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection
|
||||
}
|
||||
final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(resolveResult);
|
||||
if (interfaceMethod != null && (acceptParameterizedFunctionTypes || !interfaceMethod.hasTypeParameters())) {
|
||||
final PsiMethod[] methods = aClass.getMethods();
|
||||
if (methods.length == 1) {
|
||||
final PsiMethod method = methods[0];
|
||||
if (isClassAndMethodSuitableForConversion(aClass, method, ignoredRuntimeAnnotations)) {
|
||||
final PsiType inferredType = getInferredType(aClass, method);
|
||||
if (inferredType == null) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (isLambdaForm(aClass, ignoredRuntimeAnnotations)) {
|
||||
final PsiMethod method = aClass.getMethods()[0];
|
||||
return getInferredType(aClass, method) != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+13
-15
@@ -66,21 +66,19 @@ public class AnonymousHasLambdaAlternativeInspection extends BaseJavaBatchLocalI
|
||||
@Override
|
||||
public void visitAnonymousClass(final PsiAnonymousClass aClass) {
|
||||
super.visitAnonymousClass(aClass);
|
||||
PsiMethod[] methods = aClass.getMethods();
|
||||
if(methods.length == 1) {
|
||||
PsiMethod method = methods[0];
|
||||
PsiExpressionList argumentList = aClass.getArgumentList();
|
||||
if (AnonymousCanBeLambdaInspection.isClassAndMethodSuitableForConversion(aClass, method, Collections.emptySet()) &&
|
||||
argumentList != null && argumentList.getExpressions().length == 0) {
|
||||
PsiClassType type = aClass.getBaseClassType();
|
||||
AnonymousLambdaAlternative alternative = getAlternative(type.resolve(), method);
|
||||
if(alternative != null) {
|
||||
final PsiElement lBrace = aClass.getLBrace();
|
||||
LOG.assertTrue(lBrace != null);
|
||||
final TextRange rangeInElement = new TextRange(0, lBrace.getStartOffsetInParent() + aClass.getStartOffsetInParent() - 1);
|
||||
holder.registerProblem(aClass.getParent(), "Anonymous #ref #loc can be replaced with "+alternative.myReplacementMessage,
|
||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL, rangeInElement, new ReplaceWithLambdaAlternativeFix(alternative));
|
||||
}
|
||||
PsiExpressionList argumentList = aClass.getArgumentList();
|
||||
if (AnonymousCanBeLambdaInspection.isLambdaForm(aClass, Collections.emptySet()) &&
|
||||
argumentList != null &&
|
||||
argumentList.getExpressions().length == 0) {
|
||||
PsiMethod method = aClass.getMethods()[0];
|
||||
PsiClassType type = aClass.getBaseClassType();
|
||||
AnonymousLambdaAlternative alternative = getAlternative(type.resolve(), method);
|
||||
if(alternative != null) {
|
||||
final PsiElement lBrace = aClass.getLBrace();
|
||||
LOG.assertTrue(lBrace != null);
|
||||
final TextRange rangeInElement = new TextRange(0, lBrace.getStartOffsetInParent() + aClass.getStartOffsetInParent() - 1);
|
||||
holder.registerProblem(aClass.getParent(), "Anonymous #ref #loc can be replaced with "+alternative.myReplacementMessage,
|
||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL, rangeInElement, new ReplaceWithLambdaAlternativeFix(alternative));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user