Commit 1b4972d
committed
feat: implement custom annotation with runtime retention and reflection access
WHAT was added:
- Defined a custom annotation `@MyAnno1` with attributes:
• name (required)
• project (required)
• date (required)
• version (optional, default = "13")
- Applied meta-annotations:
• @retention(RetentionPolicy.RUNTIME) → Annotation info available at runtime.
• @target(ElementType.TYPE) → Can only be applied to classes/interfaces.
• @documented → Included in Javadoc.
• @inherited → Passed down to subclasses if not explicitly overridden.
- Annotated `InBuiltAnnotations` class with `@MyAnno1`.
- Used Java Reflection API (`getAnnotation()`) in `main()` to retrieve and print annotation values.
KEY LEARNINGS:
1. Retention Policies:
- SOURCE: Annotation discarded by compiler.
- CLASS: Retained in bytecode but not available at runtime.
- RUNTIME: Retained in bytecode and accessible via reflection.
2. Annotation Attributes:
- Work like method declarations inside an annotation.
- Can have default values (e.g., `version = "13"`).
- Mandatory attributes must be provided when applying the annotation.
3. Reflection Usage:
- `Class.getAnnotation(MyAnno1.class)` fetches annotation details at runtime.
- Null check ensures safe access.
REAL-WORLD APPLICATIONS:
- ✅ Frameworks (Spring, Hibernate) → Use runtime annotations for dependency injection, ORM mappings.
- ✅ API Documentation → With `@Documented`, annotations appear in generated Javadoc.
- ✅ Inheritance-based Config → `@Inherited` lets subclasses automatically reuse parent annotations.
- ✅ Build Tools & Testing → Reflection-driven tools can read annotations dynamically for code generation or validation.
RULE OF THUMB:
- Use `RUNTIME` retention if you need to process annotations during execution (e.g., frameworks, libraries).
- Keep attributes descriptive but concise.
- Leverage default values for optional attributes to reduce boilerplate.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent d969131 commit 1b4972d
1 file changed
+52
-11
lines changedLines changed: 52 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | 1 | | |
13 | 2 | | |
14 | 3 | | |
15 | 4 | | |
16 | 5 | | |
17 | 6 | | |
| 7 | + | |
18 | 8 | | |
19 | 9 | | |
20 | 10 | | |
| |||
38 | 28 | | |
39 | 29 | | |
40 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
0 commit comments