-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain4.java
More file actions
37 lines (33 loc) · 946 Bytes
/
Copy pathMain4.java
File metadata and controls
37 lines (33 loc) · 946 Bytes
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
package aReflection;
import java.lang.reflect.Field;
public class Main4 {
public static void main(String[] args) throws Exception{
Person p1 = new Person("xiaoming", 25);
Person p2 = new Person(null, 15);
checkPerson(p1);
checkPerson(p2);
}
static void checkPerson(Person p)throws Exception{
System.out.println("check " + p + "...");
Class cls = Person.class;
Field[] fs = cls.getDeclaredFields();
for(Field f : fs){
checkField(f, p);
}
}
static void checkField(Field f, Person p)throws Exception{
if (f.isAnnotationPresent(NotNull.class)){
Object r = f.get(p);
if (r == null){
System.out.println("Error: field " + f.getName() + " is null.");
}
}
if (f.isAnnotationPresent(Range.class)){
Range range = f.getAnnotation(Range.class);
int n = (Integer) f.get(p);
if (n<range.min() || n > range.max()){
System.out.println("Error: field" + f.getName() +" is out of range.");
}
}
}
}