move searchers to indexing* module

This commit is contained in:
Evgeny Pasynkov
2012-07-30 12:15:49 +02:00
parent 5ea14cb767
commit 7cd686956a
5 changed files with 7 additions and 5 deletions
@@ -1,49 +0,0 @@
/*
* Copyright 2000-2009 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.
*/
/*
* @author max
*/
package com.intellij.psi.impl.java.stubs.index;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.impl.search.JavaSourceFilterScope;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.stubs.StringStubIndexExtension;
import com.intellij.psi.stubs.StubIndexKey;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
public class JavaAnnotationIndex extends StringStubIndexExtension<PsiAnnotation> {
private static final JavaAnnotationIndex ourInstance = new JavaAnnotationIndex();
public static JavaAnnotationIndex getInstance() {
return ourInstance;
}
@NotNull
@Override
public StubIndexKey<String, PsiAnnotation> getKey() {
return JavaStubIndexKeys.ANNOTATIONS;
}
@Override
public Collection<PsiAnnotation> get(final String s, final Project project, @NotNull final GlobalSearchScope scope) {
return super.get(s, project, new JavaSourceFilterScope(scope));
}
}
@@ -1,132 +0,0 @@
/*
* Copyright 2000-2009 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.
*/
/*
* @author max
*/
package com.intellij.psi.impl.search;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.util.Computable;
import com.intellij.psi.*;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.PsiShortNamesCache;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.searches.AllClassesSearch;
import com.intellij.util.Processor;
import com.intellij.util.QueryExecutor;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class AllClassesSearchExecutor implements QueryExecutor<PsiClass, AllClassesSearch.SearchParameters> {
@Override
public boolean execute(@NotNull final AllClassesSearch.SearchParameters queryParameters, @NotNull final Processor<PsiClass> consumer) {
SearchScope scope = queryParameters.getScope();
if (scope instanceof GlobalSearchScope) {
return processAllClassesInGlobalScope((GlobalSearchScope)scope, consumer, queryParameters);
}
PsiElement[] scopeRoots = ((LocalSearchScope)scope).getScope();
for (final PsiElement scopeRoot : scopeRoots) {
if (!processScopeRootForAllClasses(scopeRoot, consumer)) return false;
}
return true;
}
private static boolean processAllClassesInGlobalScope(final GlobalSearchScope scope, final Processor<PsiClass> processor, AllClassesSearch.SearchParameters parameters) {
final PsiShortNamesCache cache = PsiShortNamesCache.getInstance(parameters.getProject());
final String[] names = ApplicationManager.getApplication().runReadAction(new Computable<String[]>() {
@Override
public String[] compute() {
return cache.getAllClassNames();
}
});
final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
if (indicator != null) {
indicator.checkCanceled();
}
List<String> sorted = new ArrayList<String>(names.length);
for (int i = 0; i < names.length; i++) {
String name = names[i];
if (parameters.nameMatches(name)) {
sorted.add(name);
}
if (indicator != null && i % 512 == 0) {
indicator.checkCanceled();
}
}
if (indicator != null) {
indicator.checkCanceled();
}
Collections.sort(sorted, new Comparator<String>() {
@Override
public int compare(final String o1, final String o2) {
return o1.compareToIgnoreCase(o2);
}
});
for (final String name : sorted) {
ProgressManager.checkCanceled();
final PsiClass[] classes = ApplicationManager.getApplication().runReadAction(new Computable<PsiClass[]>() {
@Override
public PsiClass[] compute() {
return cache.getClassesByName(name, scope);
}
});
for (PsiClass psiClass : classes) {
ProgressManager.checkCanceled();
if (!processor.process(psiClass)) {
return false;
}
}
}
return true;
}
private static boolean processScopeRootForAllClasses(PsiElement scopeRoot, final Processor<PsiClass> processor) {
if (scopeRoot == null) return true;
final boolean[] stopped = new boolean[]{false};
scopeRoot.accept(new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (!stopped[0]) {
super.visitElement(element);
}
}
@Override public void visitClass(PsiClass aClass) {
stopped[0] = !processor.process(aClass);
super.visitClass(aClass);
}
});
return !stopped[0];
}
}
@@ -1,134 +0,0 @@
package com.intellij.psi.impl.search;
import com.intellij.openapi.application.AccessToken;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiManagerImpl;
import com.intellij.psi.impl.java.stubs.index.JavaAnnotationIndex;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.searches.AnnotatedElementsSearch;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.Processor;
import com.intellij.util.QueryExecutor;
import com.intellij.util.indexing.FileBasedIndex;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author max
*/
public class AnnotatedElementsSearcher implements QueryExecutor<PsiModifierListOwner, AnnotatedElementsSearch.Parameters> {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.search.AnnotatedMembersSearcher");
@Override
public boolean execute(@NotNull final AnnotatedElementsSearch.Parameters p, @NotNull final Processor<PsiModifierListOwner> consumer) {
final PsiClass annClass = p.getAnnotationClass();
assert annClass.isAnnotationType() : "Annotation type should be passed to annotated members search";
final String annotationFQN = annClass.getQualifiedName();
assert annotationFQN != null;
final PsiManagerImpl psiManager = (PsiManagerImpl)annClass.getManager();
final SearchScope useScope = p.getScope();
Class<? extends PsiModifierListOwner>[] types = p.getTypes();
for (PsiElement elt : getAnnotationCandidates(annClass, useScope)) {
if (notAnnotation(elt)) continue;
final PsiAnnotation ann = (PsiAnnotation)elt;
final PsiJavaCodeReferenceElement ref = ApplicationManager.getApplication().runReadAction(new Computable<PsiJavaCodeReferenceElement>() {
@Override
public PsiJavaCodeReferenceElement compute() {
return ann.getNameReferenceElement();
}
});
if (ref == null) continue;
PsiElement parent = ann.getParent();
if (!(parent instanceof PsiModifierList)) continue; // Can be a PsiNameValuePair, if annotation is used to annotate annotation parameters
final PsiElement owner = parent.getParent();
if (!isInstanceof(owner, types)) continue;
final PsiModifierListOwner candidate = (PsiModifierListOwner)owner;
if (!ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
@Override
public Boolean compute() {
if (!candidate.isValid()) {
return false;
}
if (!psiManager.areElementsEquivalent(ref.resolve(), annClass)) {
return false;
}
if (useScope instanceof GlobalSearchScope &&
!((GlobalSearchScope)useScope).contains(candidate.getContainingFile().getVirtualFile())) {
return false;
}
return true;
}
})) {
continue;
}
if (!consumer.process(candidate)) {
return false;
}
}
return true;
}
private static Collection<? extends PsiElement> getAnnotationCandidates(final PsiClass annClass, SearchScope useScope) {
AccessToken token = ReadAction.start();
try {
if (useScope instanceof GlobalSearchScope) {
return JavaAnnotationIndex.getInstance().get(annClass.getName(), annClass.getProject(), (GlobalSearchScope)useScope);
}
final ArrayList<PsiAnnotation> result = new ArrayList<PsiAnnotation>();
for (PsiElement element : ((LocalSearchScope)useScope).getScope()) {
element.accept(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof PsiAnnotation) {
result.add((PsiAnnotation)element);
}
}
});
}
return result;
}
finally {
token.finish();
}
}
public static boolean isInstanceof(PsiElement owner, Class<? extends PsiModifierListOwner>[] types) {
for (Class<? extends PsiModifierListOwner> type : types) {
if(type.isInstance(owner)) return true;
}
return false;
}
private static boolean notAnnotation(final PsiElement found) {
if (found instanceof PsiAnnotation) return false;
VirtualFile faultyContainer = PsiUtil.getVirtualFile(found);
LOG.error("Non annotation in annotations list: " + faultyContainer+"; element:"+found);
if (faultyContainer != null && faultyContainer.isValid()) {
FileBasedIndex.getInstance().requestReindex(faultyContainer);
}
return true;
}
}
@@ -1,84 +0,0 @@
/*
* Copyright 2000-2009 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.impl.search;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.*;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.searches.AllOverridingMethodsSearch;
import com.intellij.psi.search.searches.ClassInheritorsSearch;
import com.intellij.psi.util.MethodSignature;
import com.intellij.psi.util.MethodSignatureUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.Processor;
import com.intellij.util.QueryExecutor;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
/**
* @author ven
*/
public class JavaAllOverridingMethodsSearcher implements QueryExecutor<Pair<PsiMethod, PsiMethod>, AllOverridingMethodsSearch.SearchParameters> {
@Override
public boolean execute(@NotNull final AllOverridingMethodsSearch.SearchParameters p, @NotNull final Processor<Pair<PsiMethod, PsiMethod>> consumer) {
final PsiClass psiClass = p.getPsiClass();
PsiMethod[] methodsArray = psiClass.getMethods();
final List<PsiMethod> methods = new ArrayList<PsiMethod>(methodsArray.length);
for (PsiMethod method : methodsArray) {
if (PsiUtil.canBeOverriden(method)) methods.add(method);
}
final SearchScope scope = p.getScope();
Processor<PsiClass> inheritorsProcessor = new Processor<PsiClass>() {
@Override
public boolean process(PsiClass inheritor) {
//could be null if not java inheritor, TODO only JavaClassInheritors are needed
PsiSubstitutor substitutor = TypeConversionUtil.getClassSubstitutor(psiClass, inheritor, PsiSubstitutor.EMPTY);
if (substitutor == null) return true;
for (PsiMethod method : methods) {
if (method.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) &&
!JavaPsiFacade.getInstance(inheritor.getProject()).arePackagesTheSame(psiClass, inheritor)) continue;
MethodSignature signature = method.getSignature(substitutor);
PsiMethod inInheritor = MethodSignatureUtil.findMethodBySuperSignature(inheritor, signature, false);
if (inInheritor == null || inInheritor.hasModifierProperty(PsiModifier.STATIC)) {
if (psiClass.isInterface() && !inheritor.isInterface()) { //check for sibling implementation
final PsiClass superClass = inheritor.getSuperClass();
if (superClass != null && !superClass.isInheritor(psiClass, true)) {
inInheritor = MethodSignatureUtil.findMethodInSuperClassBySignatureInDerived(inheritor, superClass, signature, true);
if (inInheritor != null && !inInheritor.hasModifierProperty(PsiModifier.STATIC)) {
if (!consumer.process(new Pair<PsiMethod, PsiMethod>(method, inInheritor))) return false;
}
}
}
continue;
}
if (!consumer.process(new Pair<PsiMethod, PsiMethod>(method, inInheritor))) return false;
}
return true;
}
};
return ClassInheritorsSearch.search(psiClass, scope, true).forEach(inheritorsProcessor);
}
}
@@ -1,41 +0,0 @@
package com.intellij.psi.impl.search;
import com.intellij.psi.PsiMethod;
import com.intellij.util.Processor;
import com.intellij.util.QueryExecutor;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
import java.util.Set;
/**
* @author peter
*/
public class MethodDeepestSuperSearcher implements QueryExecutor<PsiMethod, PsiMethod> {
@Override
public boolean execute(@NotNull PsiMethod method, @NotNull Processor<PsiMethod> consumer) {
final Set<PsiMethod> methods = new THashSet<PsiMethod>();
methods.add(method);
return findDeepestSuperOrSelfSignature(method, methods, null, consumer);
}
private static boolean findDeepestSuperOrSelfSignature(PsiMethod method,
Set<PsiMethod> set,
Set<PsiMethod> guard,
Processor<PsiMethod> processor) {
if (guard != null && !guard.add(method)) return true;
PsiMethod[] supers = method.findSuperMethods();
if (supers.length == 0 && set.add(method) && !processor.process(method)) {
return false;
}
for (PsiMethod superMethod : supers) {
if (guard == null) {
guard = new THashSet<PsiMethod>();
guard.add(method);
}
if (!findDeepestSuperOrSelfSignature(superMethod, set, guard, processor)) return false;
}
return true;
}
}