2 * Copyright 2000-2014 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.jetbrains.python.psi.search;
18 import com.intellij.psi.PsiElement;
19 import com.intellij.util.Processor;
20 import com.intellij.util.QueryExecutor;
21 import com.jetbrains.python.psi.*;
22 import com.jetbrains.python.psi.types.PyClassLikeType;
23 import com.jetbrains.python.psi.types.PyClassLikeTypeUtil;
24 import com.jetbrains.python.psi.types.TypeEvalContext;
25 import org.jetbrains.annotations.NotNull;
27 import java.util.HashSet;
33 public class PySuperMethodsSearchExecutor implements QueryExecutor<PsiElement, PySuperMethodsSearch.SearchParameters> {
35 public boolean execute(@NotNull final PySuperMethodsSearch.SearchParameters queryParameters,
36 @NotNull final Processor<PsiElement> consumer) {
37 final PyFunction func = queryParameters.getDerivedMethod();
38 final String name = func.getName();
39 final PyClass containingClass = func.getContainingClass();
40 final Set<PyClass> foundMethodContainingClasses = new HashSet<PyClass>();
41 final TypeEvalContext context = queryParameters.getContext();
42 if (name != null && containingClass != null) {
43 for (PyClass superClass : containingClass.getAncestorClasses(context)) {
44 if (!queryParameters.isDeepSearch()) {
45 boolean isAlreadyFound = false;
46 for (PyClass alreadyFound : foundMethodContainingClasses) {
47 if (alreadyFound.isSubclass(superClass)) {
48 isAlreadyFound = true;
55 PyFunction superMethod = superClass.findMethodByName(name, false);
56 if (superMethod != null) {
57 final Property property = func.getProperty();
58 final Property superProperty = superMethod.getProperty();
59 if (property != null && superProperty != null) {
60 final AccessDirection direction = PyUtil.getPropertyAccessDirection(func);
61 final PyCallable callable = superProperty.getByDirection(direction).valueOrNull();
62 superMethod = (callable instanceof PyFunction) ? (PyFunction)callable : null;
67 if (superMethod == null && context != null) {
68 // If super method still not found and we have context, we may use it to find method
69 final PyClassLikeType classLikeType = PyUtil.as(context.getType(superClass), PyClassLikeType.class);
70 if (classLikeType != null) {
71 for (PyFunction function : PyClassLikeTypeUtil.getMembersOfType(classLikeType, PyFunction.class, context)) {
72 final String elemName = function.getName();
73 if (elemName != null && elemName.equals(func.getName())) {
74 consumer.process(function);
79 if (superMethod != null) {
80 foundMethodContainingClasses.add(superClass);
81 if (!consumer.process(superMethod)) {