Java to Kotlin converter: no special logic for returns in try-with-resource is needed anymore
This commit is contained in:
@@ -246,22 +246,6 @@ open class StatementVisitor(public val converter: Converter) : JavaElementVisito
|
|||||||
var wrapResultStatement: (Expression) -> Statement = { it }
|
var wrapResultStatement: (Expression) -> Statement = { it }
|
||||||
var converterForBody = converter
|
var converterForBody = converter
|
||||||
|
|
||||||
val returns = collectReturns(tryBlock)
|
|
||||||
//TODO: support other returns when non-local returns supported by Kotlin
|
|
||||||
if (returns.size == 1 && returns.single() == tryBlock!!.getStatements().last()) {
|
|
||||||
wrapResultStatement = { ReturnStatement(it).assignPrototype(returns.single()) }
|
|
||||||
converterForBody = converter.withStatementVisitor { object : StatementVisitor(it) {
|
|
||||||
override fun visitReturnStatement(statement: PsiReturnStatement) {
|
|
||||||
if (statement == returns.single()) {
|
|
||||||
result = converter.convertExpression(statement.getReturnValue(), tryBlock!!.getContainingMethod()?.getReturnType())
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
super.visitReturnStatement(statement)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
}
|
|
||||||
|
|
||||||
var block = converterForBody.convertBlock(tryBlock)
|
var block = converterForBody.convertBlock(tryBlock)
|
||||||
var expression: Expression = Expression.Empty
|
var expression: Expression = Expression.Empty
|
||||||
for (variable in resourceVariables.reverse()) {
|
for (variable in resourceVariables.reverse()) {
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
//file
|
|
||||||
import java.io.*;
|
|
||||||
|
|
||||||
public class C {
|
|
||||||
int foo() {
|
|
||||||
try(InputStream stream = new FileInputStream("foo")) {
|
|
||||||
while(true) {
|
|
||||||
int c = stream.read();
|
|
||||||
if (c < 0 || c > 127) return c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
System.out.println(e);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,10 +3,10 @@ import java.io.*
|
|||||||
public class C {
|
public class C {
|
||||||
fun foo(): Int {
|
fun foo(): Int {
|
||||||
try {
|
try {
|
||||||
return FileInputStream("foo").use { stream ->
|
FileInputStream("foo").use { stream ->
|
||||||
// reading something
|
// reading something
|
||||||
val c = stream.read()
|
val c = stream.read()
|
||||||
c
|
return c
|
||||||
}
|
}
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
System.out.println(e)
|
System.out.println(e)
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ trait I {
|
|||||||
public class C {
|
public class C {
|
||||||
throws(javaClass<IOException>())
|
throws(javaClass<IOException>())
|
||||||
fun foo(): Int {
|
fun foo(): Int {
|
||||||
return FileInputStream("foo").use { stream ->
|
FileInputStream("foo").use { stream ->
|
||||||
bar(object : I {
|
return bar(object : I {
|
||||||
throws(javaClass<IOException>())
|
throws(javaClass<IOException>())
|
||||||
override fun doIt(stream: InputStream): Int {
|
override fun doIt(stream: InputStream): Int {
|
||||||
return stream.available()
|
return stream.available()
|
||||||
|
|||||||
Reference in New Issue
Block a user