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
.codeInsight
.daemon
.impl
.analysis
.HighlightUtil
;
21 import com
.intellij
.codeInsight
.intention
.IntentionAction
;
22 import com
.intellij
.openapi
.editor
.Editor
;
23 import com
.intellij
.openapi
.project
.Project
;
24 import com
.intellij
.psi
.*;
25 import com
.intellij
.util
.IncorrectOperationException
;
26 import org
.jetbrains
.annotations
.NotNull
;
28 public class GeneralizeCatchFix
implements IntentionAction
{
29 // private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.quickfix.DeleteCatchFix");
31 private final PsiElement myElement
;
32 private final PsiClassType myUnhandledException
;
33 private PsiTryStatement myTryStatement
;
34 private PsiParameter myCatchParameter
;
36 public GeneralizeCatchFix(PsiElement element
, PsiClassType unhandledException
) {
38 myUnhandledException
= unhandledException
;
42 public String
getText() {
43 return QuickFixBundle
.message("generalize.catch.text",
44 HighlightUtil
.formatType(myCatchParameter
.getType()),
45 HighlightUtil
.formatType(myUnhandledException
));
49 public String
getFamilyName() {
50 return QuickFixBundle
.message("generalize.catch.family");
53 public boolean isAvailable(@NotNull Project project
, Editor editor
, PsiFile file
) {
54 if (!(myElement
!= null
55 && myElement
.isValid()
56 && myUnhandledException
!= null
57 && myUnhandledException
.isValid()
58 && myElement
.getManager().isInProject(myElement
))) return false;
59 // final enclosing try
60 PsiElement element
= myElement
;
61 while (element
!= null) {
62 if (element
instanceof PsiCodeBlock
63 && element
.getParent() instanceof PsiTryStatement
64 && ((PsiTryStatement
) element
.getParent()).getTryBlock() == element
) {
65 myTryStatement
= (PsiTryStatement
) element
.getParent();
68 if (element
instanceof PsiMethod
|| (element
instanceof PsiClass
&& !(element
instanceof PsiAnonymousClass
))) break;
69 element
= element
.getParent();
71 if (myTryStatement
== null) return false;
72 // check we can generalize at least one catch
73 PsiParameter
[] catchBlockParameters
= myTryStatement
.getCatchBlockParameters();
74 for (PsiParameter catchBlockParameter
: catchBlockParameters
) {
75 PsiType type
= catchBlockParameter
.getType();
76 if (myUnhandledException
.isAssignableFrom(type
)) {
77 myCatchParameter
= catchBlockParameter
;
81 return myCatchParameter
!= null;
84 public void invoke(@NotNull Project project
, Editor editor
, PsiFile file
) throws IncorrectOperationException
{
85 if (!CodeInsightUtilBase
.prepareFileForWrite(myElement
.getContainingFile())) return;
86 PsiElementFactory factory
= JavaPsiFacade
.getInstance(myElement
.getProject()).getElementFactory();
87 PsiTypeElement type
= factory
.createTypeElement(myUnhandledException
);
88 myCatchParameter
.getTypeElement().replace(type
);
91 public boolean startInWriteAction() {