Java - Access Repeatable Annotations at Runtime

Description

Access Repeatable Annotations at Runtime

Demo

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PACKAGE })
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(EventLogs.class)
@interface EventLog {
  String date();/*from  w w  w  .  jav  a 2  s. c o  m*/

  String comments();
}

@Target({ ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PACKAGE })
@Retention(RetentionPolicy.RUNTIME)
@interface EventLogs {
  EventLog[] value();
}

@EventLog(date = "02/01/2018", comments = "A")
@EventLog(date = "02/22/2018", comments = "B")
public class Main {
  public static void main(String[] args) {
    Class<Main> mainClass = Main.class;
    Class<EventLog> annClass = EventLog.class;

    // Access annotations using the EventLog type
    System.out.println("Using the EventLog type...");
    EventLog[] annList = mainClass.getAnnotationsByType(EventLog.class);
    for (EventLog log : annList) {
      System.out.println("Date=" + log.date() + ", Comments=" + log.comments());
    }

    // Access annotations using the EventLogs containing annotation type
    System.out.println("\nUsing the EventLogs type...");
    Class<EventLogs> containingAnnClass = EventLogs.class;
    EventLogs logs = mainClass.getAnnotation(containingAnnClass);
    for (EventLog log : logs.value()) {
      System.out.println("Date=" + log.date() + ", Comments=" + log.comments());
    }
  }
}

Result

Evolving Annotation Types

If you add a new element to an annotation type, you need supply its default value.

All existing instances of the annotation will use the default value for the new elements.

If you add a new element to an existing annotation type without specifying a default value for the element, the code that uses the annotation will break.