Annotations on Interfaces?

前端 未结 10 2141
一生所求
一生所求 2020-12-08 21:52

I can\'t figure out a use case for being able to annotate interfaces in Java.

Maybe someone could give me an example?

相关标签:
10条回答
  • 2020-12-08 22:39

    I mark my interfaces with @Immutable to clearly indicate to developers of subclasses which Mutability contract a class implementing the interface should have. As gehel puts it, programming by contract.

    0 讨论(0)
  • 2020-12-08 22:44

    More an example, but Local and Remote annotations in EJB3. According to the java doc,

    When used on an interface, designates that interface as a local business interface.

    I guess the use case here is that the interface has a special function best denoted by an annotation.

    0 讨论(0)
  • 2020-12-08 22:47

    I have used it when defining a REST interface. RESTeasy allows you to create REST client that uses an annotated interface (RESTInterfaceV1 in this example):

    final RESTInterfaceV1 client = ProxyFactory.create(RESTInterfaceV1.class, "http://localhost:8080/rest");
    

    Although the annotations have to be duplicated on object that actually implements the REST interface, the interface itself can be distributed separately to those wanting to make a Java REST client.

    0 讨论(0)
  • 2020-12-08 22:50

    I use findbugs extensively. I find the use of annotations to specify Nullity constraints very useful. Even if you dont actually use findbugs, it makes the intent of the code much clearer. Those annotations have their place on Interfaces as much as Classes. You could argue that it is kind of programming by contract ...

    0 讨论(0)
提交回复
热议问题