AnonymousCanBeLambdaInspection#isLambdaForm, minor cleanup (IDEA-CR-14331)

This commit is contained in:
Tagir Valeev
2016-10-17 17:31:29 +07:00
parent e437019267
commit 03354ff73d
2 changed files with 20 additions and 28 deletions
@@ -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;
}
}
}
@@ -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));
}
}
}