Looking at this question , I noticed that applying @Throws
to get
or set
has no effect.
Further, only valid for purposes @Throws
are AnnotationTarget.FUNCTION
, AnnotationTarget.PROPERTY_GETTER
, AnnotationTarget.PROPERTY_SETTER
and AnnotationTarget.CONSTRUCTOR
.
Other annotations, such as and JPA annotations Deprecated
, work great and apply to the method!
This is a weird behavior.
To demonstrate, I created a simple abstract class in Java, with one constructor, one method, and one get method.
public abstract class JavaAbstractClass {
@Deprecated
@NotNull public abstract String getString() throws IOException;
public abstract void setString(@NotNull String string) throws IOException;
public abstract void throwsFunction() throws IOException;
public JavaAbstractClass() throws IOException {
}
}
As you can see, each method / constructor is marked as metaling IOException
.
, Kotlin throws
interop, getString
setString
throws
.
abstract class KotlinAbstractClass @Throws(IOException::class) constructor() {
@get:Deprecated("Deprecated")
@get:Throws(IOException::class)
@set:Throws(IOException::class)
abstract var string: String
@Throws(IOException::class)
abstract fun throwsFunction()
}
:
@Metadata(Some metadata here)
public abstract class KotlinAbstractClass {
@Deprecated(
message = "Deprecated"
)
@NotNull
public abstract String getString();
public abstract void setString(@NotNull String var1);
public abstract void throwsFunction() throws IOException;
public KotlinAbstractClass() throws IOException {
}
}
, , , , .
, :
val string: String
@Throws(IOException::class) get() = "Foo"
public final String getString() throws IOException
!
, ?
?
. , .
:
@get:Throws(IOException::class)
val string: String
get() = BufferedReader(FileReader("file.txt")).readText()
-
@NotNull
public final String getString() {
return TextStreamsKt.readText((Reader)(new BufferedReader((Reader)(new FileReader("file.txt")))));
}
, FileReader
FileNotFoundException
.
, , throws
.
@tynn, :
class ConcreteClass : KotlinAbstractClass() {
override val string: String
get() = BufferedReader(FileReader("file.txt")).readText()
...
}
.