17

I know about basic annotations in Java like @Override etc.

Annotations are only metadata and they do not contain any business logic.

I am going through repeating annotations from Oracle documentation page to understand Java 8 new feature.

For example, you are writing code to use a "timer service that enables you to run a method at a given time or on a certain schedule, similar to the UNIX cron service". Now you want to set a timer to run a method, doPeriodicCleanup, on the last day of the month and on every Friday at 11:00 p.m. To set the timer to run, create an @Schedule annotation and apply it twice to the doPeriodicCleanup method.

@Schedule(dayOfMonth="last")
@Schedule(dayOfWeek="Fri", hour="23")
public void doPeriodicCleanup() { ... }

Declare a Repeatable Annotation Type

The annotation type must be marked with the @Repeatable meta-annotation. The following example defines a custom @Schedule repeatable annotation type:

import java.lang.annotation.Repeatable;

@Repeatable(Schedules.class)
public @interface Schedule {
    String dayOfMonth() default "first";
    String dayOfWeek() default "Mon";
    int hour() default 12;
}

Declare the Containing Annotation Type

The containing annotation type must have a value element with an array type. The component type of the array type must be the repeatable annotation type. The declaration for the Schedules containing annotation type is the following:

public @interface Schedules {
    Schedule[] value();
}

I did not understand use and usage of @Schedules annotation. How does it work for below method now? .

public void doPeriodicCleanup() { ... }

Thanks in advance.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • Why do you need a repeating annotation? Seems to me you could have all the attributes on a single instance and then figure out the intent from which attributes are present and which are omitted. Clearly there are some combinations of attributes that are invalid but you have that situation already. – Jim Garrison Mar 11 '16 at 23:11
  • 3
    Just I am trying to understand oracle documentation page. This example has been quoted from there. – Ravindra babu Mar 11 '16 at 23:12

1 Answers1

26

Before Java 8, a given annotation could only be set once on a method (or class, or field, etc.). So, if you wanted to be able to set two different schedules on a single method, you had to define a "wrapping" annotation such as Schedules, containing an array of Schedule annotions.

The developer thus had to do something like this:

@Schedules(value = {
    @Schedule(dayOfMonth="last"),
    @Schedule(dayOfWeek="Fri", hour="23")
})

This is not very readable, and the Schedules annotation doesn't have any purpose other than containing several Schedule annotations.

To reduce the boilerplate, but keep the annotations API identical, it's now allowed to simply annotate the method with

@Schedule(dayOfMonth="last"),
@Schedule(dayOfWeek="Fri", hour="23")

by declaring Schedule as repeatable and specifying its "wrapping" annotation. But it's just syntax sugar, that results in the same thing as the previous code: the method, at runtime, is seen as being annotated with a single Schedules annotation containing two Schedule. The compiler transforms the second piece of code into the first one.

Holger
  • 285,553
  • 42
  • 434
  • 765
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks for the info. I just have one small query. Now where should we place @Repeatable? – Ravindra babu Mar 11 '16 at 23:32
  • 1
    On the annotation that must be repeatable: Schedule. Just as the tutorial explains. – JB Nizet Mar 12 '16 at 06:32
  • oops. Silly mistake from my end . – Ravindra babu Mar 12 '16 at 13:07
  • It’s syntactic sugar plus some helper methods in the Reflections API to get such repeated annotations easier from a type or member. Since the repeated annotations are syntactic sugar for the container annotation, you can use these new methods to extract a container annotation as well (if the element annotation has the appropriate `@Repeatable` annotation)… – Holger Mar 14 '16 at 10:06