Java 8 repeatable annotation: will it change JavaEE 8?


Not sure you noticed it since some changes are more important in java 8 but you can now repeat annotations. What does it mean?

Basically if we suppose you have this annotation:

// retention/target you want
public  @interface Foo {
    String value();
}

you can now define “as usual” a plural annotation:

// no more retention/target than Foo
public @interface Foos {
    Foo[] value();
}

Then use it this way

// of course it works everywhere and not only types 😉
@Foo("1")
@Foo("2")
public class MyService {
    // ...
}

Actually java 8 will generate the following bytecode:

// of course it works everywhere and not only types 😉
@Foos({
  @Foo("1"),
  @Foo("2")
})
public class MyService {
    // ...
}

Clever no? Runtime doesn’t need to check if the annotations are @Repeatable or not :).

So how can it affect JavaEE? I’m pretty sure you thought about two specifications at least (but a lot more can be concerned):

  • JPA: @NamedQueries can now be implicit, even in JavaEE 5, 6, 7 containers (supposing it handles java 8 bytecode) if you use a @NamedQuery version annotated with @Repeatable(NamedQueries.class)
  • Bean Validation: here the convention was to use @Annotation.List but decorating the parent with the list would make it working too. And this last one is already usable in all containers (or in JavaSE 8) for custom constraints!

This change is clearly not the most important but clearly shows that Java wants to get rid of all the boilerplate it can keeping the language accessible for users and consistent with JavaEE.

Another change which is interesting is the default method for interfaces but we still don’t know how it will be integrated to EJBs and CDI…that said OpenEJB and TomEE already support interface as EJB so something should be doable ;).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s