diff --git a/java/java-indexing-impl/src/com/intellij/psi/impl/search/JavaFunctionalExpressionSearcher.java b/java/java-indexing-impl/src/com/intellij/psi/impl/search/JavaFunctionalExpressionSearcher.java index b2e823b8c98b..26a26cf6dcb3 100644 --- a/java/java-indexing-impl/src/com/intellij/psi/impl/search/JavaFunctionalExpressionSearcher.java +++ b/java/java-indexing-impl/src/com/intellij/psi/impl/search/JavaFunctionalExpressionSearcher.java @@ -37,7 +37,6 @@ import com.intellij.psi.util.*; import com.intellij.util.CommonProcessors; import com.intellij.util.Function; import com.intellij.util.Processor; -import com.intellij.util.containers.ContainerUtil; import com.intellij.util.containers.HashSet; import com.intellij.util.indexing.FileBasedIndex; import org.jetbrains.annotations.NotNull; @@ -46,6 +45,8 @@ import java.util.Collection; import java.util.LinkedHashSet; import java.util.Set; +import static com.intellij.util.containers.ContainerUtil.*; + public class JavaFunctionalExpressionSearcher extends QueryExecutorBase { private static final Logger LOG = Logger.getInstance("#" + JavaFunctionalExpressionSearcher.class.getName()); @@ -85,7 +86,7 @@ public class JavaFunctionalExpressionSearcher extends QueryExecutorBase methodCandidates = getCandidateMethodsWithSuitableParams(aClass, project, useScope); + final Collection methodCandidates = getCandidateMethodsWithSuitableParams(aClass, project, useScope, candidateFiles); final LinkedHashSet filesToProcess = new LinkedHashSet(); final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance(); @@ -139,7 +140,7 @@ public class JavaFunctionalExpressionSearcher extends QueryExecutorBase consumer, Set filesToProcess, final int expectedFunExprParamsCount) { LOG.info("#usage files: " + filesToProcess.size()); - ContainerUtil.process(filesToProcess, new ReadActionProcessor() { + process(filesToProcess, new ReadActionProcessor() { @Override public boolean processInReadAction(VirtualFile file) { //resolve functional expressions to ensure that functional expression type is appropriate @@ -150,26 +151,39 @@ public class JavaFunctionalExpressionSearcher extends QueryExecutorBase getCandidateMethodsWithSuitableParams(final PsiClass aClass, final Project project, - final GlobalSearchScope useScope) { + final GlobalSearchScope useScope, + final Set candidateFiles) { return ApplicationManager.getApplication().runReadAction(new Computable>() { @Override public Collection compute() { - final String functionalInterfaceName = aClass.getName(); - JavaMethodParameterTypesIndex parameterTypesIndex = JavaMethodParameterTypesIndex.getInstance(); - - LinkedHashSet methods = new LinkedHashSet(parameterTypesIndex.get(functionalInterfaceName, project, useScope)); - - methods.addAll(parameterTypesIndex.get(JavaMethodElementType.TYPE_PARAMETER_PSEUDO_NAME, project, - GlobalSearchScope.allScope(project))); + GlobalSearchScope visibleFromCandidates = combineResolveScopes(project, candidateFiles); + JavaMethodParameterTypesIndex index = JavaMethodParameterTypesIndex.getInstance(); + + LinkedHashSet methods = newLinkedHashSet(); + methods.addAll(index.get(aClass.getName(), project, useScope.intersectWith(visibleFromCandidates))); + methods.addAll(index.get(JavaMethodElementType.TYPE_PARAMETER_PSEUDO_NAME, project, visibleFromCandidates)); LOG.info("#methods: " + methods.size()); return methods; } }); } + @NotNull + private static GlobalSearchScope combineResolveScopes(Project project, Set candidateFiles) { + final PsiManager psiManager = PsiManager.getInstance(project); + LinkedHashSet resolveScopes = newLinkedHashSet(mapNotNull(candidateFiles, new Function() { + @Override + public GlobalSearchScope fun(VirtualFile file) { + PsiFile psiFile = psiManager.findFile(file); + return psiFile == null ? null : psiFile.getResolveScope(); + } + })); + return GlobalSearchScope.union(resolveScopes.toArray(new GlobalSearchScope[resolveScopes.size()])); + } + @NotNull private static Set getFilesWithFunctionalExpressionsScope(Project project, GlobalSearchScope useScope) { - final Set files = ContainerUtil.newLinkedHashSet(); + final Set files = newLinkedHashSet(); final PsiSearchHelperImpl helper = (PsiSearchHelperImpl)PsiSearchHelper.SERVICE.getInstance(project); final CommonProcessors.CollectProcessor processor = new CommonProcessors.CollectProcessor(files); helper.processFilesWithText(useScope, UsageSearchContext.IN_CODE, true, "::", processor); @@ -185,7 +199,7 @@ public class JavaFunctionalExpressionSearcher extends QueryExecutorBase files = new HashSet(); - ContainerUtil.addAllNotNull(files, ContainerUtil.map(((LocalSearchScope)useScope).getScope(), new Function() { + addAllNotNull(files, map(((LocalSearchScope)useScope).getScope(), new Function() { @Override public VirtualFile fun(PsiElement element) { return PsiUtilCore.getVirtualFile(element); @@ -225,7 +239,7 @@ public class JavaFunctionalExpressionSearcher extends QueryExecutorBase