java / intermediate
Snippet
Custom Annotations for Metadata
Custom annotations allow you to add metadata to your code that can be processed at runtime via Reflection. This is often used by frameworks for cross-cutting concerns like logging or validation.
snippet.java
1
2
3
4
5
@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public @interface LogExecutionTime {String value() default "Generic";}
Breakdown
1
@Retention(RetentionPolicy.RUNTIME)
Specifies that the annotation should be available during execution for inspection via reflection.
2
@Target(ElementType.METHOD)
Restricts where the annotation can be used; in this case, only on methods.