j2k: Generate class literals in throws

This commit is contained in:
Denis Zharkov
2015-04-20 19:55:59 +03:00
parent 5ce4f4cad1
commit 036f31f36b
13 changed files with 28 additions and 22 deletions
@@ -566,7 +566,7 @@ class Converter private(
if (types.isEmpty()) return Annotations.Empty if (types.isEmpty()) return Annotations.Empty
val arguments = types.indices.map { index -> val arguments = types.indices.map { index ->
val convertedType = typeConverter.convertType(types[index], Nullability.NotNull) val convertedType = typeConverter.convertType(types[index], Nullability.NotNull)
null to deferredElement<Expression> { MethodCallExpression.buildNotNull(null, "javaClass", listOf(), listOf(convertedType)).assignPrototype(refElements[index]) } null to deferredElement<Expression> { ClassLiteralExpression(convertedType.assignPrototype(refElements[index])) }
} }
val annotation = Annotation(Identifier("throws").assignNoPrototype(), arguments, false, true) val annotation = Annotation(Identifier("throws").assignNoPrototype(), arguments, false, true)
return Annotations(listOf(annotation.assignPrototype(throwsList))).assignPrototype(throwsList) return Annotations(listOf(annotation.assignPrototype(throwsList))).assignPrototype(throwsList)
@@ -164,6 +164,12 @@ class DownToExpression(val start: Expression, val end: Expression): Expression()
} }
} }
class ClassLiteralExpression(val type: Type): Expression() {
override fun generateCode(builder: CodeBuilder) {
builder.append(type).append("::class")
}
}
fun createArrayInitializerExpression(arrayType: ArrayType, initializers: List<Expression>, needExplicitType: Boolean = true) : MethodCallExpression { fun createArrayInitializerExpression(arrayType: ArrayType, initializers: List<Expression>, needExplicitType: Boolean = true) : MethodCallExpression {
val elementType = arrayType.elementType val elementType = arrayType.elementType
val createArrayFunction = if (elementType is PrimitiveType) val createArrayFunction = if (elementType is PrimitiveType)
@@ -11,7 +11,7 @@ class Test : Base() {
return super.equals(o) return super.equals(o)
} }
throws(javaClass<CloneNotSupportedException>()) throws(CloneNotSupportedException::class)
override fun clone(): Any { override fun clone(): Any {
return super.clone() return super.clone()
} }
@@ -20,7 +20,7 @@ class Test : Base() {
return super.toString() return super.toString()
} }
throws(javaClass<Throwable>()) throws(Throwable::class)
override fun finalize() { override fun finalize() {
super.finalize() super.finalize()
} }
@@ -35,7 +35,7 @@ open class Base {
return super.equals(o) return super.equals(o)
} }
throws(javaClass<CloneNotSupportedException>()) throws(CloneNotSupportedException::class)
protected open fun clone(): Any { protected open fun clone(): Any {
return super.clone() return super.clone()
} }
@@ -44,7 +44,7 @@ open class Base {
return super.toString() return super.toString()
} }
throws(javaClass<Throwable>()) throws(Throwable::class)
protected open fun finalize() { protected open fun finalize() {
super.finalize() super.finalize()
} }
@@ -12,14 +12,14 @@ class X {
return super.toString() return super.toString()
} }
throws(javaClass<CloneNotSupportedException>()) throws(CloneNotSupportedException::class)
protected fun clone(): Any { protected fun clone(): Any {
return super.clone() return super.clone()
} }
} }
class Y : Thread() { class Y : Thread() {
throws(javaClass<CloneNotSupportedException>()) throws(CloneNotSupportedException::class)
override fun clone(): Any { override fun clone(): Any {
return super.clone() return super.clone()
} }
@@ -14,7 +14,7 @@ class X : Base() {
return super.toString() return super.toString()
} }
throws(javaClass<CloneNotSupportedException>()) throws(CloneNotSupportedException::class)
protected fun clone(): Any { protected fun clone(): Any {
return super.clone() return super.clone()
} }
@@ -1,2 +1,2 @@
throws(javaClass<IOException>(), javaClass<SerializationException>()) throws(IOException::class, SerializationException::class)
fun foo() fun foo()
@@ -1,7 +1,7 @@
import java.lang.reflect.Constructor import java.lang.reflect.Constructor
object X { object X {
throws(javaClass<Exception>()) throws(Exception::class)
fun <T> foo(constructor: Constructor<T>, args1: Array<Any>, args2: Array<Any>) { fun <T> foo(constructor: Constructor<T>, args1: Array<Any>, args2: Array<Any>) {
constructor.newInstance(*args1) constructor.newInstance(*args1)
constructor.newInstance(args1, args2) constructor.newInstance(args1, args2)
@@ -1,7 +1,7 @@
import java.io.* import java.io.*
public class C { public class C {
throws(javaClass<IOException>()) throws(IOException::class)
fun foo() { fun foo() {
ByteArrayInputStream(ByteArray(10)).use { stream -> ByteArrayInputStream(ByteArray(10)).use { stream ->
// reading something // reading something
@@ -1,7 +1,7 @@
import java.io.* import java.io.*
public class C { public class C {
throws(javaClass<IOException>()) throws(IOException::class)
fun foo() { fun foo() {
ByteArrayInputStream(ByteArray(10)).use { input -> ByteArrayInputStream(ByteArray(10)).use { input ->
ByteArrayOutputStream().use { output -> ByteArrayOutputStream().use { output ->
@@ -1,7 +1,7 @@
import java.io.* import java.io.*
public class C { public class C {
throws(javaClass<IOException>()) throws(IOException::class)
fun foo() { fun foo() {
ByteArrayInputStream(ByteArray(10)).use { stream -> println(stream.read()) } ByteArrayInputStream(ByteArray(10)).use { stream -> println(stream.read()) }
} }
@@ -1,7 +1,7 @@
import java.io.* import java.io.*
public class C { public class C {
throws(javaClass<IOException>()) throws(IOException::class)
fun foo() { fun foo() {
try { try {
ByteArrayInputStream(ByteArray(10)).use { stream -> ByteArrayInputStream(ByteArray(10)).use { stream ->
@@ -1,16 +1,16 @@
import java.io.* import java.io.*
trait I { trait I {
throws(javaClass<IOException>()) throws(IOException::class)
public fun doIt(stream: InputStream): Int public fun doIt(stream: InputStream): Int
} }
public class C { public class C {
throws(javaClass<IOException>()) throws(IOException::class)
fun foo() { fun foo() {
ByteArrayInputStream(ByteArray(10)).use { stream -> ByteArrayInputStream(ByteArray(10)).use { stream ->
bar(object : I { bar(object : I {
throws(javaClass<IOException>()) throws(IOException::class)
override fun doIt(stream: InputStream): Int { override fun doIt(stream: InputStream): Int {
return stream.available() return stream.available()
} }
@@ -18,7 +18,7 @@ public class C {
} }
} }
throws(javaClass<IOException>()) throws(IOException::class)
fun bar(i: I, stream: InputStream): Int { fun bar(i: I, stream: InputStream): Int {
return i.doIt(stream) return i.doIt(stream)
} }
@@ -1,16 +1,16 @@
import java.io.* import java.io.*
trait I { trait I {
throws(javaClass<IOException>()) throws(IOException::class)
public fun doIt(stream: InputStream): Int public fun doIt(stream: InputStream): Int
} }
public class C { public class C {
throws(javaClass<IOException>()) throws(IOException::class)
fun foo(): Int { fun foo(): Int {
ByteArrayInputStream(ByteArray(10)).use { stream -> ByteArrayInputStream(ByteArray(10)).use { stream ->
return bar(object : I { return bar(object : I {
throws(javaClass<IOException>()) throws(IOException::class)
override fun doIt(stream: InputStream): Int { override fun doIt(stream: InputStream): Int {
return stream.available() return stream.available()
} }
@@ -18,7 +18,7 @@ public class C {
} }
} }
throws(javaClass<IOException>()) throws(IOException::class)
fun bar(i: I, stream: InputStream): Int { fun bar(i: I, stream: InputStream): Int {
return i.doIt(stream) return i.doIt(stream)
} }