Skip to content

Commit 1b4972d

Browse files
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

File tree

1 file changed

+52
-11
lines changed

1 file changed

+52
-11
lines changed

Section21AnnotationsandJavaDoc/src/InBuiltAnnotations.java

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
1-
/*
2-
InBuilt Annonations:
3-
1.Retention.
4-
2.Documented
5-
3.Target
6-
4.Inherited
7-
5.Repeatable.
8-
*/
9-
10-
//After One annotaions to another annotaions.
11-
//Defining Own annontation That affect the another annontaions.
121
import java.lang.annotation.*;
132

143
@Retention(RetentionPolicy.RUNTIME) // Change from CLASS to RUNTIME
154
@Documented
165
@Target(ElementType.TYPE)
176
@Inherited
7+
188
@interface MyAnno1 {
199
String name();
2010
String project();
@@ -38,3 +28,54 @@ public static void main(String[] args) {
3828
}
3929
}
4030
}
31+
32+
/*
33+
InBuilt Annotations:
34+
1.Retention.
35+
2.Documented
36+
3.Target
37+
4.Inherited
38+
5.Repeatable.
39+
40+
After One annotations to another annotations. Defining Own annotation That affect the another annotations.
41+
42+
1. @interface
43+
- Java me custom annotation banane ke liye `@interface` use karte hain.
44+
- Annotation ek metadata hai jo class/method/field ke upar extra info provide karta hai.
45+
46+
2. Built-in meta-annotations:
47+
- @Retention → batata hai annotation runtime pe available rahega ya sirf compile-time tak.
48+
* RetentionPolicy.CLASS (default): compile hone ke baad bhi .class file me hota hai, lekin runtime pe accessible nahi.
49+
* RetentionPolicy.RUNTIME: runtime pe reflection ke zariye access kar sakte ho. (yahan use kiya gaya hai)
50+
- @Target → annotation kahan lag sakta hai (class, method, field, etc.)
51+
- @Inherited → agar ek class pe annotation hai, to uske subclass ko bhi inherit ho jata hai.
52+
- @Documented → annotation javadoc me include hoga.
53+
54+
3. Annotation definition (MyAnno1)
55+
- Tumne 4 elements define kiye:
56+
* name (required)
57+
* project (required)
58+
* date (required)
59+
* version (optional, default = "13")
60+
61+
Example usage:
62+
@MyAnno1(name="Tim", project="Bank", date="2025-02-13")
63+
64+
4. Accessing annotation at runtime
65+
- Reflection ke through `.getAnnotation(MyAnno1.class)` se class pe lage annotation ki values fetch ki ja sakti hain.
66+
- Example output:
67+
Name: Tim
68+
Project: Bank
69+
Date: 2025-02-13
70+
Version: 13
71+
72+
5. Why annotations are useful?
73+
- Metadata for frameworks (Spring, Hibernate).
74+
- Code generation, validations, dependency injection.
75+
- Custom runtime processing (logging, auditing, etc.)
76+
77+
1. Define annotation → `@interface MyAnno1`
78+
2. Configure meta-annotations → `@Retention(RUNTIME)`, `@Target(TYPE)`
79+
3. Apply annotation → `@MyAnno1(...)` on class
80+
4. Use Reflection → `class.getAnnotation(...)` to read values
81+
*/

0 commit comments

Comments
 (0)