[java] process inferred type parameters in add to throws/catch sections (IDEA-259729)

GitOrigin-RevId: aea7cd3f30f891d752116fb619d6215d3dc3431b
This commit is contained in:
Anna Kozlova
2022-05-16 11:37:24 +00:00
committed by intellij-monorepo-bot
parent bc51c73b75
commit 2ead949be8
6 changed files with 58 additions and 17 deletions
@@ -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 {
@@ -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,
@@ -0,0 +1,15 @@
// "Surround with try/catch" "true"
class Test {
interface I<E extends Exception> {
void call() throws E;
}
public static <E extends Exception> void method(I<E> i) {
try {
i.call();
} catch (Exception e) {
<selection><caret>throw new RuntimeException(e);</selection>
}
}
}
@@ -0,0 +1,11 @@
// "Surround with try/catch" "true"
class Test {
interface I<E extends Exception> {
void call() throws E;
}
public static <E extends Exception> void method(I<E> i) {
i.ca<caret>ll();
}
}
@@ -0,0 +1,11 @@
// "Add exception to method signature" "true"
class Test {
interface I<E extends Exception> {
void call() throws E;
}
public static <E extends Exception> void method(I<E> i) throws E {
i.call();
}
}
@@ -0,0 +1,11 @@
// "Add exception to method signature" "true"
class Test {
interface I<E extends Exception> {
void call() throws E;
}
public static <E extends Exception> void method(I<E> i) {
i.ca<caret>ll();
}
}