Fixed KT-5223 Converter from Java should convert "throws" in method declaration
#KT-5223 Fixed
This commit is contained in:
@@ -328,7 +328,7 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
||||
private fun doConvertMethod(method: PsiMethod, membersToRemove: MutableSet<PsiMember>): Function {
|
||||
val returnType = typeConverter.convertMethodReturnType(method)
|
||||
|
||||
val annotations = convertAnnotations(method)
|
||||
val annotations = convertAnnotations(method) + convertThrows(method)
|
||||
val modifiers = convertModifiers(method)
|
||||
|
||||
val comments = getComments(method)
|
||||
@@ -683,6 +683,15 @@ public class Converter private(val project: Project, val settings: ConverterSett
|
||||
}
|
||||
}
|
||||
|
||||
private fun convertThrows(method: PsiMethod): Annotations {
|
||||
val types = method.getThrowsList().getReferencedTypes()
|
||||
if (types.isEmpty()) return Annotations.Empty
|
||||
return Annotations(listOf(Annotation(Identifier("throws"),
|
||||
types.map { null to MethodCallExpression.buildNotNull(null, "javaClass", listOf(), listOf(typeConverter.convertType(it, Nullability.NotNull))) },
|
||||
false)),
|
||||
true)
|
||||
}
|
||||
|
||||
private val TYPE_MAP: Map<String, String> = mapOf(
|
||||
JAVA_LANG_BYTE to "byte",
|
||||
JAVA_LANG_SHORT to "short",
|
||||
|
||||
@@ -1305,6 +1305,11 @@ public class JavaToKotlinConverterTestGenerated extends AbstractJavaToKotlinConv
|
||||
doTest("j2k/tests/testData/ast/function/public.java");
|
||||
}
|
||||
|
||||
@TestMetadata("throws.java")
|
||||
public void testThrows() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/function/throws.java");
|
||||
}
|
||||
|
||||
@TestMetadata("varVararg.java")
|
||||
public void testVarVararg() throws Exception {
|
||||
doTest("j2k/tests/testData/ast/function/varVararg.java");
|
||||
|
||||
@@ -9,6 +9,7 @@ class Test() : Base() {
|
||||
return super.equals(o)
|
||||
}
|
||||
|
||||
throws(javaClass<CloneNotSupportedException>())
|
||||
override fun clone(): Any {
|
||||
return super.clone()
|
||||
}
|
||||
@@ -17,6 +18,7 @@ class Test() : Base() {
|
||||
return super.toString()
|
||||
}
|
||||
|
||||
throws(javaClass<Throwable>())
|
||||
override fun finalize() {
|
||||
super.finalize()
|
||||
}
|
||||
@@ -31,6 +33,7 @@ class Base() {
|
||||
return super.equals(o)
|
||||
}
|
||||
|
||||
throws(javaClass<CloneNotSupportedException>())
|
||||
protected fun clone(): Any {
|
||||
return super.clone()
|
||||
}
|
||||
@@ -39,6 +42,7 @@ class Base() {
|
||||
return super.toString()
|
||||
}
|
||||
|
||||
throws(javaClass<Throwable>())
|
||||
protected fun finalize() {
|
||||
super.finalize()
|
||||
}
|
||||
|
||||
@@ -11,12 +11,14 @@ class X() {
|
||||
return super.toString()
|
||||
}
|
||||
|
||||
throws(javaClass<CloneNotSupportedException>())
|
||||
protected fun clone(): Any {
|
||||
return super.clone()
|
||||
}
|
||||
}
|
||||
|
||||
class Y() : Thread() {
|
||||
throws(javaClass<CloneNotSupportedException>())
|
||||
override fun clone(): Any {
|
||||
return super.clone()
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ class X() : Base() {
|
||||
return super.toString()
|
||||
}
|
||||
|
||||
throws(javaClass<CloneNotSupportedException>())
|
||||
protected fun clone(): Any {
|
||||
return super.clone()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
//method
|
||||
void foo() throws IOException, SerializationException;
|
||||
@@ -0,0 +1,2 @@
|
||||
throws(javaClass<IOException>(), javaClass<SerializationException>())
|
||||
fun foo()
|
||||
@@ -1,6 +1,7 @@
|
||||
import java.io.*
|
||||
|
||||
public class C() {
|
||||
throws(javaClass<IOException>())
|
||||
fun foo() {
|
||||
FileInputStream("foo").use { stream ->
|
||||
// reading something
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import java.io.*
|
||||
|
||||
public class C() {
|
||||
throws(javaClass<IOException>())
|
||||
fun foo() {
|
||||
FileInputStream("foo").use { input ->
|
||||
FileOutputStream("bar").use { output ->
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import java.io.*
|
||||
|
||||
public class C() {
|
||||
throws(javaClass<IOException>())
|
||||
fun foo() {
|
||||
FileInputStream("foo").use { stream -> System.out.println(stream.read()) }
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import java.io.*
|
||||
|
||||
public class C() {
|
||||
throws(javaClass<IOException>())
|
||||
fun foo() {
|
||||
try {
|
||||
FileInputStream("foo").use { stream ->
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
import java.io.*
|
||||
|
||||
trait I {
|
||||
throws(javaClass<IOException>())
|
||||
public fun doIt(stream: InputStream): Int
|
||||
}
|
||||
|
||||
public class C() {
|
||||
throws(javaClass<IOException>())
|
||||
fun foo() {
|
||||
FileInputStream("foo").use { stream ->
|
||||
bar(object : I {
|
||||
throws(javaClass<IOException>())
|
||||
override fun doIt(stream: InputStream): Int {
|
||||
return stream.available()
|
||||
}
|
||||
@@ -15,6 +18,7 @@ public class C() {
|
||||
}
|
||||
}
|
||||
|
||||
throws(javaClass<IOException>())
|
||||
fun bar(i: I, stream: InputStream): Int {
|
||||
return i.doIt(stream)
|
||||
}
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
import java.io.*
|
||||
|
||||
trait I {
|
||||
throws(javaClass<IOException>())
|
||||
public fun doIt(stream: InputStream): Int
|
||||
}
|
||||
|
||||
public class C() {
|
||||
throws(javaClass<IOException>())
|
||||
fun foo(): Int {
|
||||
return FileInputStream("foo").use { stream ->
|
||||
bar(object : I {
|
||||
throws(javaClass<IOException>())
|
||||
override fun doIt(stream: InputStream): Int {
|
||||
return stream.available()
|
||||
}
|
||||
@@ -15,6 +18,7 @@ public class C() {
|
||||
}
|
||||
}
|
||||
|
||||
throws(javaClass<IOException>())
|
||||
fun bar(i: I, stream: InputStream): Int {
|
||||
return i.doIt(stream)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user