diff --git a/java/java-psi-impl/src/com/intellij/psi/scope/processor/MethodResolveProcessor.java b/java/java-psi-impl/src/com/intellij/psi/scope/processor/MethodResolveProcessor.java new file mode 100644 index 000000000000..463e2a12b04a --- /dev/null +++ b/java/java-psi-impl/src/com/intellij/psi/scope/processor/MethodResolveProcessor.java @@ -0,0 +1,96 @@ +/* + * 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.scope.processor; + +import com.intellij.openapi.util.Key; +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.ResolveState; +import com.intellij.psi.scope.ElementClassHint; +import com.intellij.psi.scope.NameHint; +import com.intellij.psi.scope.PsiScopeProcessor; +import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Dmitry Avdeev + */ +public class MethodResolveProcessor implements PsiScopeProcessor, ElementClassHint, NameHint { + + private final String myNameHint; + private final List myMethods = new ArrayList(); + + public MethodResolveProcessor() { + myNameHint = null; + } + + public MethodResolveProcessor(final String name) { + myNameHint = name; + } + + public PsiMethod[] getMethods() { + return myMethods.toArray(new PsiMethod[myMethods.size()]); + } + + public boolean execute(@NotNull PsiElement element, ResolveState state) { + if (element instanceof PsiMethod) { + ContainerUtil.addIfNotNull(myMethods, (PsiMethod)element); + } + return true; + } + + public T getHint(@NotNull Key hintKey) { + if (hintKey == ElementClassHint.KEY) { + return (T)this; + } + if (hintKey == NameHint.KEY && myNameHint != null) { + return (T)this; + } + return null; + } + + public void handleEvent(Event event, Object associated) { + } + + public boolean shouldProcess(DeclarationKind kind) { + return kind == DeclarationKind.METHOD; + } + + public static PsiMethod[] findMethod(PsiClass psiClass, String methodName) { + MethodResolveProcessor processor = new MethodResolveProcessor(methodName); + psiClass.processDeclarations(processor, ResolveState.initial(), null, psiClass); + return processor.getMethods(); + } + + public static PsiMethod[] getAllMethods(PsiClass psiClass) { + MethodResolveProcessor processor = new MethodResolveProcessor(); + psiClass.processDeclarations(processor, ResolveState.initial(), null, psiClass); + return processor.getMethods(); + } + + + @Nullable + @Override + public String getName(ResolveState state) { + return myNameHint; + } +}