Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Runtime annotations

โญโญโญ advancedโณ 15 min read๐Ÿ“ Lesson 146 of 180

Retention policies, declaring custom annotations, and reading them reflectively โ€” the framework reader pattern.

Annotations are typed metadata attached to program elements. Their power depends entirely on retention:

  • SOURCE โ€” compiler-only (like @Override); gone in the class file.
  • CLASS โ€” in the class file, invisible at runtime (the default, rarely used).
  • RUNTIME โ€” readable via reflection; this is what frameworks use.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Route { String value(); }

Reading them back is the whole trick behind every routing table, validator, and injection engine:

for (Method m : obj.getClass().getDeclaredMethods()) {
    Route r = m.getAnnotation(Route.class);
    if (r != null) table.put(r.value(), m);
}

An annotation with no reader is a comment. The reader is the framework โ€” which means you can write one.