Java: fix extract method missing throws clause on string template and add test (IJ-CR-115108)

GitOrigin-RevId: 5fcbd8eb55a8f03f1db1ce67daa850964dae63a4
This commit is contained in:
Bas Leijdekkers
2023-09-21 14:33:12 +00:00
committed by intellij-monorepo-bot
parent 779e37f831
commit a4cb2b8f6f
6 changed files with 55 additions and 5 deletions
@@ -799,14 +799,14 @@ public final class ExceptionUtil {
public static HandlePlace getHandlePlace(@Nullable PsiElement element,
@NotNull PsiClassType exceptionType,
@Nullable PsiElement topElement) {
if (element == null || element.getParent() == topElement || element.getParent() == null) return HandlePlace.UNHANDLED;
if (element == null || element == topElement) return HandlePlace.UNHANDLED;
PsiElement parent = element.getParent();
if (parent == null || parent == topElement) return HandlePlace.UNHANDLED;
for (CustomExceptionHandler exceptionHandler : CustomExceptionHandler.KEY.getExtensionList()) {
if (exceptionHandler.isHandled(element, exceptionType, topElement)) return HandlePlace.UNKNOWN;
}
final PsiElement parent = element.getParent();
if (parent instanceof PsiMethod) {
PsiMethod method = (PsiMethod)parent;
return HandlePlace.fromBoolean(isHandledByMethodThrowsClause(method, exceptionType));
@@ -0,0 +1,21 @@
package java.lang;
public interface StringTemplate {
Processor<String, RuntimeException> STR = null;
@PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES)
@FunctionalInterface
public interface Processor<R, E extends Throwable> {
R process(StringTemplate stringTemplate) throws E;
}
}
class Main {
class FailureException extends Exception {}
public static void x(StringTemplate.Processor<String, FailureException> p, int i, int j) {
try {
String t = <selection>p."\{i} + \{j} = \{i + j}";</selection>
}
catch (FailureException ignore) {}
}
}
@@ -0,0 +1,25 @@
package java.lang;
public interface StringTemplate {
Processor<String, RuntimeException> STR = null;
@PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES)
@FunctionalInterface
public interface Processor<R, E extends Throwable> {
R process(StringTemplate stringTemplate) throws E;
}
}
class Main {
class FailureException extends Exception {}
public static void x(StringTemplate.Processor<String, FailureException> p, int i, int j) {
try {
String t = newMethod(p, i, j);
}
catch (FailureException ignore) {}
}
private static String newMethod(StringTemplate.Processor<String, FailureException> p, int i, int j) throws FailureException {
return p."\{i} + \{j} = \{i + j}";
}
}
@@ -262,8 +262,12 @@ public class ExtractMethodTest extends LightJavaCodeInsightTestCase {
doTestWithJava17();
}
public void testStringTemplate() throws Exception {
doTestWithLanguageLevel(LanguageLevel.JDK_21_PREVIEW);
public void testStringTemplateJava21Preview() throws Exception {
doTest();
}
public void testStringTemplateExceptionJava21Preview() throws Exception {
doTest();
}
private void doTestWithJava17() throws Exception {