-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnnotationDemo.java
More file actions
36 lines (30 loc) · 1.25 KB
/
AnnotationDemo.java
File metadata and controls
36 lines (30 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// custom annotation demo based on article from http://www.25hoursaday.com/CsharpVsJava.html#same
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Documented // annotation will show up in the javadoc
@Retention(RetentionPolicy.RUNTIME) // annotation metadata exposed at runtime
// special '@interface' annotation creation keyword
@interface AuthorInfo {
String author();
String email();
String version() default "1.0";
}
@AuthorInfo(author="Andy Mender", email="andymenderunix@gmail.com")
class DummyClass {
}
class AnnotationDemo {
public static void main(String[] args)
throws Exception {
// get Class object of DummyClass
Class c = Class.forName("DummyClass");
// get AuthorInfo metadata via the special annotation interface AuthorInfo
// (requires an explicit cast!)
AuthorInfo a = (AuthorInfo) c.getAnnotation(AuthorInfo.class);
// print the metadata (requires calls to AuthorInfo methods!)
System.out.println("Author information for " + c + ":");
System.out.println("Author: " + a.author());
System.out.println("Email: " + a.email());
System.out.println("Version: " + a.version());
}
}