this question을 보면서 get
또는 set
사용 사이트에 @Throws
을 적용해도 아무런 효과가 없습니다. 또한 @ 목표가 속성 인 경우 목표가 적용되지 않습니다.
@Throws
에 대한는
AnnotationTarget.FUNCTION
,
AnnotationTarget.PROPERTY_GETTER
,
AnnotationTarget.PROPERTY_SETTER
및
AnnotationTarget.CONSTRUCTOR
있습니다.
JPA 주석 및 Deprecated
과 같은 기타 특수 효과가 올바르게 적용되며 해당 메소드에 올바르게 적용됩니다.
이상한 행동입니다.
설명하기 위해 Java로 간단한 추상 클래스를 생성했습니다. 하나의 생성자, 하나의 메소드 및 하나의 get 메소드가 있습니다.
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 {
}
}
모든 메소드/생성자는 IOException
으로 표시됩니다.
, 내가 코 틀린에 해당하는 클래스를 작성 및 상호 운용성에 대한 throws
에 적절한 방법을 표시하려고하면 발생 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 */
@Deprecated(
message = "Deprecated"
) // @Deprecated made it through!
@NotNull
public abstract String getString(); // Nothing here!
public abstract void setString(@NotNull String var1); // Nothing here!
public abstract void throwsFunction() throws IOException;
public KotlinAbstractClass() throws IOException {
}
}
, 이러한 내부 주석 대신 방법에 직접 적용되는 컴파일러에 의해 특별히 처리해야하기 때문에 것으로 보인다.
또한, 비 추상적 인 속성의 게터에 적용 :
val string: String
@Throws(IOException::class) get() = "Foo"
는는 서명 public final String getString() throws IOException
와 방법을 생성 않습니다!
아마도이 사례가 제대로 처리되지 않았습니까?
이것은 버그입니까?
참고 :이 메서드가 실제로이 예외를 throw하는지 여부와 관련이 없습니다.
내가 할 경우 :
@get:Throws(IOException::class)
val string: String
get() = BufferedReader(FileReader("file.txt")).readText()
컴파일 된 코드는 FileReader
constructor가 FileNotFoundException
발생한다는 사실에도 불구하고 여전히
@NotNull
public final String getString() {
return TextStreamsKt.readText((Reader)(new BufferedReader((Reader)(new FileReader("file.txt")))));
}
입니다.
또한 구현을 가질 수없고 여전히 throws
절을 가질 수 있으므로 추상 메소드의 경우 중요하지 않습니다.
@tynn이 제안하고, 구체적인 구현이 추가로 내가 할 경우
class ConcreteClass : KotlinAbstractClass() {
override val string: String
get() = BufferedReader(FileReader("file.txt")).readText()
...
}
나는 여전히 같은 결과를 얻을 수 있습니다.
override val string: String
@Throws(FileNotFoundException::class) get() = BufferedReader(FileReader("file.txt")).readText()
이는 서명 당신에게 throws
에 적절한 Java 버전을 제공한다 : 나는 @tynn 믿을
실제로이 예외를 발생시키는 getter 또는 setter를 구현해보십시오. 코드가 안전하다면, 오버라이드 한 것에 대한'throws' 선언을 재 선언 할 필요가 없습니다. 어쩌면 _Kotlin_은'@ Throws' 어노테이션으로 똑똑 할 수도 있습니다. – tynn
@ tynn 질문을 수정하겠습니다. – Moira