Java @Repeatable

www‮al.‬utturi.com
Java @Repeatable

In Java, the @Repeatable annotation is used to indicate that an annotation type can be applied more than once to a single element. Prior to Java 8, it was not possible to apply the same annotation multiple times to a single element. With the introduction of the @Repeatable annotation in Java 8, it is now possible to do this.

Here is an example of how the @Repeatable annotation can be used:

import java.lang.annotation.Repeatable;

@Repeatable(Values.class)
public @interface Value {
  int val();
}

public @interface Values {
  Value[] value();
}

public class MyClass {
  @Value(val = 1)
  @Value(val = 2)
  public void doSomething() {
    // code goes here
  }
}

In this example, the @Value annotation can be applied multiple times to the doSomething method because it is marked as @Repeatable. The @Values annotation is used to specify the container for the repeated annotations.

Created Time:2017-11-01 22:29:45  Author:lautturi