This feature can extremely useful, for example build your how unit of tests or something else.
The interface is here you define or annotation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.annotation.*; | |
@Documented | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface Annotation { | |
String name(); | |
String description(); | |
} |
And in your class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Annotation(name="Something", description="This is a annotation") | |
public class Main { | |
public static void main(String[] args) throws ClassNotFoundException { | |
Class<?> klass = Class.forName("Main"); | |
Annotation annotation = (Annotation) klass.getAnnotation(Annotation.class); | |
System.out.println("Annotation " + klass); | |
System.out.println("name: " + annotation.name()); | |
System.out.println("Version: " + annotation.description()); | |
} | |
} |
Output
Annotation class Main
name: Something
Version: This is a annotation