update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / turnRefsToSuper / TurnRefsToSuperProcessor.java
blob0cd864054e485fd25a7d2f8381ec91d9c0c356ee
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.refactoring.turnRefsToSuper;
18 import com.intellij.openapi.application.ApplicationManager;
19 import com.intellij.openapi.diagnostic.Logger;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.ui.Messages;
22 import com.intellij.openapi.util.Ref;
23 import com.intellij.pom.java.LanguageLevel;
24 import com.intellij.psi.*;
25 import com.intellij.psi.search.GlobalSearchScope;
26 import com.intellij.psi.search.searches.ReferencesSearch;
27 import com.intellij.psi.util.InheritanceUtil;
28 import com.intellij.psi.util.PsiUtil;
29 import com.intellij.refactoring.RefactoringBundle;
30 import com.intellij.usageView.UsageInfo;
31 import com.intellij.usageView.UsageViewDescriptor;
32 import com.intellij.usageView.UsageViewUtil;
33 import com.intellij.util.IncorrectOperationException;
34 import org.jetbrains.annotations.NotNull;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.Collections;
40 public class TurnRefsToSuperProcessor extends TurnRefsToSuperProcessorBase {
41 private static final Logger LOG = Logger.getInstance("#com.intellij.refactoring.turnRefsToSuper.TurnRefsToSuperProcessor");
43 private PsiClass mySuper;
44 public TurnRefsToSuperProcessor(Project project,
45 @NotNull PsiClass aClass,
46 @NotNull PsiClass aSuper,
47 boolean replaceInstanceOf) {
48 super(project, replaceInstanceOf, aSuper.getName());
49 myClass = aClass;
50 mySuper = aSuper;
53 protected String getCommandName() {
54 return RefactoringBundle.message("turn.refs.to.super.command",
55 UsageViewUtil.getDescriptiveName(myClass), UsageViewUtil.getDescriptiveName(mySuper));
58 protected UsageViewDescriptor createUsageViewDescriptor(UsageInfo[] usages) {
59 return new RefsToSuperViewDescriptor(myClass, mySuper);
62 private void setClasses(@NotNull final PsiClass aClass, @NotNull final PsiClass aSuper) {
63 myClass = aClass;
64 mySuper = aSuper;
67 @NotNull
68 protected UsageInfo[] findUsages() {
69 final PsiReference[] refs = ReferencesSearch.search(myClass, GlobalSearchScope.projectScope(myProject), false).toArray(new PsiReference[0]);
71 final ArrayList<UsageInfo> result = detectTurnToSuperRefs(refs, new ArrayList<UsageInfo>());
73 final UsageInfo[] usageInfos = result.toArray(new UsageInfo[result.size()]);
74 return UsageViewUtil.removeDuplicatedUsages(usageInfos);
77 protected void refreshElements(final PsiElement[] elements) {
78 LOG.assertTrue(elements.length == 2 && elements[0] instanceof PsiClass && elements[1] instanceof PsiClass);
79 setClasses ((PsiClass) elements[0], (PsiClass) elements[1]);
82 protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
83 if (!ApplicationManager.getApplication().isUnitTestMode() && refUsages.get().length == 0) {
84 String message = RefactoringBundle.message("no.usages.can.be.replaced", myClass.getQualifiedName(), mySuper.getQualifiedName());
85 Messages.showInfoMessage(myProject, message, TurnRefsToSuperHandler.REFACTORING_NAME);
86 return false;
89 return super.preprocessUsages(refUsages);
92 protected boolean canTurnToSuper(final PsiElement refElement) {
93 return super.canTurnToSuper(refElement) &&
94 JavaPsiFacade.getInstance(myProject).getResolveHelper().isAccessible(mySuper, refElement, null);
97 protected void performRefactoring(UsageInfo[] usages) {
98 try {
99 final PsiClass aSuper = mySuper;
100 processTurnToSuperRefs(usages, aSuper);
102 } catch (IncorrectOperationException e) {
103 LOG.error(e);
106 performVariablesRenaming();
109 protected boolean isInSuper(PsiElement member) {
110 if (!(member instanceof PsiMember)) return false;
111 final PsiManager manager = member.getManager();
112 if (InheritanceUtil.isInheritorOrSelf(mySuper, ((PsiMember)member).getContainingClass(), true)) return true;
114 if (member instanceof PsiField) {
115 final PsiClass containingClass = ((PsiField) member).getContainingClass();
116 LanguageLevel languageLevel = PsiUtil.getLanguageLevel(member);
117 if (manager.areElementsEquivalent(containingClass, JavaPsiFacade.getInstance(manager.getProject()).getElementFactory().getArrayClass(languageLevel))) {
118 return true;
120 } else if (member instanceof PsiMethod) {
121 return mySuper.findMethodBySignature((PsiMethod) member, true) != null;
124 return false;
127 protected boolean isSuperInheritor(PsiClass aClass) {
128 return InheritanceUtil.isInheritorOrSelf(mySuper, aClass, true);
131 public PsiClass getSuper() {
132 return mySuper;
135 public PsiClass getTarget() {
136 return myClass;
139 public boolean isReplaceInstanceOf() {
140 return myReplaceInstanceOf;
143 @NotNull
144 protected Collection<? extends PsiElement> getElementsToWrite(@NotNull final UsageViewDescriptor descriptor) {
145 return Collections.emptyList(); // neither myClass nor mySuper are subject to change, it's just references that are going to change