update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / daemon / impl / quickfix / ReuseVariableDeclarationFix.java
blob18dd2f6283bc011630f575206eef95be2025039b
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.codeInsight.daemon.impl.quickfix;
18 import com.intellij.codeInsight.CodeInsightUtil;
19 import com.intellij.codeInsight.daemon.QuickFixBundle;
20 import com.intellij.codeInsight.intention.IntentionAction;
21 import com.intellij.openapi.editor.Editor;
22 import com.intellij.openapi.project.Project;
23 import com.intellij.openapi.util.Comparing;
24 import com.intellij.psi.*;
25 import com.intellij.psi.util.PsiUtil;
26 import com.intellij.psi.scope.processor.VariablesNotProcessor;
27 import com.intellij.psi.scope.util.PsiScopesUtil;
28 import com.intellij.util.IncorrectOperationException;
29 import org.jetbrains.annotations.NotNull;
31 /**
32 * Created by IntelliJ IDEA.
33 * User: cdr
34 * Date: Nov 20, 2002
35 * Time: 3:01:25 PM
36 * To change this template use Options | File Templates.
38 public class ReuseVariableDeclarationFix implements IntentionAction {
39 private final PsiVariable variable;
40 private final PsiIdentifier identifier;
42 public ReuseVariableDeclarationFix(PsiVariable variable, PsiIdentifier identifier) {
43 this.variable = variable;
44 this.identifier = identifier;
47 @NotNull
48 public String getFamilyName() {
49 return QuickFixBundle.message("reuse.variable.declaration.family");
52 @NotNull
53 public String getText() {
54 return QuickFixBundle.message("reuse.variable.declaration.text", variable.getName());
57 public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
59 PsiVariable previousVariable = findPreviousVariable();
60 return
61 variable != null
62 && variable.isValid()
63 && variable instanceof PsiLocalVariable
64 && previousVariable != null
65 && Comparing.equal(previousVariable.getType(), variable.getType())
66 && identifier != null
67 && identifier.isValid()
68 && variable.getManager().isInProject(variable);
71 public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
72 PsiVariable refVariable = findPreviousVariable();
73 if (refVariable == null) return;
74 if (!CodeInsightUtil.preparePsiElementsForWrite(variable, refVariable)) return;
75 PsiUtil.setModifierProperty(refVariable, PsiModifier.FINAL, false);
76 if (variable.getInitializer() == null) {
77 variable.delete();
78 return;
80 PsiDeclarationStatement declaration = (PsiDeclarationStatement) variable.getParent();
81 PsiElementFactory factory = JavaPsiFacade.getInstance(variable.getProject()).getElementFactory();
82 PsiStatement statement = factory.createStatementFromText(variable.getName() + " = " + variable.getInitializer().getText()+";", variable);
83 declaration.replace(statement);
86 private PsiVariable findPreviousVariable() {
87 PsiElement scope = variable.getParent();
88 while (scope != null) {
89 if (scope instanceof PsiFile || scope instanceof PsiMethod || scope instanceof PsiClassInitializer) break;
90 scope = scope.getParent();
92 if (scope == null) return null;
93 VariablesNotProcessor proc = new VariablesNotProcessor(variable, false);
94 PsiScopesUtil.treeWalkUp(proc, identifier, scope);
96 if(proc.size() > 0)
97 return proc.getResult(0);
98 return null;
101 public boolean startInWriteAction() {
102 return true;