lundi 13 mai 2019

Require all fields of a given type to be annotated by a certain field annotation

I have a nice field annotation

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Foo {
    // lots of useful, mandatory values here
}

and I would like to create a type annotation

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE) // <- not sure
public @interface Bar {
    ...
}

such that any types annotated @Bar will throw compiler warnings if they contain fields that are not annotated with @Foo.

In other words, I would like Bar to enforce a contract in which all of the @Bar-annotated class' fields must have a @Foo annotation, otherwise I will get a nice warning from my compiler.

Is this possible?

Thanks!





Aucun commentaire:

Enregistrer un commentaire