From c138ff43f1d0d09422454689c7f5ebed24914c69 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Fri, 12 Dec 2014 14:13:42 +0100 Subject: [PATCH] register functional interfaces from guava, trove, common collections --- .../FunctionalInterfaceSuggester.java | 54 ++++++++++++++++--- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/FunctionalInterfaceSuggester.java b/java/java-impl/src/com/intellij/codeInsight/FunctionalInterfaceSuggester.java index a06ff51072b1..101af8d94fb2 100644 --- a/java/java-impl/src/com/intellij/codeInsight/FunctionalInterfaceSuggester.java +++ b/java/java-impl/src/com/intellij/codeInsight/FunctionalInterfaceSuggester.java @@ -33,6 +33,37 @@ import org.jetbrains.annotations.Nullable; import java.util.*; public class FunctionalInterfaceSuggester { + public static final String[] FUNCTIONAL_INTERFACES = { + //old jdk without annotations + CommonClassNames.JAVA_LANG_RUNNABLE, + CommonClassNames.JAVA_UTIL_CONCURRENT_CALLABLE, + CommonClassNames.JAVA_UTIL_COMPARATOR, + + //IDEA + "com.intellij.util.Function", + "com.intellij.util.Consumer", + "com.intellij.openapi.util.Computable", + "com.intellij.openapi.util.Condition", + "com.intellij.util.Processor", + "com.intellij.util.Producer", + + //guava + "com.google.common.base.Function", + "com.google.common.base.Predicate", + "com.google.common.base.Supplier", + + //common collections + "org.apache.commons.collections.Closure", + "org.apache.commons.collections.Factory", + "org.apache.commons.collections.Predicate", + "org.apache.commons.collections.Transformer", + + //trove + "gnu.trove.TObjectFunction", + "gnu.trove.TObjectProcedure", + "gnu.trove.TObjectObjectProcedure", + }; + public static Collection suggestFunctionalInterfaces(final @NotNull PsiFunctionalExpression expression) { final PsiType qualifierType = expression instanceof PsiMethodReferenceExpression ? PsiMethodReferenceUtil.getQualifierType((PsiMethodReferenceExpression)expression) : null; @@ -113,13 +144,8 @@ public class FunctionalInterfaceSuggester { private static Collection suggestFunctionalInterfaces(final @NotNull T element, final NullableFunction acceptanceChecker) { final Project project = element.getProject(); - final PsiClass functionalInterfaceClass = JavaPsiFacade.getInstance(project).findClass(CommonClassNames.JAVA_LANG_FUNCTIONAL_INTERFACE, - GlobalSearchScope.allScope(project)); - if (functionalInterfaceClass == null) { - return Collections.emptyList(); - } final Set types = new HashSet(); - AnnotatedMembersSearch.search(functionalInterfaceClass, element.getResolveScope()).forEach(new Processor() { + final Processor consumer = new Processor() { @Override public boolean process(PsiMember member) { if (member instanceof PsiClass && !Java15APIUsageInspectionBase.isForbiddenApiUsage(member, PsiUtil.getLanguageLevel(element))) { @@ -130,7 +156,21 @@ public class FunctionalInterfaceSuggester { } return true; } - }); + }; + final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); + final GlobalSearchScope allScope = GlobalSearchScope.allScope(project); + final PsiClass functionalInterfaceClass = psiFacade.findClass(CommonClassNames.JAVA_LANG_FUNCTIONAL_INTERFACE, allScope); + if (functionalInterfaceClass != null) { + AnnotatedMembersSearch.search(functionalInterfaceClass, element.getResolveScope()).forEach(consumer); + } + + for (String functionalInterface : FUNCTIONAL_INTERFACES) { + final PsiClass aClass = psiFacade.findClass(functionalInterface, allScope); + if (aClass != null) { + consumer.process(aClass); + } + } + final ArrayList typesToSuggest = new ArrayList(types); Collections.sort(typesToSuggest, new Comparator() { @Override