forked from winterbe/java8-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnnotations1.java
More file actions
47 lines (35 loc) · 1.07 KB
/
Annotations1.java
File metadata and controls
47 lines (35 loc) · 1.07 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
37
38
39
40
41
42
43
44
45
46
47
package com.winterbe.java8.samples.misc;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Benjamin Winterberg
*/
public class Annotations1 {
@Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
@interface MyAnnotation {
}
@Retention(RetentionPolicy.RUNTIME)
@interface Hints {
Hint[] value();
}
@Repeatable(Hints.class)
@Retention(RetentionPolicy.RUNTIME)
@interface Hint {
String value();
}
@Hint("hint1")
@Hint("hint2")
class Person {
}
public static void main(String[] args) {
Hint hint = Person.class.getAnnotation(Hint.class);
System.out.println(hint); // null
Hints hints1 = Person.class.getAnnotation(Hints.class);
System.out.println(hints1.value().length); // 2
Hint[] hints2 = Person.class.getAnnotationsByType(Hint.class);
System.out.println(hints2.length); // 2
}
}