-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNoteComment.java
More file actions
49 lines (38 loc) · 1.13 KB
/
Copy pathNoteComment.java
File metadata and controls
49 lines (38 loc) · 1.13 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
import com.sun.tools.javac.code.Attribute;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
public class NoteComment {
public static void main(String[] args) throws Exception {
}
void check(Person person) throws Exception {
for (Field field: person.getClass().getFields()) {
Range range = field.getAnnotation(Range.class);
if (range != null) {
Object value = field.get(person);
if (value instanceof String) {
String s = (String) value;
if ( s.length() < range.min() || s.length() > range.max() ) {
throw new IllegalArgumentException("Invalid field: " + field.getName());
}
}
}
}
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Range {
int min() default 0;
int max() default 255;
}
class UseAnnon {
@Range(min = 1, max = 20)
public String name;
@Range(max = 10)
public String city;
}