Custom annotations
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
define and read a runtime annotation.
Planned topics
- Declare with
@interface; elements look like methods. - Meta-annotations:
@Retention(RUNTIME)so reflection can see it;@Targetlimits where it applies. - Without RUNTIME retention the annotation is invisible at runtime.
- Read with
method.isAnnotationPresentorgetAnnotation. - Frameworks pair annotations with reflection or proxies.
Draft outline details
Demonstration
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)
@interface Audited { String action() default ""; }Check your understanding
What happens with @Retention(SOURCE)?
Show answer
The annotation is dropped before runtime, so reflection cannot see it.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.