Java के Advanced Annotations
In this page:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface AnnotationName {
String element() default "value";
}
Creating Custom Annotations
Custom annotations आपको अपने code पर structural metadata tags जोड़ने देते हैं जिन्हें tools, compilers, या runtime frameworks बाद में पढ़ और उन पर काम कर सकते हैं, बिना उस metadata के खुद annotated code के असली runtime behavior को बदले।
उदाहरण: Creating Custom Annotations
import java.lang.annotation.*;
public class Main {
@interface Info { String value(); } // structural metadata, no runtime behavior on its own
@Info("demo")
static class Sample {}
public static void main(String[] args) {
System.out.println("Custom annotation @Info declared");
}
}
Login to try C/C++/Java/PHP code in the editor
Retention Policy levels
@Retention policy नियंत्रित करती है कि किसी annotation का metadata कितनी देर बना रहता है: SOURCE annotations compiler द्वारा हटा दिए जाते हैं और .class file में कभी नहीं पहुंचते, जबकि RUNTIME annotations संरक्षित रहते हैं ताकि program चलते समय reflection के ज़रिए पढ़े जा सकें।
उदाहरण: Retention Policy levels
import java.lang.annotation.*;
public class Main {
@Retention(RetentionPolicy.RUNTIME) // preserved so reflection can read it while running
@interface Tag { String value(); }
@Tag("example")
static class Sample {}
public static void main(String[] args) {
Tag tag = Sample.class.getAnnotation(Tag.class);
System.out.println(tag.value());
}
}
Login to try C/C++/Java/PHP code in the editor
Targeting Annotation Elements
@Target annotation सीमित करता है कि किसी custom annotation को कहां रखा जा सकता है -- उदाहरण के लिए, इसे सिर्फ fields तक, या सिर्फ methods तक सीमित करना -- ताकि compiler गलत उपयोग (जैसे किसी field-only annotation को किसी class पर लगाना) को चुपचाप अनुमति देने के बजाय पकड़ सके।
उदाहरण: Targeting Annotation Elements
import java.lang.annotation.*;
public class Main {
@Target(ElementType.FIELD) // restricted to fields only
@Retention(RetentionPolicy.RUNTIME)
@interface Required {}
static class Sample {
@Required
String name;
}
public static void main(String[] args) {
System.out.println("@Required can only be placed on fields");
}
}
Login to try C/C++/Java/PHP code in the editor
Annotation Inheritance
Annotations fields और methods की तरह subclasses को अपने आप inherit नहीं होते, जब तक annotation की अपनी declaration खुद @Inherited चिह्नित न हो। यह कई developers को हैरान करता है जो मान लेते हैं कि annotation का behavior साधारण Java inheritance जैसा ही है।
उदाहरण: Annotation Inheritance
import java.lang.annotation.*;
public class Main {
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@interface Marker {}
@Marker
static class Parent {}
static class Child extends Parent {}
public static void main(String[] args) {
System.out.println(Child.class.isAnnotationPresent(Marker.class)); // true, because @Inherited was used
}
}
Login to try C/C++/Java/PHP code in the editor
Annotation Processing
Runtime पर, annotation processors annotated elements को scan करने और उन पर काम करने के लिए reflection का उपयोग करते हैं -- object state validate करना, fields को database columns से map करना, या routes register करना -- जो वह mechanism है जो JPA और Spring जैसे frameworks को आपके annotations को असली behavior में translate करने देता है।
उदाहरण: Annotation Processing
import java.lang.annotation.*;
import java.lang.reflect.*;
public class Main {
@Retention(RetentionPolicy.RUNTIME)
@interface Column { String name(); }
static class User {
@Column(name = "user_name")
String name;
}
public static void main(String[] args) throws Exception {
Field field = User.class.getDeclaredField("name");
Column column = field.getAnnotation(Column.class);
System.out.println("Maps to DB column: " + column.name()); // annotation processor scanning at runtime
}
}
Login to try C/C++/Java/PHP code in the editor
@Retention(RetentionPolicy.RUNTIME)भूल जाना, जिससे reflection runtime पर annotation नहीं देख सकता क्योंकि defaultCLASSहै।- किसी annotation को वहां रखना जहां उसका
@Targetअनुमति नहीं देता, जैसे किसी method-only annotation को किसी class पर, जो एक compile error है। - यह उम्मीद करना कि कोई subclass एक annotation inherit करेगी, जबकि यह सिर्फ
@Inheritedसे चिह्नित class annotations के लिए होता है।
Chapter Quiz — Complete all 19 topics to unlock
0/19 topics done
Complete these topics first:
- Java Reflection API
- Java Annotations Advanced
- Java Garbage Collection
- Java Memory Management
- Java Performance Optimization
- Java Advanced Interview Questions
- Java CompletableFuture
- Java Atomic Classes
- Java Locks & Semaphores
- Java Concurrent Collections
- Java Cryptography Basics
- Java Hashing (MD5, SHA)
- Java SSL & HTTPS
- Java Logging (Log4j/SLF4J)
- Java Serialization Advanced
- Java Interview Questions Advanced
- Java Connection Pooling
- Java Test Driven Development
- Java Integration Testing