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.
18 * created at Oct 8, 2001
21 package com
.intellij
.refactoring
.util
;
23 import com
.intellij
.psi
.*;
24 import com
.intellij
.psi
.util
.PsiFormatUtil
;
25 import com
.intellij
.psi
.util
.PsiUtil
;
26 import com
.intellij
.refactoring
.RefactoringBundle
;
27 import com
.intellij
.usageView
.UsageViewUtil
;
28 import com
.intellij
.util
.containers
.MultiMap
;
29 import org
.jetbrains
.annotations
.NotNull
;
30 import org
.jetbrains
.annotations
.Nullable
;
34 public class ConflictsUtil
{
35 private ConflictsUtil() {
39 public static PsiElement
getContainer(PsiElement place
) {
40 PsiElement parent
= place
;
42 if (parent
instanceof PsiMember
&& !(parent
instanceof PsiTypeParameter
))
44 if (parent
instanceof PsiFile
) return parent
;
45 parent
= parent
.getParent();
49 public static void checkMethodConflicts(@Nullable PsiClass aClass
,
50 PsiMethod refactoredMethod
,
52 final MultiMap
<PsiElement
,String
> conflicts
) {
53 if (prototype
== null) return;
55 PsiMethod method
= aClass
!= null ? aClass
.findMethodBySignature(prototype
, true) : null;
57 if (method
!= null && method
!= refactoredMethod
) {
58 if (aClass
.equals(method
.getContainingClass())) {
59 final String classDescr
= aClass
instanceof PsiAnonymousClass ?
60 RefactoringBundle
.message("current.class") :
61 RefactoringUIUtil
.getDescription(aClass
, false);
62 conflicts
.putValue(method
, RefactoringBundle
.message("method.0.is.already.defined.in.the.1",
63 getMethodPrototypeString(prototype
),
66 else { // method somewhere in base class
67 if (JavaPsiFacade
.getInstance(method
.getProject()).getResolveHelper().isAccessible(method
, aClass
, null)) {
68 String protoMethodInfo
= getMethodPrototypeString(prototype
);
69 String className
= CommonRefactoringUtil
.htmlEmphasize(UsageViewUtil
.getDescriptiveName(method
.getContainingClass()));
70 if (PsiUtil
.getAccessLevel(prototype
.getModifierList()) >= PsiUtil
.getAccessLevel(method
.getModifierList()) ) {
71 boolean isMethodAbstract
= method
.hasModifierProperty(PsiModifier
.ABSTRACT
);
72 boolean isMyMethodAbstract
= refactoredMethod
!= null && refactoredMethod
.hasModifierProperty(PsiModifier
.ABSTRACT
);
73 final String conflict
= isMethodAbstract
!= isMyMethodAbstract ?
74 RefactoringBundle
.message("method.0.will.implement.method.of.the.base.class", protoMethodInfo
, className
) :
75 RefactoringBundle
.message("method.0.will.override.a.method.of.the.base.class", protoMethodInfo
, className
);
76 conflicts
.putValue(method
, conflict
);
78 else { // prototype is private, will be compile-error
79 conflicts
.putValue(method
, RefactoringBundle
.message("method.0.will.hide.method.of.the.base.class",
80 protoMethodInfo
, className
));
87 private static String
getMethodPrototypeString(final PsiMethod prototype
) {
88 return PsiFormatUtil
.formatMethod(
90 PsiSubstitutor
.EMPTY
, PsiFormatUtil
.SHOW_NAME
| PsiFormatUtil
.SHOW_PARAMETERS
,
91 PsiFormatUtil
.SHOW_TYPE
95 public static void checkFieldConflicts(@Nullable PsiClass aClass
, String newName
, final Map
<PsiElement
, String
> conflicts
) {
96 PsiField existingField
= aClass
!= null ? aClass
.findFieldByName(newName
, true) : null;
97 if (existingField
!= null) {
98 if (aClass
.equals(existingField
.getContainingClass())) {
99 String className
= aClass
instanceof PsiAnonymousClass ?
100 RefactoringBundle
.message("current.class") :
101 RefactoringUIUtil
.getDescription(aClass
, false);
102 final String conflict
= RefactoringBundle
.message("field.0.is.already.defined.in.the.1",
103 existingField
.getName(), className
);
104 conflicts
.put(existingField
, conflict
);
106 else { // method somewhere in base class
107 if (!existingField
.hasModifierProperty(PsiModifier
.PRIVATE
)) {
108 String fieldInfo
= PsiFormatUtil
.formatVariable(existingField
, PsiFormatUtil
.SHOW_NAME
| PsiFormatUtil
.SHOW_TYPE
| PsiFormatUtil
.TYPE_AFTER
, PsiSubstitutor
.EMPTY
);
109 String className
= RefactoringUIUtil
.getDescription(existingField
.getContainingClass(), false);
110 final String descr
= RefactoringBundle
.message("field.0.will.hide.field.1.of.the.base.class",
111 newName
, fieldInfo
, className
);
112 conflicts
.put(existingField
, descr
);