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
.Nullable
;
33 public class ConflictsUtil
{
34 private ConflictsUtil() {
38 public static PsiMember
getContainer(PsiElement place
) {
39 PsiElement parent
= place
;
41 if (parent
instanceof PsiMember
&& !(parent
instanceof PsiTypeParameter
))
42 return (PsiMember
)parent
;
43 if (parent
instanceof PsiFile
) return null;
44 parent
= parent
.getParent();
48 public static void checkMethodConflicts(@Nullable PsiClass aClass
,
49 PsiMethod refactoredMethod
,
51 final MultiMap
<PsiElement
,String
> conflicts
) {
52 if (prototype
== null) return;
54 PsiMethod method
= aClass
!= null ? aClass
.findMethodBySignature(prototype
, true) : null;
56 if (method
!= null && method
!= refactoredMethod
) {
57 if (aClass
.equals(method
.getContainingClass())) {
58 final String classDescr
= aClass
instanceof PsiAnonymousClass ?
59 RefactoringBundle
.message("current.class") :
60 RefactoringUIUtil
.getDescription(aClass
, false);
61 conflicts
.putValue(method
, RefactoringBundle
.message("method.0.is.already.defined.in.the.1",
62 getMethodPrototypeString(prototype
),
65 else { // method somewhere in base class
66 if (JavaPsiFacade
.getInstance(method
.getProject()).getResolveHelper().isAccessible(method
, aClass
, null)) {
67 String protoMethodInfo
= getMethodPrototypeString(prototype
);
68 String className
= CommonRefactoringUtil
.htmlEmphasize(UsageViewUtil
.getDescriptiveName(method
.getContainingClass()));
69 if (PsiUtil
.getAccessLevel(prototype
.getModifierList()) >= PsiUtil
.getAccessLevel(method
.getModifierList()) ) {
70 boolean isMethodAbstract
= method
.hasModifierProperty(PsiModifier
.ABSTRACT
);
71 boolean isMyMethodAbstract
= refactoredMethod
!= null && refactoredMethod
.hasModifierProperty(PsiModifier
.ABSTRACT
);
72 final String conflict
= isMethodAbstract
!= isMyMethodAbstract ?
73 RefactoringBundle
.message("method.0.will.implement.method.of.the.base.class", protoMethodInfo
, className
) :
74 RefactoringBundle
.message("method.0.will.override.a.method.of.the.base.class", protoMethodInfo
, className
);
75 conflicts
.putValue(method
, conflict
);
77 else { // prototype is private, will be compile-error
78 conflicts
.putValue(method
, RefactoringBundle
.message("method.0.will.hide.method.of.the.base.class",
79 protoMethodInfo
, className
));
86 private static String
getMethodPrototypeString(final PsiMethod prototype
) {
87 return PsiFormatUtil
.formatMethod(
89 PsiSubstitutor
.EMPTY
, PsiFormatUtil
.SHOW_NAME
| PsiFormatUtil
.SHOW_PARAMETERS
,
90 PsiFormatUtil
.SHOW_TYPE
94 public static void checkFieldConflicts(@Nullable PsiClass aClass
, String newName
, final Map
<PsiElement
, String
> conflicts
) {
95 PsiField existingField
= aClass
!= null ? aClass
.findFieldByName(newName
, true) : null;
96 if (existingField
!= null) {
97 if (aClass
.equals(existingField
.getContainingClass())) {
98 String className
= aClass
instanceof PsiAnonymousClass ?
99 RefactoringBundle
.message("current.class") :
100 RefactoringUIUtil
.getDescription(aClass
, false);
101 final String conflict
= RefactoringBundle
.message("field.0.is.already.defined.in.the.1",
102 existingField
.getName(), className
);
103 conflicts
.put(existingField
, conflict
);
105 else { // method somewhere in base class
106 if (!existingField
.hasModifierProperty(PsiModifier
.PRIVATE
)) {
107 String fieldInfo
= PsiFormatUtil
.formatVariable(existingField
, PsiFormatUtil
.SHOW_NAME
| PsiFormatUtil
.SHOW_TYPE
| PsiFormatUtil
.TYPE_AFTER
, PsiSubstitutor
.EMPTY
);
108 String className
= RefactoringUIUtil
.getDescription(existingField
.getContainingClass(), false);
109 final String descr
= RefactoringBundle
.message("field.0.will.hide.field.1.of.the.base.class",
110 newName
, fieldInfo
, className
);
111 conflicts
.put(existingField
, descr
);