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
.generation
.surroundWith
;
18 import com
.intellij
.codeInsight
.CodeInsightBundle
;
19 import com
.intellij
.codeInsight
.ExceptionUtil
;
20 import com
.intellij
.openapi
.editor
.Editor
;
21 import com
.intellij
.openapi
.project
.Project
;
22 import com
.intellij
.openapi
.ui
.Messages
;
23 import com
.intellij
.openapi
.util
.TextRange
;
24 import com
.intellij
.psi
.*;
25 import com
.intellij
.psi
.codeStyle
.CodeStyleManager
;
26 import com
.intellij
.psi
.codeStyle
.JavaCodeStyleManager
;
27 import com
.intellij
.psi
.codeStyle
.VariableKind
;
28 import com
.intellij
.util
.IncorrectOperationException
;
29 import org
.jetbrains
.annotations
.NonNls
;
31 import java
.util
.Collections
;
32 import java
.util
.List
;
34 public class JavaWithTryCatchSurrounder
extends JavaStatementsSurrounder
{
35 protected boolean myGenerateFinally
= false;
37 public String
getTemplateDescription() {
38 return CodeInsightBundle
.message("surround.with.try.catch.template");
41 public TextRange
surroundStatements(Project project
, Editor editor
, PsiElement container
, PsiElement
[] statements
)
42 throws IncorrectOperationException
{
43 PsiManager manager
= PsiManager
.getInstance(project
);
44 PsiElementFactory factory
= JavaPsiFacade
.getInstance(manager
.getProject()).getElementFactory();
45 JavaCodeStyleManager codeStyleManager
= JavaCodeStyleManager
.getInstance(project
);
47 statements
= SurroundWithUtil
.moveDeclarationsOut(container
, statements
, true);
48 if (statements
.length
== 0) {
52 List
<PsiClassType
> exceptions
= ExceptionUtil
.getUnhandledExceptions(statements
);
53 if (exceptions
.isEmpty()) {
54 exceptions
= ExceptionUtil
.getThrownExceptions(statements
);
55 if (exceptions
.isEmpty()) {
56 exceptions
= Collections
.singletonList(factory
.createTypeByFQClassName("java.lang.Exception", container
.getResolveScope()));
60 @NonNls StringBuilder buffer
= new StringBuilder();
61 buffer
.append("try{\n}");
62 for (PsiClassType exception
: exceptions
) {
63 buffer
.append("catch(Exception e){\n}");
65 if (myGenerateFinally
) {
66 buffer
.append("finally{\n}");
68 String text
= buffer
.toString();
69 PsiTryStatement tryStatement
= (PsiTryStatement
)factory
.createStatementFromText(text
, null);
70 tryStatement
= (PsiTryStatement
)CodeStyleManager
.getInstance(project
).reformat(tryStatement
);
72 tryStatement
= (PsiTryStatement
)container
.addAfter(tryStatement
, statements
[statements
.length
- 1]);
74 PsiCodeBlock tryBlock
= tryStatement
.getTryBlock();
75 tryBlock
.addRange(statements
[0], statements
[statements
.length
- 1]);
77 PsiCatchSection
[] catchSections
= tryStatement
.getCatchSections();
79 for (int i
= 0; i
< exceptions
.size(); i
++) {
80 PsiClassType exception
= exceptions
.get(i
);
81 String
[] nameSuggestions
= codeStyleManager
.suggestVariableName(VariableKind
.PARAMETER
, null, null, exception
).names
;
82 String name
= codeStyleManager
.suggestUniqueVariableName(nameSuggestions
[0], tryBlock
, false);
83 PsiCatchSection catchSection
;
85 catchSection
= factory
.createCatchSection(exception
, name
, null);
87 catch (IncorrectOperationException e
) {
88 Messages
.showErrorDialog(project
, CodeInsightBundle
.message("surround.with.try.catch.incorrect.template.message"),
89 CodeInsightBundle
.message("surround.with.try.catch.incorrect.template.title"));
92 catchSection
= (PsiCatchSection
)catchSections
[i
].replace(catchSection
);
93 codeStyleManager
.shortenClassReferences(catchSection
);
96 container
.deleteChildRange(statements
[0], statements
[statements
.length
- 1]);
98 PsiCodeBlock firstCatch
= tryStatement
.getCatchBlocks()[0];
99 return SurroundWithUtil
.getRangeToSelect(firstCatch
);