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 by IntelliJ IDEA.
22 * To change template for new class use
23 * Code Style | Class Templates options (Tools | IDE Options).
25 package com
.intellij
.refactoring
.makeStatic
;
27 import com
.intellij
.openapi
.actionSystem
.DataContext
;
28 import com
.intellij
.openapi
.actionSystem
.LangDataKeys
;
29 import com
.intellij
.openapi
.actionSystem
.PlatformDataKeys
;
30 import com
.intellij
.openapi
.application
.ApplicationManager
;
31 import com
.intellij
.openapi
.diagnostic
.Logger
;
32 import com
.intellij
.openapi
.editor
.Editor
;
33 import com
.intellij
.openapi
.editor
.ScrollType
;
34 import com
.intellij
.openapi
.project
.Project
;
35 import com
.intellij
.psi
.*;
36 import com
.intellij
.psi
.codeStyle
.JavaCodeStyleManager
;
37 import com
.intellij
.psi
.codeStyle
.VariableKind
;
38 import com
.intellij
.refactoring
.HelpID
;
39 import com
.intellij
.refactoring
.RefactoringActionHandler
;
40 import com
.intellij
.refactoring
.RefactoringBundle
;
41 import com
.intellij
.refactoring
.util
.CommonRefactoringUtil
;
42 import org
.jetbrains
.annotations
.NotNull
;
43 import org
.jetbrains
.annotations
.Nullable
;
45 public class MakeStaticHandler
implements RefactoringActionHandler
{
46 public static final String REFACTORING_NAME
= RefactoringBundle
.message("make.method.static.title");
47 private static final Logger LOG
= Logger
.getInstance("#com.intellij.refactoring.makeMethodStatic.MakeMethodStaticHandler");
49 public void invoke(@NotNull Project project
, Editor editor
, PsiFile file
, DataContext dataContext
) {
50 PsiElement element
= LangDataKeys
.PSI_ELEMENT
.getData(dataContext
);
51 editor
.getScrollingModel().scrollToCaret(ScrollType
.MAKE_VISIBLE
);
52 if (element
== null) {
53 element
= file
.findElementAt(editor
.getCaretModel().getOffset());
56 if (element
== null) return;
57 if (element
instanceof PsiIdentifier
) element
= element
.getParent();
59 if(!(element
instanceof PsiTypeParameterListOwner
)) {
60 String message
= RefactoringBundle
.getCannotRefactorMessage(RefactoringBundle
.message("error.wrong.caret.position.method.or.class.name"));
61 CommonRefactoringUtil
.showErrorHint(project
, editor
, message
, REFACTORING_NAME
, HelpID
.MAKE_METHOD_STATIC
);
64 if(LOG
.isDebugEnabled()) {
65 LOG
.debug("MakeStaticHandler invoked");
67 invoke(project
, new PsiElement
[]{element
}, dataContext
);
70 public void invoke(@NotNull final Project project
, @NotNull PsiElement
[] elements
, DataContext dataContext
) {
71 if(elements
.length
!= 1 || !(elements
[0] instanceof PsiTypeParameterListOwner
)) return;
73 final PsiTypeParameterListOwner member
= (PsiTypeParameterListOwner
)elements
[0];
74 if (!CommonRefactoringUtil
.checkReadOnlyStatus(project
, member
)) return;
76 String error
= validateTarget(member
);
78 Editor editor
= PlatformDataKeys
.EDITOR
.getData(dataContext
);
79 CommonRefactoringUtil
.showErrorHint(project
, editor
, error
, REFACTORING_NAME
, HelpID
.MAKE_METHOD_STATIC
);
83 final InternalUsageInfo
[] classRefsInMember
= MakeStaticUtil
.findClassRefsInMember(member
, false);
86 String classParameterName = "anObject";
87 ParameterTablePanel.VariableData[] fieldParameterData = null;
90 AbstractMakeStaticDialog dialog
;
91 if (!ApplicationManager
.getApplication().isUnitTestMode()) {
93 if (classRefsInMember
.length
> 0) {
94 final PsiType type
= JavaPsiFacade
.getInstance(member
.getProject()).getElementFactory().createType(member
.getContainingClass());
96 String
[] nameSuggestions
=
97 JavaCodeStyleManager
.getInstance(project
).suggestVariableName(VariableKind
.PARAMETER
, null, null, type
).names
;
99 dialog
= new MakeParameterizedStaticDialog(project
, member
,
106 dialog
= new SimpleMakeStaticDialog(project
, member
);
114 public static String
validateTarget(final PsiTypeParameterListOwner member
) {
115 final PsiClass containingClass
= member
.getContainingClass();
117 // Checking various preconditions
118 if(member
instanceof PsiMethod
&& ((PsiMethod
)member
).isConstructor()) {
119 return RefactoringBundle
.getCannotRefactorMessage(RefactoringBundle
.message("constructor.cannot.be.made.static"));
122 if(member
.getContainingClass() == null) {
123 return RefactoringBundle
.getCannotRefactorMessage(RefactoringBundle
.message("this.member.does.not.seem.to.belong.to.any.class"));
126 if(member
.hasModifierProperty(PsiModifier
.STATIC
)) {
127 return RefactoringBundle
.getCannotRefactorMessage(RefactoringBundle
.message("member.is.already.static"));
130 if(member
instanceof PsiMethod
&& member
.hasModifierProperty(PsiModifier
.ABSTRACT
)) {
131 return RefactoringBundle
.getCannotRefactorMessage(RefactoringBundle
.message("cannot.make.abstract.method.static"));
134 if(containingClass
instanceof PsiAnonymousClass
||
135 (containingClass
.getContainingClass() != null && !containingClass
.hasModifierProperty(PsiModifier
.STATIC
))) {
136 return RefactoringBundle
.getCannotRefactorMessage(RefactoringBundle
.message("inner.classes.cannot.have.static.members"));