update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / daemon / impl / quickfix / MethodThrowsFix.java
blobde1858def50d4891355af0efe55f1a953d378edb
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.CodeInsightUtilBase;
19 import com.intellij.codeInsight.daemon.QuickFixBundle;
20 import com.intellij.codeInspection.IntentionAndQuickFixAction;
21 import com.intellij.openapi.command.undo.UndoUtil;
22 import com.intellij.openapi.diagnostic.Logger;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.openapi.editor.Editor;
25 import com.intellij.psi.*;
26 import com.intellij.psi.codeStyle.JavaCodeStyleManager;
27 import com.intellij.psi.util.PsiFormatUtil;
28 import com.intellij.util.IncorrectOperationException;
29 import org.jetbrains.annotations.NotNull;
31 public class MethodThrowsFix extends IntentionAndQuickFixAction {
32 private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.MethodThrowsFix");
34 private final PsiMethod myMethod;
35 private final String myThrowsCanonicalText;
36 private final boolean myShouldThrow;
37 private final boolean myShowContainingClass;
39 public MethodThrowsFix(PsiMethod method, PsiClassType exceptionType, boolean shouldThrow, boolean showContainingClass) {
40 myMethod = method;
41 myThrowsCanonicalText = exceptionType.getCanonicalText();
42 myShouldThrow = shouldThrow;
43 myShowContainingClass = showContainingClass;
46 @NotNull
47 public String getName() {
48 String methodName = PsiFormatUtil.formatMethod(myMethod,
49 PsiSubstitutor.EMPTY,
50 PsiFormatUtil.SHOW_NAME | (myShowContainingClass ? PsiFormatUtil.SHOW_CONTAINING_CLASS: 0),
51 0);
52 return QuickFixBundle.message(myShouldThrow ? "fix.throws.list.add.exception" : "fix.throws.list.remove.exception",
53 myThrowsCanonicalText,
54 methodName);
57 @NotNull
58 public String getFamilyName() {
59 return QuickFixBundle.message("fix.throws.list.family");
62 public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
63 return myMethod != null
64 && myMethod.isValid()
65 && myMethod.getManager().isInProject(myMethod);
68 public void applyFix(final Project project, final PsiFile file, final Editor editor) {
69 if (!CodeInsightUtilBase.prepareFileForWrite(myMethod.getContainingFile())) return;
70 PsiJavaCodeReferenceElement[] referenceElements = myMethod.getThrowsList().getReferenceElements();
71 try {
72 boolean alreadyThrows = false;
73 for (PsiJavaCodeReferenceElement referenceElement : referenceElements) {
74 if (referenceElement.getCanonicalText().equals(myThrowsCanonicalText)) {
75 alreadyThrows = true;
76 if (!myShouldThrow) {
77 referenceElement.delete();
78 break;
82 if (myShouldThrow && !alreadyThrows) {
83 final PsiElementFactory factory = JavaPsiFacade.getInstance(myMethod.getProject()).getElementFactory();
84 final PsiClassType type = (PsiClassType)factory.createTypeFromText(myThrowsCanonicalText, myMethod);
85 PsiJavaCodeReferenceElement ref = factory.createReferenceElementByType(type);
86 ref = (PsiJavaCodeReferenceElement)JavaCodeStyleManager.getInstance(project).shortenClassReferences(ref);
87 myMethod.getThrowsList().add(ref);
89 UndoUtil.markPsiFileForUndo(file);
90 } catch (IncorrectOperationException e) {
91 LOG.error(e);