From 2ead949be814cd2a49ddd544b42bc532241b2d87 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Mon, 16 May 2022 12:23:48 +0200 Subject: [PATCH] [java] process inferred type parameters in add to throws/catch sections (IDEA-259729) GitOrigin-RevId: aea7cd3f30f891d752116fb619d6215d3dc3431b --- .../JavaWithTryCatchSurrounder.java | 25 +++++++------------ .../src/com/intellij/psi/util/PsiUtil.java | 2 +- .../addCatchBlock/afterTypeParameter.java | 15 +++++++++++ .../addCatchBlock/beforeTypeParameter.java | 11 ++++++++ .../addToThrows/afterTypeParameter.java | 11 ++++++++ .../addToThrows/beforeTypeParameter.java | 11 ++++++++ 6 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addCatchBlock/afterTypeParameter.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addCatchBlock/beforeTypeParameter.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addToThrows/afterTypeParameter.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addToThrows/beforeTypeParameter.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 17542ed4ef7a..38d85d4febf9 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 @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.generation.surroundWith; import com.intellij.codeInsight.ExceptionUtil; @@ -62,7 +48,7 @@ public class JavaWithTryCatchSurrounder extends JavaStatementsSurrounder { @NonNls StringBuilder buffer = new StringBuilder(); buffer.append("try{\n}"); - for (PsiClassType exception : exceptions) { + for (PsiClassType ignored : exceptions) { buffer.append("catch(Exception e){\n}"); } if (myGenerateFinally) { @@ -82,6 +68,13 @@ public class JavaWithTryCatchSurrounder extends JavaStatementsSurrounder { for (int i = 0; i < exceptions.size(); i++) { PsiClassType exception = exceptions.get(i); + PsiClass target = exception.resolve(); + if (target instanceof PsiTypeParameter) { + PsiClassType[] extendsListTypes = target.getExtendsListTypes(); + if (extendsListTypes.length > 0) { + exception = extendsListTypes[0]; + } + } String name = new VariableNameGenerator(tryBlock, VariableKind.PARAMETER).byName("e", "ex", "exc").byType(exception).generate(false); PsiCatchSection catchSection; try { diff --git a/java/java-psi-api/src/com/intellij/psi/util/PsiUtil.java b/java/java-psi-api/src/com/intellij/psi/util/PsiUtil.java index 63c98d794384..d1a1bdf2e435 100644 --- a/java/java-psi-api/src/com/intellij/psi/util/PsiUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/util/PsiUtil.java @@ -165,7 +165,7 @@ public final class PsiUtil extends PsiUtilCore { } public static void addException(@NotNull PsiMethod method, @NotNull PsiClass exceptionClass) throws IncorrectOperationException { - addException(method, exceptionClass, exceptionClass.getQualifiedName()); + addException(method, exceptionClass, exceptionClass instanceof PsiTypeParameter ? exceptionClass.getName() : exceptionClass.getQualifiedName()); } private static void addException(@NotNull PsiMethod method, diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addCatchBlock/afterTypeParameter.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addCatchBlock/afterTypeParameter.java new file mode 100644 index 000000000000..1d23a0129846 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addCatchBlock/afterTypeParameter.java @@ -0,0 +1,15 @@ +// "Surround with try/catch" "true" +class Test { + + interface I { + void call() throws E; + } + + public static void method(I i) { + try { + i.call(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addCatchBlock/beforeTypeParameter.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addCatchBlock/beforeTypeParameter.java new file mode 100644 index 000000000000..d17f803c2d74 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addCatchBlock/beforeTypeParameter.java @@ -0,0 +1,11 @@ +// "Surround with try/catch" "true" +class Test { + + interface I { + void call() throws E; + } + + public static void method(I i) { + i.call(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addToThrows/afterTypeParameter.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addToThrows/afterTypeParameter.java new file mode 100644 index 000000000000..725064afac8c --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addToThrows/afterTypeParameter.java @@ -0,0 +1,11 @@ +// "Add exception to method signature" "true" +class Test { + + interface I { + void call() throws E; + } + + public static void method(I i) throws E { + i.call(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addToThrows/beforeTypeParameter.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addToThrows/beforeTypeParameter.java new file mode 100644 index 000000000000..1b85efcfc151 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/addToThrows/beforeTypeParameter.java @@ -0,0 +1,11 @@ +// "Add exception to method signature" "true" +class Test { + + interface I { + void call() throws E; + } + + public static void method(I i) { + i.call(); + } +} \ No newline at end of file