guava type migration: convert anonymous Function-s with runtime retention policy annotations to anonymous classes except @javax.annotations.Nullable (can be configured in settings)

This commit is contained in:
Dmitry Batkovich
2016-03-22 14:46:28 +03:00
parent b19935c978
commit 0790362737
21 changed files with 323 additions and 58 deletions
@@ -100,7 +100,7 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection
final PsiElement lambdaContext = parent != null ? parent.getParent() : null;
if (lambdaContext != null &&
(LambdaUtil.isValidLambdaContext(lambdaContext) || !(lambdaContext instanceof PsiExpressionStatement)) &&
canBeConvertedToLambda(aClass, false, reportNotAnnotatedInterfaces)) {
canBeConvertedToLambda(aClass, false, reportNotAnnotatedInterfaces, Collections.emptySet())) {
final PsiElement lBrace = aClass.getLBrace();
LOG.assertTrue(lBrace != null);
final TextRange rangeInElement = new TextRange(0, aClass.getStartOffsetInParent() + lBrace.getStartOffsetInParent());
@@ -111,12 +111,15 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection
};
}
private static boolean hasRuntimeAnnotations(PsiMethod method) {
private static boolean hasRuntimeAnnotations(PsiMethod method, @NotNull Set<String> runtimeAnnotationsToIgnore) {
PsiAnnotation[] annotations = method.getModifierList().getAnnotations();
for (PsiAnnotation annotation : annotations) {
PsiJavaCodeReferenceElement ref = annotation.getNameReferenceElement();
PsiElement target = ref != null ? ref.resolve() : null;
if (target instanceof PsiClass) {
if (runtimeAnnotationsToIgnore.contains(((PsiClass)target).getQualifiedName())) {
continue;
}
final PsiAnnotation retentionAnno = AnnotationUtil.findAnnotation((PsiClass)target, Retention.class.getName());
if (retentionAnno != null) {
PsiAnnotationMemberValue value = retentionAnno.findAttributeValue("value");
@@ -178,13 +181,16 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection
return null;
}
public static boolean canBeConvertedToLambda(PsiAnonymousClass aClass, boolean acceptParameterizedFunctionTypes) {
return canBeConvertedToLambda(aClass, acceptParameterizedFunctionTypes, true);
public static boolean canBeConvertedToLambda(PsiAnonymousClass aClass,
boolean acceptParameterizedFunctionTypes,
@NotNull Set<String> ignoredRuntimeAnnotations) {
return canBeConvertedToLambda(aClass, acceptParameterizedFunctionTypes, true, ignoredRuntimeAnnotations);
}
public static boolean canBeConvertedToLambda(PsiAnonymousClass aClass,
boolean acceptParameterizedFunctionTypes,
boolean reportNotAnnotatedInterfaces) {
boolean reportNotAnnotatedInterfaces,
@NotNull Set<String> ignoredRuntimeAnnotations) {
if (PsiUtil.getLanguageLevel(aClass).isAtLeast(LanguageLevel.JDK_1_8)) {
final PsiClassType baseClassType = aClass.getBaseClassType();
final PsiClassType.ClassResolveResult resolveResult = baseClassType.resolveGenerics();
@@ -203,7 +209,7 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection
final PsiMethod method = methods[0];
return method.getBody() != null &&
!hasForbiddenRefsInsideBody(method, aClass) &&
!hasRuntimeAnnotations(method) &&
!hasRuntimeAnnotations(method, ignoredRuntimeAnnotations) &&
!method.hasModifierProperty(PsiModifier.SYNCHRONIZED);
}
}
@@ -29,6 +29,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.Collections;
/**
* User: anna
@@ -76,7 +77,7 @@ public class AnonymousCanBeMethodReferenceInspection extends BaseJavaBatchLocalI
@Override
public void visitAnonymousClass(PsiAnonymousClass aClass) {
super.visitAnonymousClass(aClass);
if (AnonymousCanBeLambdaInspection.canBeConvertedToLambda(aClass, true, reportNotAnnotatedInterfaces)) {
if (AnonymousCanBeLambdaInspection.canBeConvertedToLambda(aClass, true, reportNotAnnotatedInterfaces, Collections.emptySet())) {
final PsiMethod method = aClass.getMethods()[0];
final PsiCodeBlock body = method.getBody();
final PsiCallExpression callExpression =