forked from hacker85/JavaLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnnotationExample.java
More file actions
38 lines (34 loc) · 1.01 KB
/
AnnotationExample.java
File metadata and controls
38 lines (34 loc) · 1.01 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
package anotation;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
/**
* Created by max on 2/19/17.
*/
public class AnnotationExample {
public static void main(String[] args) throws IllegalAccessException {
Person person = new Person();
print(person, person.getClass());
}
private static void print(Object o, Class c) throws IllegalAccessException {
Field[] fields = c.getDeclaredFields();
for (Field field : fields) {
Annotation[] annotations = field.getDeclaredAnnotations();
for (Annotation annotation : annotations) {
if(annotation.annotationType().equals(Show.class)) {
System.out.println(field.get(o));
}
}
}
}
}
@Retention(RetentionPolicy.RUNTIME)
@interface Show {
boolean value() default true;
}
class Person {
@Show
String name = "Max";
int age = 18;
}