Question

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

Maybe someone could give me an example?

Was it helpful?

Solution

I've used it in Spring to annotate interfaces where the annotation should apply to all subclasses. For example, say you have a Service interface and you might have multiple implementations of the interface but you want a security annotation to apply regardless of the annotation. In that case, it makes the most sense to annotate the interface.

OTHER TIPS

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 ...

Even without examples, it should be clear to explain - interfaces describe behaviour, and so can annotations, so it's a logical match to put them together.

A use case that I am working with is javax/hibernate bean validation, we are using interfaces to help us on avoiding to define validations on every specific class.

public interface IUser {
    @NotNull Long getUserId();
    ...
}

public class WebUser implements IUser {
    private Long userId;

    @Override
    public Long getUserId(){
        return userId;
    }
    ...
}

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.

I've seen a lot of research tools use method annotations to allow users to specify protocols, restrictions, etc. to facilitate automatic checking later.

Since annotations don't dictate what you can do with them, there is no good reason not to allow users to annotate interfaces.

You could use it for contract style programming - go one step further than just defining the interface (types and method names) and also define some semantics (contents of the types, preconditions, postconditions).

I'd have to check up on how annotations work in Java though, but this stuff could easily be done with Python annotations...

When deploying applications under JBoss you can use annotations on a interface to export a service in the jmx-console.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top