More precise annotation targets

arjan tijms arjan.tijms at gmail.com
Thu Oct 11 13:40:39 UTC 2018


Hi Brian,

You're right. It can be done today that way.

Do you think though it would be an option to consider for a language
change, given that when being a language feature it would have much better
support from IDEs and other tools?

Kind regards,
Arjan Tijms





On Thu, Oct 11, 2018 at 2:27 PM Brian Goetz <brian.goetz at oracle.com> wrote:

> You can accomplish this today with an annotation processor. Make your
> own @TargetMethods meta-annotations, and then have an AP that enforces
> that the constraints of the meta-annotations are respected.  You don't
> need a language change.
>
>
>
>
>
> On 10/11/2018 9:18 AM, arjan tijms wrote:
> > Hi,
> >
> > I wonder if the following would make sense to consider for a Java
> language
> > enhancement:
> >
> > Annotations can currently be targeted
> > to TYPE, FIELD, METHOD, TYPE_PARAMETER etc.
> >
> > For lots of cases this is fine, but in other case you need a bit more
> > detail. For instance, consider an @ActionEvent annotation that targets
> > METHOD, but actually can only be applied on methods with a specific
> > signature such as void method(Action); and void method();
> >
> > You might want to have correct placement of the annotation expressed in a
> > little more exact way, like e.g.
> >
> > @Target(METHOD)
> > @TargetMethods( {ActionInterface.class, ActionInterface2.class})
> > @Retention(RUNTIME)
> > public @interface ActionEvent {}
> >
> >
> > With ActionInterface.class and ActionInterface2.class both functional
> > classes.
> >
> > Alternatively:
> >
> > @Target(METHOD)
> > @TargetMethods( {
> >      @MethodSignature(
> >          return=Void.class, arguments=Action.class),
> >       @MethodSignature(
> >          return=Void.class)}
> > }
> > @Retention(RUNTIME)
> > public @interface ActionEvent {}
> >
> > The syntax used is just illustrative of the idea.
> >
> > Thoughts?
> >
> > Kind regards,
> > Arjan
>
>


More information about the discuss mailing list