Fixed KT-5443 J2K converter crashes when code contains catch with multiple exception types
#KT-5443 Fixed
This commit is contained in:
@@ -150,14 +150,7 @@ open class StatementVisitor(public val converter: Converter) : JavaElementVisito
|
|||||||
|
|
||||||
override fun visitTryStatement(tryStatement: PsiTryStatement) {
|
override fun visitTryStatement(tryStatement: PsiTryStatement) {
|
||||||
val tryBlock = tryStatement.getTryBlock()
|
val tryBlock = tryStatement.getTryBlock()
|
||||||
val catchesConverted = run {
|
val catchesConverted = convertCatches(tryStatement)
|
||||||
val catchBlocks = tryStatement.getCatchBlocks()
|
|
||||||
val catchBlockParameters = tryStatement.getCatchBlockParameters()
|
|
||||||
catchBlocks.indices.map {
|
|
||||||
CatchStatement(converter.convertParameter(catchBlockParameters[it], Nullability.NotNull),
|
|
||||||
converter.convertBlock(catchBlocks[it])).assignNoPrototype()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val finallyConverted = converter.convertBlock(tryStatement.getFinallyBlock())
|
val finallyConverted = converter.convertBlock(tryStatement.getFinallyBlock())
|
||||||
|
|
||||||
val resourceList = tryStatement.getResourceList()
|
val resourceList = tryStatement.getResourceList()
|
||||||
@@ -172,6 +165,29 @@ open class StatementVisitor(public val converter: Converter) : JavaElementVisito
|
|||||||
result = TryStatement(converter.convertBlock(tryBlock), catchesConverted, finallyConverted)
|
result = TryStatement(converter.convertBlock(tryBlock), catchesConverted, finallyConverted)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun convertCatches(tryStatement: PsiTryStatement): List<CatchStatement> {
|
||||||
|
val catches = ArrayList<CatchStatement>()
|
||||||
|
for ((block, parameter) in tryStatement.getCatchBlocks().zip(tryStatement.getCatchBlockParameters())) {
|
||||||
|
val blockConverted = converter.convertBlock(block)
|
||||||
|
val annotations = converter.convertAnnotations(parameter)
|
||||||
|
val parameterType = parameter.getType()
|
||||||
|
val types = if (parameterType is PsiDisjunctionType)
|
||||||
|
parameterType.getDisjunctions()
|
||||||
|
else
|
||||||
|
listOf(parameterType)
|
||||||
|
for (t in types) {
|
||||||
|
var convertedType = converter.typeConverter.convertType(t, Nullability.NotNull)
|
||||||
|
val convertedParameter = Parameter(parameter.declarationIdentifier(),
|
||||||
|
convertedType,
|
||||||
|
Parameter.VarValModifier.None,
|
||||||
|
annotations,
|
||||||
|
Modifiers.Empty).assignPrototype(parameter)
|
||||||
|
catches.add(CatchStatement(convertedParameter, blockConverted).assignNoPrototype())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return catches
|
||||||
|
}
|
||||||
|
|
||||||
private fun convertTryWithResources(tryBlock: PsiCodeBlock?, resourceVariables: List<PsiResourceVariable>, catchesConverted: List<CatchStatement>, finallyConverted: Block): Statement {
|
private fun convertTryWithResources(tryBlock: PsiCodeBlock?, resourceVariables: List<PsiResourceVariable>, catchesConverted: List<CatchStatement>, finallyConverted: Block): Statement {
|
||||||
var wrapResultStatement: (Expression) -> Statement = { it }
|
var wrapResultStatement: (Expression) -> Statement = { it }
|
||||||
var converterForBody = converter
|
var converterForBody = converter
|
||||||
|
|||||||
@@ -16,14 +16,17 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.j2k.test;
|
package org.jetbrains.jet.j2k.test;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
import junit.framework.TestSuite;
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
import org.jetbrains.jet.JetTestUtils;
|
import org.jetbrains.jet.JetTestUtils;
|
||||||
import org.jetbrains.jet.test.InnerTestClasses;
|
import org.jetbrains.jet.test.InnerTestClasses;
|
||||||
import org.jetbrains.jet.test.TestMetadata;
|
import org.jetbrains.jet.test.TestMetadata;
|
||||||
|
|
||||||
import java.io.File;
|
import org.jetbrains.jet.j2k.test.AbstractJavaToKotlinConverterTest;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
@@ -2946,6 +2949,11 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
|
|||||||
doTest("j2k/tests/testData/ast/tryStatement/commonCaseForTryStatement.java");
|
doTest("j2k/tests/testData/ast/tryStatement/commonCaseForTryStatement.java");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("disjunctionType.java")
|
||||||
|
public void testDisjunctionType() throws Exception {
|
||||||
|
doTest("j2k/tests/testData/ast/tryStatement/disjunctionType.java");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("emptyTryWithTwoCatchesWithEmptyFinally.java")
|
@TestMetadata("emptyTryWithTwoCatchesWithEmptyFinally.java")
|
||||||
public void testEmptyTryWithTwoCatchesWithEmptyFinally() throws Exception {
|
public void testEmptyTryWithTwoCatchesWithEmptyFinally() throws Exception {
|
||||||
doTest("j2k/tests/testData/ast/tryStatement/emptyTryWithTwoCatchesWithEmptyFinally.java");
|
doTest("j2k/tests/testData/ast/tryStatement/emptyTryWithTwoCatchesWithEmptyFinally.java");
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
//file
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
class A {
|
||||||
|
void foo() {
|
||||||
|
try {
|
||||||
|
bar()
|
||||||
|
}
|
||||||
|
catch(RuntimeException | IOException e) {
|
||||||
|
e.printStackTrace(); // print stack trace
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
class A {
|
||||||
|
fun foo() {
|
||||||
|
try {
|
||||||
|
bar()
|
||||||
|
} catch (e: RuntimeException) {
|
||||||
|
e.printStackTrace() // print stack trace
|
||||||
|
} catch (e: IOException) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user