← Back to Advanced Java Course | Chapter 11: Advanced & Security | Lesson 2 of 19

Java के Advanced Annotations

Annotations code पर चिपकाए गए labels हैं, और advanced वाले आपको नियमों के साथ अपने खुद के labels बनाने देते हैं। Tools खास काम करने के लिए इन्हें पढ़ सकते हैं।
Syntax
java
@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

java
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");
	}
}

Retention Policy levels

@Retention policy नियंत्रित करती है कि किसी annotation का metadata कितनी देर बना रहता है: SOURCE annotations compiler द्वारा हटा दिए जाते हैं और .class file में कभी नहीं पहुंचते, जबकि RUNTIME annotations संरक्षित रहते हैं ताकि program चलते समय reflection के ज़रिए पढ़े जा सकें।

उदाहरण: Retention Policy levels

java
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());
	}
}

Targeting Annotation Elements

@Target annotation सीमित करता है कि किसी custom annotation को कहां रखा जा सकता है -- उदाहरण के लिए, इसे सिर्फ fields तक, या सिर्फ methods तक सीमित करना -- ताकि compiler गलत उपयोग (जैसे किसी field-only annotation को किसी class पर लगाना) को चुपचाप अनुमति देने के बजाय पकड़ सके।

उदाहरण: Targeting Annotation Elements

java
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");
	}
}

Annotation Inheritance

Annotations fields और methods की तरह subclasses को अपने आप inherit नहीं होते, जब तक annotation की अपनी declaration खुद @Inherited चिह्नित न हो। यह कई developers को हैरान करता है जो मान लेते हैं कि annotation का behavior साधारण Java inheritance जैसा ही है।

उदाहरण: Annotation Inheritance

java
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
	}
}

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

java
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
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. @Retention(RetentionPolicy.RUNTIME) भूल जाना, जिससे reflection runtime पर annotation नहीं देख सकता क्योंकि default CLASS है।
  2. किसी annotation को वहां रखना जहां उसका @Target अनुमति नहीं देता, जैसे किसी method-only annotation को किसी class पर, जो एक compile error है।
  3. यह उम्मीद करना कि कोई subclass एक annotation inherit करेगी, जबकि यह सिर्फ @Inherited से चिह्नित class annotations के लिए होता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.