search for method references during constructor search (IDEA-92804)

This commit is contained in:
anna
2012-10-15 14:16:19 +02:00
parent 30ac225758
commit 1c0933b830
3 changed files with 78 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package com.intellij.psi.impl.search;
import com.intellij.openapi.util.TextRange;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.impl.light.LightMemberReference;
import com.intellij.psi.search.PsiSearchScopeUtil;
@@ -8,6 +9,7 @@ import com.intellij.psi.search.SearchRequestCollector;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.searches.ClassInheritorsSearch;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.PairProcessor;
import com.intellij.util.Processor;
@@ -72,6 +74,24 @@ public class ConstructorReferencesSearchHelper {
};
ReferencesSearch.searchOptimized(aClass, searchScope, ignoreAccessScope, collector, true, processor1);
if (PsiUtil.getLanguageLevel(aClass).isAtLeast(LanguageLevel.JDK_1_8)) {
ReferencesSearch.search(aClass).forEach(new Processor<PsiReference>() {
@Override
public boolean process(PsiReference reference) {
final PsiElement element = reference.getElement();
if (element != null) {
final PsiElement parent = element.getParent();
if (parent instanceof PsiMethodReferenceExpression &&
((PsiMethodReferenceExpression)parent).getReferenceNameElement() instanceof PsiKeyword) {
if (((PsiMethodReferenceExpression)parent).isReferenceTo(constructor)) {
processor.process(reference);
}
}
}
return true;
}
});
}
final boolean constructorCanBeCalledImplicitly = constructor.getParameterList().getParametersCount() == 0;
// search usages like "this(..)"

View File

@@ -0,0 +1,11 @@
class ConstructorUsages {
ConstructorUsages() {
}
void foo() {
BlahBlah<ConstructorUsages> blahBlah = ConstructorUsages::new;
}
}
interface BlahBlah<T> {
T foo();
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.lambda;
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiReference;
import com.intellij.psi.search.searches.MethodReferencesSearch;
import org.jetbrains.annotations.NonNls;
import java.util.Collection;
/**
* User: anna
* Date: 10/15/12
*/
public class MethodReferencesFindUsagesTest extends LightDaemonAnalyzerTestCase {
@NonNls static final String BASE_PATH = "/codeInsight/daemonCodeAnalyzer/lambda/methodRef/findUsages/";
public void testConstructorUsages() throws Exception {
final String testName = getTestName(false);
configureByFile(BASE_PATH + testName + ".java");
final PsiClass aClass = getJavaFacade().findClass(testName);
assertNotNull(aClass);
final PsiMethod[] constructors = aClass.getConstructors();
assertEquals(constructors.length, 1);
Collection<PsiReference> references = MethodReferencesSearch.search(constructors[0]).findAll();
assertEquals(1, references.size());
}
}