Fixed KT-5443 J2K converter crashes when code contains catch with multiple exception types

#KT-5443 Fixed
This commit is contained in:
Valentin Kipyatkov
2014-07-16 13:18:05 +04:00
parent 957ffb0313
commit b9bd5c8fb8
4 changed files with 61 additions and 10 deletions
@@ -150,14 +150,7 @@ open class StatementVisitor(public val converter: Converter) : JavaElementVisito
override fun visitTryStatement(tryStatement: PsiTryStatement) {
val tryBlock = tryStatement.getTryBlock()
val catchesConverted = run {
val catchBlocks = tryStatement.getCatchBlocks()
val catchBlockParameters = tryStatement.getCatchBlockParameters()
catchBlocks.indices.map {
CatchStatement(converter.convertParameter(catchBlockParameters[it], Nullability.NotNull),
converter.convertBlock(catchBlocks[it])).assignNoPrototype()
}
}
val catchesConverted = convertCatches(tryStatement)
val finallyConverted = converter.convertBlock(tryStatement.getFinallyBlock())
val resourceList = tryStatement.getResourceList()
@@ -172,6 +165,29 @@ open class StatementVisitor(public val converter: Converter) : JavaElementVisito
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 {
var wrapResultStatement: (Expression) -> Statement = { it }
var converterForBody = converter
@@ -16,14 +16,17 @@
package org.jetbrains.jet.j2k.test;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.io.File;
import java.util.regex.Pattern;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.test.InnerTestClasses;
import org.jetbrains.jet.test.TestMetadata;
import java.io.File;
import java.util.regex.Pattern;
import org.jetbrains.jet.j2k.test.AbstractJavaToKotlinConverterTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@@ -2946,6 +2949,11 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
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")
public void testEmptyTryWithTwoCatchesWithEmptyFinally() throws Exception {
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()
}
}
}