update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / ide / util / SuperMethodWarningUtil.java
blobed577175a08c5ffc9e4e256131fa739bda57f67e
1 /*
2 * Copyright 2000-2009 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.intellij.ide.util;
18 import com.intellij.openapi.application.ApplicationManager;
19 import com.intellij.openapi.ui.DialogWrapper;
20 import com.intellij.psi.PsiClass;
21 import com.intellij.psi.PsiElement;
22 import com.intellij.psi.PsiMethod;
23 import com.intellij.psi.PsiModifier;
24 import com.intellij.psi.search.searches.DeepestSuperMethodsSearch;
25 import com.intellij.usageView.UsageViewUtil;
26 import com.intellij.util.ArrayUtil;
27 import org.jetbrains.annotations.NotNull;
29 import java.util.Collection;
30 import java.util.HashSet;
31 import java.util.Set;
33 public class SuperMethodWarningUtil {
34 private SuperMethodWarningUtil() {}
36 @NotNull
37 public static PsiMethod[] checkSuperMethods(final PsiMethod method, String actionString) {
38 return checkSuperMethods(method, actionString, null);
41 @NotNull
42 public static PsiMethod[] checkSuperMethods(final PsiMethod method, String actionString, Collection<PsiElement> ignore) {
43 PsiClass aClass = method.getContainingClass();
44 if (aClass == null) return new PsiMethod[]{method};
46 final Collection<PsiMethod> superMethods = DeepestSuperMethodsSearch.search(method).findAll();
47 if (ignore != null) {
48 superMethods.removeAll(ignore);
51 if (superMethods.isEmpty()) return new PsiMethod[]{method};
54 Set<String> superClasses = new HashSet<String>();
55 boolean superAbstract = false;
56 boolean parentInterface = false;
57 for (final PsiMethod superMethod : superMethods) {
58 final PsiClass containingClass = superMethod.getContainingClass();
59 superClasses.add(containingClass.getQualifiedName());
60 final boolean isInterface = containingClass.isInterface();
61 superAbstract |= isInterface || superMethod.hasModifierProperty(PsiModifier.ABSTRACT);
62 parentInterface |= isInterface;
65 SuperMethodWarningDialog dialog =
66 new SuperMethodWarningDialog(method.getProject(), UsageViewUtil.getDescriptiveName(method), actionString, superAbstract,
67 parentInterface, aClass.isInterface(), ArrayUtil.toStringArray(superClasses));
68 dialog.show();
70 if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
71 return superMethods.toArray(new PsiMethod[superMethods.size()]);
73 if (dialog.getExitCode() == SuperMethodWarningDialog.NO_EXIT_CODE) {
74 return new PsiMethod[]{method};
77 return PsiMethod.EMPTY_ARRAY;
81 public static PsiMethod checkSuperMethod(final PsiMethod method, String actionString) {
82 PsiClass aClass = method.getContainingClass();
83 if (aClass == null) return method;
85 PsiMethod superMethod = method.findDeepestSuperMethod();
86 if (superMethod == null) return method;
88 if (ApplicationManager.getApplication().isUnitTestMode()) return superMethod;
90 PsiClass containingClass = superMethod.getContainingClass();
92 SuperMethodWarningDialog dialog =
93 new SuperMethodWarningDialog(
94 method.getProject(),
95 UsageViewUtil.getDescriptiveName(method), actionString, containingClass.isInterface() || superMethod.hasModifierProperty(PsiModifier.ABSTRACT),
96 containingClass.isInterface(), aClass.isInterface(), containingClass.getQualifiedName()
98 dialog.show();
100 if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) return superMethod;
101 if (dialog.getExitCode() == SuperMethodWarningDialog.NO_EXIT_CODE) return method;
103 return null;