Runtime annotations
advanced15 min readLesson 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.