From 6b54c8047bc37a08220c07ac9195a1582819f231 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Fri, 2 Nov 2018 11:10:15 +0100 Subject: [PATCH] customization template for catch block parameter (IDEA-125327; IDEA-116670) --- .../JavaWithTryCatchSurrounder.java | 2 +- .../ide/fileTemplates/JavaTemplateUtil.java | 1 + .../impl/JavaPsiImplementationHelperImpl.java | 21 +++++++++++++- .../code/Catch Statement Declaration.java.ft | 1 + .../Catch Statement Declaration.java.html | 28 +++++++++++++++++++ ...urroundWithTryCatchWithFinalParameter.java | 10 +++++++ ...dWithTryCatchWithFinalParameter_after.java | 14 ++++++++++ .../surroundWith/JavaSurroundWithTest.java | 12 ++++++++ 8 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 java/java-impl/src/fileTemplates/code/Catch Statement Declaration.java.ft create mode 100644 java/java-impl/src/fileTemplates/code/Catch Statement Declaration.java.html create mode 100644 java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryCatchWithFinalParameter.java create mode 100644 java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryCatchWithFinalParameter_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryCatchSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryCatchSurrounder.java index 8e30093d47f5..38acd461b8c4 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryCatchSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithTryCatchSurrounder.java @@ -85,7 +85,7 @@ public class JavaWithTryCatchSurrounder extends JavaStatementsSurrounder { String name = codeStyleManager.suggestUniqueVariableName(nameSuggestions[0], tryBlock, false); PsiCatchSection catchSection; try { - catchSection = factory.createCatchSection(exception, name, null); + catchSection = factory.createCatchSection(exception, name, tryBlock); } catch (IncorrectOperationException e) { Messages.showErrorDialog(project, CodeInsightBundle.message("surround.with.try.catch.incorrect.template.message"), diff --git a/java/java-impl/src/com/intellij/ide/fileTemplates/JavaTemplateUtil.java b/java/java-impl/src/com/intellij/ide/fileTemplates/JavaTemplateUtil.java index 6c1948f28054..555418e4accc 100644 --- a/java/java-impl/src/com/intellij/ide/fileTemplates/JavaTemplateUtil.java +++ b/java/java-impl/src/com/intellij/ide/fileTemplates/JavaTemplateUtil.java @@ -27,6 +27,7 @@ import static com.intellij.util.ObjectUtils.notNull; */ public class JavaTemplateUtil { public static final String TEMPLATE_CATCH_BODY = "Catch Statement Body.java"; + public static final String TEMPLATE_CATCH_DECLARATION = "Catch Statement Declaration.java"; public static final String TEMPLATE_IMPLEMENTED_METHOD_BODY = "Implemented Method Body.java"; public static final String TEMPLATE_OVERRIDDEN_METHOD_BODY = "Overridden Method Body.java"; public static final String TEMPLATE_FROM_USAGE_METHOD_BODY = "New Method Body.java"; diff --git a/java/java-impl/src/com/intellij/psi/impl/JavaPsiImplementationHelperImpl.java b/java/java-impl/src/com/intellij/psi/impl/JavaPsiImplementationHelperImpl.java index 732b264fb9ff..f37c65555ce0 100644 --- a/java/java-impl/src/com/intellij/psi/impl/JavaPsiImplementationHelperImpl.java +++ b/java/java-impl/src/com/intellij/psi/impl/JavaPsiImplementationHelperImpl.java @@ -24,6 +24,7 @@ import com.intellij.openapi.vfs.jrt.JrtFileSystem; import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleSettings; +import com.intellij.psi.codeStyle.JavaCodeStyleManager; import com.intellij.psi.codeStyle.JavaCodeStyleSettings; import com.intellij.psi.codeStyle.arrangement.MemberOrderService; import com.intellij.psi.impl.compiled.ClsClassImpl; @@ -291,7 +292,7 @@ public class JavaPsiImplementationHelperImpl extends JavaPsiImplementationHelper @Override public void setupCatchBlock(@NotNull String exceptionName, @NotNull PsiType exceptionType, PsiElement context, @NotNull PsiCatchSection catchSection) { FileTemplate template = FileTemplateManager.getInstance(catchSection.getProject()).getCodeTemplate(JavaTemplateUtil.TEMPLATE_CATCH_BODY); - if (template == null) throw new IncorrectOperationException("Missing template: " + JavaTemplateUtil.TEMPLATE_CATCH_BODY); + FileTemplate declarationTemplate = FileTemplateManager.getInstance(catchSection.getProject()).getCodeTemplate(JavaTemplateUtil.TEMPLATE_CATCH_DECLARATION); Properties props = FileTemplateManager.getInstance(myProject).getDefaultProperties(); props.setProperty(FileTemplate.ATTRIBUTE_EXCEPTION, exceptionName); @@ -304,6 +305,24 @@ public class JavaPsiImplementationHelperImpl extends JavaPsiImplementationHelper } try { + PsiTryStatement tryStmt = (PsiTryStatement)PsiElementFactory.SERVICE.getInstance(myProject) + .createStatementFromText("try {} catch (" + declarationTemplate.getText(props) + ") {\n}", null); + PsiParameter parameter = tryStmt.getCatchSections()[0].getParameter(); + + String parameterName = parameter == null ? null : parameter.getName(); + if (parameterName != null) { + if (!exceptionName.equals(parameterName)) { + parameterName = JavaCodeStyleManager.getInstance(myProject).suggestUniqueVariableName(parameterName, context, false); + props.setProperty(FileTemplate.ATTRIBUTE_EXCEPTION, parameterName); + parameter.setName(parameterName); + } + + PsiParameter sectionParameter = catchSection.getParameter(); + if (sectionParameter != null) { + sectionParameter.replace(parameter); + } + } + PsiCodeBlock block = PsiElementFactory.SERVICE.getInstance(myProject).createCodeBlockFromText("{\n" + template.getText(props) + "\n}", null); Objects.requireNonNull(catchSection.getCatchBlock()).replace(block); diff --git a/java/java-impl/src/fileTemplates/code/Catch Statement Declaration.java.ft b/java/java-impl/src/fileTemplates/code/Catch Statement Declaration.java.ft new file mode 100644 index 000000000000..8235449ae333 --- /dev/null +++ b/java/java-impl/src/fileTemplates/code/Catch Statement Declaration.java.ft @@ -0,0 +1 @@ +${EXCEPTION_TYPE} ${EXCEPTION} \ No newline at end of file diff --git a/java/java-impl/src/fileTemplates/code/Catch Statement Declaration.java.html b/java/java-impl/src/fileTemplates/code/Catch Statement Declaration.java.html new file mode 100644 index 000000000000..795d822dbca0 --- /dev/null +++ b/java/java-impl/src/fileTemplates/code/Catch Statement Declaration.java.html @@ -0,0 +1,28 @@ + + + + + + +
This is a built-in template used for filling the + catch parameter when it is generated, e.g. when using the + Code | Surround with... function.
+ The template is editable, e.g. you can make the generated parameter final and always use predefined name despite of qualified name of the caught exception.
+
+ + + + + + + + + + + + + + +
Predefined variables will take the following values:
${EXCEPTION} name of the Exception variable specified as a catch parameter derived from caught exception type, e, if qualified name ends with Exception
${EXCEPTION_TYPE} type of the catch parameter
+ + \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryCatchWithFinalParameter.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryCatchWithFinalParameter.java new file mode 100644 index 000000000000..95bd9cb10805 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryCatchWithFinalParameter.java @@ -0,0 +1,10 @@ +class ModuleATest { + { + Exception ex = null; + foo(); + Exception ex1 = null; + } + + void foo() throws Smth {} + static class Smth extends Exception {} +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryCatchWithFinalParameter_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryCatchWithFinalParameter_after.java new file mode 100644 index 000000000000..44e36a6d35a5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundWithTryCatchWithFinalParameter_after.java @@ -0,0 +1,14 @@ +class ModuleATest { + { + Exception ex = null; + try { + foo(); + } catch (final Smth ex1) { + ex1.printStackTrace(); + } + Exception ex1 = null; + } + + void foo() throws Smth {} + static class Smth extends Exception {} +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/generation/surroundWith/JavaSurroundWithTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/generation/surroundWith/JavaSurroundWithTest.java index 8969bacf014c..c9e41098bbf0 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/generation/surroundWith/JavaSurroundWithTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/generation/surroundWith/JavaSurroundWithTest.java @@ -195,6 +195,18 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase { } } + public void testSurroundWithTryCatchWithFinalParameter() { + FileTemplate template = FileTemplateManager.getInstance(getProject()).getCodeTemplate(JavaTemplateUtil.TEMPLATE_CATCH_DECLARATION); + String old = template.getText(); + template.setText("final ${EXCEPTION_TYPE} ex"); + try { + doTest(new JavaWithTryCatchSurrounder()); + } + finally { + template.setText(old); + } + } + public void testSurroundIfBranchWithNoBracesAndComment() { doTest(new JavaWithBlockSurrounder()); }