mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Move JavaOverridingMethodsSearcher to java-indexing-impl
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
package com.intellij.psi.impl.search;
|
||||
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.searches.SuperMethodsSearch;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.MethodSignatureBackedByPsiMethod;
|
||||
import com.intellij.psi.util.MethodSignatureUtil;
|
||||
import com.intellij.util.Processor;
|
||||
import com.intellij.util.QueryExecutor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author ven
|
||||
*/
|
||||
public class MethodSuperSearcher implements QueryExecutor<MethodSignatureBackedByPsiMethod, SuperMethodsSearch.SearchParameters> {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.search.MethodSuperSearcher");
|
||||
|
||||
@Override
|
||||
public boolean execute(@NotNull final SuperMethodsSearch.SearchParameters queryParameters, @NotNull final Processor<MethodSignatureBackedByPsiMethod> consumer) {
|
||||
final PsiClass parentClass = queryParameters.getPsiClass();
|
||||
final PsiMethod method = queryParameters.getMethod();
|
||||
HierarchicalMethodSignature signature = method.getHierarchicalMethodSignature();
|
||||
|
||||
final boolean checkBases = queryParameters.isCheckBases();
|
||||
final boolean allowStaticMethod = queryParameters.isAllowStaticMethod();
|
||||
final List<HierarchicalMethodSignature> supers = signature.getSuperSignatures();
|
||||
for (HierarchicalMethodSignature superSignature : supers) {
|
||||
if (MethodSignatureUtil.isSubsignature(superSignature, signature)) {
|
||||
if (!addSuperMethods(superSignature, method, parentClass, allowStaticMethod, checkBases, consumer)) return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean addSuperMethods(final HierarchicalMethodSignature signature,
|
||||
final PsiMethod method,
|
||||
final PsiClass parentClass,
|
||||
final boolean allowStaticMethod,
|
||||
final boolean checkBases,
|
||||
final Processor<MethodSignatureBackedByPsiMethod> consumer) {
|
||||
PsiMethod signatureMethod = signature.getMethod();
|
||||
PsiClass hisClass = signatureMethod.getContainingClass();
|
||||
if (parentClass == null || InheritanceUtil.isInheritorOrSelf(parentClass, hisClass, true)) {
|
||||
if (isAcceptable(signatureMethod, method, allowStaticMethod)) {
|
||||
if (parentClass != null && !parentClass.equals(hisClass) && !checkBases) {
|
||||
return true;
|
||||
}
|
||||
LOG.assertTrue(signatureMethod != method, method); // method != method.getsuper()
|
||||
return consumer.process(signature); //no need to check super classes
|
||||
}
|
||||
}
|
||||
for (HierarchicalMethodSignature superSignature : signature.getSuperSignatures()) {
|
||||
if (MethodSignatureUtil.isSubsignature(superSignature, signature)) {
|
||||
addSuperMethods(superSignature, method, parentClass, allowStaticMethod, checkBases, consumer);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean isAcceptable(final PsiMethod superMethod, final PsiMethod method, final boolean allowStaticMethod) {
|
||||
boolean hisStatic = superMethod.hasModifierProperty(PsiModifier.STATIC);
|
||||
return hisStatic == method.hasModifierProperty(PsiModifier.STATIC) &&
|
||||
(allowStaticMethod || !hisStatic) &&
|
||||
JavaPsiFacade.getInstance(method.getProject()).getResolveHelper().isAccessible(superMethod, method, null);
|
||||
}
|
||||
}
|
||||
+97
-94
@@ -1,94 +1,97 @@
|
||||
/*
|
||||
* 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.psi.search.searches;
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.psi.PsiAnonymousClass;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.intellij.psi.search.SearchScope;
|
||||
import com.intellij.util.EmptyQuery;
|
||||
import com.intellij.util.Query;
|
||||
|
||||
/**
|
||||
* @author max
|
||||
*/
|
||||
public class OverridingMethodsSearch extends ExtensibleQueryFactory<PsiMethod, OverridingMethodsSearch.SearchParameters> {
|
||||
public static final OverridingMethodsSearch INSTANCE = new OverridingMethodsSearch();
|
||||
|
||||
public static class SearchParameters {
|
||||
private final PsiMethod myMethod;
|
||||
private final SearchScope myScope;
|
||||
private final boolean myCheckDeep;
|
||||
|
||||
public SearchParameters(final PsiMethod aClass, SearchScope scope, final boolean checkDeep) {
|
||||
myMethod = aClass;
|
||||
myScope = scope;
|
||||
myCheckDeep = checkDeep;
|
||||
}
|
||||
|
||||
public PsiMethod getMethod() {
|
||||
return myMethod;
|
||||
}
|
||||
|
||||
public boolean isCheckDeep() {
|
||||
return myCheckDeep;
|
||||
}
|
||||
|
||||
public SearchScope getScope() {
|
||||
return myScope;
|
||||
}
|
||||
}
|
||||
|
||||
private OverridingMethodsSearch() {
|
||||
}
|
||||
|
||||
public static Query<PsiMethod> search(final PsiMethod method, SearchScope scope, final boolean checkDeep) {
|
||||
if (ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
|
||||
@Override
|
||||
public Boolean compute() {
|
||||
return cannotBeOverriden(method);
|
||||
}
|
||||
})) return EmptyQuery.getEmptyQuery(); // Optimization
|
||||
return INSTANCE.createUniqueResultsQuery(new SearchParameters(method, scope, checkDeep));
|
||||
}
|
||||
|
||||
private static boolean cannotBeOverriden(final PsiMethod method) {
|
||||
final PsiClass parentClass = method.getContainingClass();
|
||||
return parentClass == null
|
||||
|| method.isConstructor()
|
||||
|| method.hasModifierProperty(PsiModifier.STATIC)
|
||||
|| method.hasModifierProperty(PsiModifier.FINAL)
|
||||
|| method.hasModifierProperty(PsiModifier.PRIVATE)
|
||||
|| parentClass instanceof PsiAnonymousClass
|
||||
|| parentClass.hasModifierProperty(PsiModifier.FINAL);
|
||||
}
|
||||
|
||||
public static Query<PsiMethod> search(final PsiMethod method, final boolean checkDeep) {
|
||||
return search(method, ApplicationManager.getApplication().runReadAction(new Computable<SearchScope>() {
|
||||
@Override
|
||||
public SearchScope compute() {
|
||||
return method.getUseScope();
|
||||
}
|
||||
}), checkDeep);
|
||||
}
|
||||
|
||||
public static Query<PsiMethod> search(final PsiMethod method) {
|
||||
return search(method, true);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* 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.psi.search.searches;
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.extensions.ExtensionPointName;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.psi.PsiAnonymousClass;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.intellij.psi.search.SearchScope;
|
||||
import com.intellij.util.EmptyQuery;
|
||||
import com.intellij.util.Query;
|
||||
import com.intellij.util.QueryExecutor;
|
||||
|
||||
/**
|
||||
* @author max
|
||||
*/
|
||||
public class OverridingMethodsSearch extends ExtensibleQueryFactory<PsiMethod, OverridingMethodsSearch.SearchParameters> {
|
||||
public static ExtensionPointName<QueryExecutor> EP_NAME = ExtensionPointName.create("com.intellij.overridingMethodsSearch");
|
||||
public static final OverridingMethodsSearch INSTANCE = new OverridingMethodsSearch();
|
||||
|
||||
public static class SearchParameters {
|
||||
private final PsiMethod myMethod;
|
||||
private final SearchScope myScope;
|
||||
private final boolean myCheckDeep;
|
||||
|
||||
public SearchParameters(final PsiMethod aClass, SearchScope scope, final boolean checkDeep) {
|
||||
myMethod = aClass;
|
||||
myScope = scope;
|
||||
myCheckDeep = checkDeep;
|
||||
}
|
||||
|
||||
public PsiMethod getMethod() {
|
||||
return myMethod;
|
||||
}
|
||||
|
||||
public boolean isCheckDeep() {
|
||||
return myCheckDeep;
|
||||
}
|
||||
|
||||
public SearchScope getScope() {
|
||||
return myScope;
|
||||
}
|
||||
}
|
||||
|
||||
private OverridingMethodsSearch() {
|
||||
}
|
||||
|
||||
public static Query<PsiMethod> search(final PsiMethod method, SearchScope scope, final boolean checkDeep) {
|
||||
if (ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
|
||||
@Override
|
||||
public Boolean compute() {
|
||||
return cannotBeOverriden(method);
|
||||
}
|
||||
})) return EmptyQuery.getEmptyQuery(); // Optimization
|
||||
return INSTANCE.createUniqueResultsQuery(new SearchParameters(method, scope, checkDeep));
|
||||
}
|
||||
|
||||
private static boolean cannotBeOverriden(final PsiMethod method) {
|
||||
final PsiClass parentClass = method.getContainingClass();
|
||||
return parentClass == null
|
||||
|| method.isConstructor()
|
||||
|| method.hasModifierProperty(PsiModifier.STATIC)
|
||||
|| method.hasModifierProperty(PsiModifier.FINAL)
|
||||
|| method.hasModifierProperty(PsiModifier.PRIVATE)
|
||||
|| parentClass instanceof PsiAnonymousClass
|
||||
|| parentClass.hasModifierProperty(PsiModifier.FINAL);
|
||||
}
|
||||
|
||||
public static Query<PsiMethod> search(final PsiMethod method, final boolean checkDeep) {
|
||||
return search(method, ApplicationManager.getApplication().runReadAction(new Computable<SearchScope>() {
|
||||
@Override
|
||||
public SearchScope compute() {
|
||||
return method.getUseScope();
|
||||
}
|
||||
}), checkDeep);
|
||||
}
|
||||
|
||||
public static Query<PsiMethod> search(final PsiMethod method) {
|
||||
return search(method, true);
|
||||
}
|
||||
}
|
||||
|
||||
+78
-78
@@ -1,78 +1,78 @@
|
||||
package com.intellij.psi.impl.search;
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.SearchScope;
|
||||
import com.intellij.psi.search.searches.ClassInheritorsSearch;
|
||||
import com.intellij.psi.search.searches.OverridingMethodsSearch;
|
||||
import com.intellij.psi.util.MethodSignature;
|
||||
import com.intellij.psi.util.MethodSignatureUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.util.Processor;
|
||||
import com.intellij.util.QueryExecutor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author max
|
||||
*/
|
||||
public class JavaOverridingMethodsSearcher implements QueryExecutor<PsiMethod, OverridingMethodsSearch.SearchParameters> {
|
||||
@Override
|
||||
public boolean execute(@NotNull final OverridingMethodsSearch.SearchParameters p, @NotNull final Processor<PsiMethod> consumer) {
|
||||
final PsiMethod method = p.getMethod();
|
||||
final SearchScope scope = p.getScope();
|
||||
|
||||
final PsiClass parentClass = ApplicationManager.getApplication().runReadAction(new Computable<PsiClass>() {
|
||||
@Nullable
|
||||
@Override
|
||||
public PsiClass compute() {
|
||||
return method.getContainingClass();
|
||||
}
|
||||
});
|
||||
assert parentClass != null;
|
||||
Processor<PsiClass> inheritorsProcessor = new Processor<PsiClass>() {
|
||||
@Override
|
||||
public boolean process(final PsiClass inheritor) {
|
||||
PsiMethod found = ApplicationManager.getApplication().runReadAction(new Computable<PsiMethod>() {
|
||||
@Override
|
||||
@Nullable
|
||||
public PsiMethod compute() {
|
||||
return findOverridingMethod(inheritor, parentClass, method);
|
||||
}
|
||||
});
|
||||
return found == null || consumer.process(found) && p.isCheckDeep();
|
||||
}
|
||||
};
|
||||
|
||||
return ClassInheritorsSearch.search(parentClass, scope, true).forEach(inheritorsProcessor);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiMethod findOverridingMethod(PsiClass inheritor, @NotNull PsiClass parentClass, PsiMethod method) {
|
||||
PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(parentClass, inheritor, PsiSubstitutor.EMPTY);
|
||||
MethodSignature signature = method.getSignature(substitutor);
|
||||
PsiMethod found = MethodSignatureUtil.findMethodBySuperSignature(inheritor, signature, false);
|
||||
if (found != null && isAcceptable(found, method)) {
|
||||
return found;
|
||||
}
|
||||
|
||||
if (parentClass.isInterface() && !inheritor.isInterface()) { //check for sibling implementation
|
||||
final PsiClass superClass = inheritor.getSuperClass();
|
||||
if (superClass != null && !superClass.isInheritor(parentClass, true)) {
|
||||
PsiMethod derived = MethodSignatureUtil.findMethodInSuperClassBySignatureInDerived(inheritor, superClass, signature, true);
|
||||
if (derived != null && isAcceptable(derived, method)) {
|
||||
return derived;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean isAcceptable(final PsiMethod found, final PsiMethod method) {
|
||||
return !found.hasModifierProperty(PsiModifier.STATIC) &&
|
||||
(!method.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) ||
|
||||
JavaPsiFacade.getInstance(found.getProject())
|
||||
.arePackagesTheSame(method.getContainingClass(), found.getContainingClass()));
|
||||
}
|
||||
}
|
||||
package com.intellij.psi.impl.search;
|
||||
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.SearchScope;
|
||||
import com.intellij.psi.search.searches.ClassInheritorsSearch;
|
||||
import com.intellij.psi.search.searches.OverridingMethodsSearch;
|
||||
import com.intellij.psi.util.MethodSignature;
|
||||
import com.intellij.psi.util.MethodSignatureUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.util.Processor;
|
||||
import com.intellij.util.QueryExecutor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author max
|
||||
*/
|
||||
public class JavaOverridingMethodsSearcher implements QueryExecutor<PsiMethod, OverridingMethodsSearch.SearchParameters> {
|
||||
@Override
|
||||
public boolean execute(@NotNull final OverridingMethodsSearch.SearchParameters p, @NotNull final Processor<PsiMethod> consumer) {
|
||||
final PsiMethod method = p.getMethod();
|
||||
final SearchScope scope = p.getScope();
|
||||
|
||||
final PsiClass parentClass = ApplicationManager.getApplication().runReadAction(new Computable<PsiClass>() {
|
||||
@Nullable
|
||||
@Override
|
||||
public PsiClass compute() {
|
||||
return method.getContainingClass();
|
||||
}
|
||||
});
|
||||
assert parentClass != null;
|
||||
Processor<PsiClass> inheritorsProcessor = new Processor<PsiClass>() {
|
||||
@Override
|
||||
public boolean process(final PsiClass inheritor) {
|
||||
PsiMethod found = ApplicationManager.getApplication().runReadAction(new Computable<PsiMethod>() {
|
||||
@Override
|
||||
@Nullable
|
||||
public PsiMethod compute() {
|
||||
return findOverridingMethod(inheritor, parentClass, method);
|
||||
}
|
||||
});
|
||||
return found == null || consumer.process(found) && p.isCheckDeep();
|
||||
}
|
||||
};
|
||||
|
||||
return ClassInheritorsSearch.search(parentClass, scope, true).forEach(inheritorsProcessor);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiMethod findOverridingMethod(PsiClass inheritor, @NotNull PsiClass parentClass, PsiMethod method) {
|
||||
PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(parentClass, inheritor, PsiSubstitutor.EMPTY);
|
||||
MethodSignature signature = method.getSignature(substitutor);
|
||||
PsiMethod found = MethodSignatureUtil.findMethodBySuperSignature(inheritor, signature, false);
|
||||
if (found != null && isAcceptable(found, method)) {
|
||||
return found;
|
||||
}
|
||||
|
||||
if (parentClass.isInterface() && !inheritor.isInterface()) { //check for sibling implementation
|
||||
final PsiClass superClass = inheritor.getSuperClass();
|
||||
if (superClass != null && !superClass.isInheritor(parentClass, true)) {
|
||||
PsiMethod derived = MethodSignatureUtil.findMethodInSuperClassBySignatureInDerived(inheritor, superClass, signature, true);
|
||||
if (derived != null && isAcceptable(derived, method)) {
|
||||
return derived;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean isAcceptable(final PsiMethod found, final PsiMethod method) {
|
||||
return !found.hasModifierProperty(PsiModifier.STATIC) &&
|
||||
(!method.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) ||
|
||||
JavaPsiFacade.getInstance(found.getProject())
|
||||
.arePackagesTheSame(method.getContainingClass(), found.getContainingClass()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user