How to specify @Throws for a property in an interface

I am currently porting Java RMI code to Kotlin. Inherited interface in Java:

interface Foo: Remote {
    Bar getBar() throws RemoteException
}

After starting the automatic migration tool, the field barwill be changed to the property:

interface Foo: Remote {
    val bar: Bar
}

However, the ported program is getBarno longer marked as throws RemoteException, which causes an error illegal remote method encounteredin the RMI call.

I was wondering if there is a way to mark @Throwsfor a property?

+2
source share
1 answer

Well, if you look @Throws:

If there is a specific getter that does not use a support field, simply annotate it directly:

val bar: Bar
    @Throws(RemoteException::class) get() = doSomething()

Acceptable goals for @Throwsare

AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.PROPERTY_SETTER,
AnnotationTarget.CONSTRUCTOR

, , :

@get:Throws(RemoteException::class)

:

  • ( Java);
  • ;
  • get ( getter);
  • set (set setter);
  • ( );
  • param ( );
  • setparam ( );
  • delegate (, ).

@get , .

interface Foo: Remote {
    @get:Throws(RemoteException::class)
    val bar: Bar
}

, - throws. , , . CONSTRUCTOR FUNCTION , , .


Kotlin, , this:

interface ReplStateFacade : Remote {

    @Throws(RemoteException::class)
    fun getId(): Int

    ...
}

, @Throws. , ?

+4

Source: https://habr.com/ru/post/1690510/


All Articles