From 58761c671ce0be8f3f51a71e827605d472d890f6 Mon Sep 17 00:00:00 2001 From: Evgeny Pasynkov Date: Wed, 4 Jul 2012 13:17:12 +0200 Subject: [PATCH] Move JavaOverridingMethodsSearcher to java-indexing-impl --- .../psi/impl/search/MethodSuperSearcher.java | 71 ------- .../searches/OverridingMethodsSearch.java | 191 +++++++++--------- .../search/JavaOverridingMethodsSearcher.java | 156 +++++++------- 3 files changed, 175 insertions(+), 243 deletions(-) delete mode 100644 java/java-impl/src/com/intellij/psi/impl/search/MethodSuperSearcher.java rename java/{java-impl => java-indexing-impl}/src/com/intellij/psi/impl/search/JavaOverridingMethodsSearcher.java (97%) diff --git a/java/java-impl/src/com/intellij/psi/impl/search/MethodSuperSearcher.java b/java/java-impl/src/com/intellij/psi/impl/search/MethodSuperSearcher.java deleted file mode 100644 index d65e80e7f997..000000000000 --- a/java/java-impl/src/com/intellij/psi/impl/search/MethodSuperSearcher.java +++ /dev/null @@ -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 { - 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 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 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 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); - } -} diff --git a/java/java-indexing-api/src/com/intellij/psi/search/searches/OverridingMethodsSearch.java b/java/java-indexing-api/src/com/intellij/psi/search/searches/OverridingMethodsSearch.java index ef1fcd588ffd..e0b97e716a2b 100644 --- a/java/java-indexing-api/src/com/intellij/psi/search/searches/OverridingMethodsSearch.java +++ b/java/java-indexing-api/src/com/intellij/psi/search/searches/OverridingMethodsSearch.java @@ -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 { - 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 search(final PsiMethod method, SearchScope scope, final boolean checkDeep) { - if (ApplicationManager.getApplication().runReadAction(new Computable() { - @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 search(final PsiMethod method, final boolean checkDeep) { - return search(method, ApplicationManager.getApplication().runReadAction(new Computable() { - @Override - public SearchScope compute() { - return method.getUseScope(); - } - }), checkDeep); - } - - public static Query 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 { + public static ExtensionPointName 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 search(final PsiMethod method, SearchScope scope, final boolean checkDeep) { + if (ApplicationManager.getApplication().runReadAction(new Computable() { + @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 search(final PsiMethod method, final boolean checkDeep) { + return search(method, ApplicationManager.getApplication().runReadAction(new Computable() { + @Override + public SearchScope compute() { + return method.getUseScope(); + } + }), checkDeep); + } + + public static Query search(final PsiMethod method) { + return search(method, true); + } +} diff --git a/java/java-impl/src/com/intellij/psi/impl/search/JavaOverridingMethodsSearcher.java b/java/java-indexing-impl/src/com/intellij/psi/impl/search/JavaOverridingMethodsSearcher.java similarity index 97% rename from java/java-impl/src/com/intellij/psi/impl/search/JavaOverridingMethodsSearcher.java rename to java/java-indexing-impl/src/com/intellij/psi/impl/search/JavaOverridingMethodsSearcher.java index b9c5dab62232..a4155ab975bb 100644 --- a/java/java-impl/src/com/intellij/psi/impl/search/JavaOverridingMethodsSearcher.java +++ b/java/java-indexing-impl/src/com/intellij/psi/impl/search/JavaOverridingMethodsSearcher.java @@ -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 { - @Override - public boolean execute(@NotNull final OverridingMethodsSearch.SearchParameters p, @NotNull final Processor consumer) { - final PsiMethod method = p.getMethod(); - final SearchScope scope = p.getScope(); - - final PsiClass parentClass = ApplicationManager.getApplication().runReadAction(new Computable() { - @Nullable - @Override - public PsiClass compute() { - return method.getContainingClass(); - } - }); - assert parentClass != null; - Processor inheritorsProcessor = new Processor() { - @Override - public boolean process(final PsiClass inheritor) { - PsiMethod found = ApplicationManager.getApplication().runReadAction(new Computable() { - @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 { + @Override + public boolean execute(@NotNull final OverridingMethodsSearch.SearchParameters p, @NotNull final Processor consumer) { + final PsiMethod method = p.getMethod(); + final SearchScope scope = p.getScope(); + + final PsiClass parentClass = ApplicationManager.getApplication().runReadAction(new Computable() { + @Nullable + @Override + public PsiClass compute() { + return method.getContainingClass(); + } + }); + assert parentClass != null; + Processor inheritorsProcessor = new Processor() { + @Override + public boolean process(final PsiClass inheritor) { + PsiMethod found = ApplicationManager.getApplication().runReadAction(new Computable() { + @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())); + } +}