自定义一个注解+注解处理器
发布日期:2021-04-30 21:06:00
浏览次数:106
分类:精选文章
本文共 3687 字,大约阅读时间需要 12 分钟。
Student???????????
Student?????
???????Student????name?age????????????????????????????????Java???????
????????
- age???????@AgeBound??????????????ageMin?ageMax?????????????????
- name???????@NameBound????????????nameLimit???????????????
public class Student { @AgeBound(ageMin = 18, ageMax = 25) private int age; @NameBound(nameLimit = 5) private String name; private Student() { } public Student(int age, String name) { this.age = age; this.name = name; } @Override public String toString() { return "Student{" + "age=" + age + ", name='" + name + '\'' + '}'; }} ???????
?????????????@AgeBound?@NameBound???????age?name??????
@AgeBound??
@Target(ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@interface AgeBound { int ageMin(); int ageMax();} @NameBound??
@Target(ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@interface NameBound { int nameLimit();} ?????
????????????????????????StudentFactory????????????????????????????
???????
public class StudentFactory { private Class studentCls = Student.class; public Student getStudent(int age, String name) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException { // ???? judgeAge(age); // ?????? judgeName(name); // ??????Student?? Constructor declaredConstructor = studentCls.getDeclaredConstructor(int.class, String.class); declaredConstructor.setAccessible(true); Student student = (Student) declaredConstructor.newInstance(age, name); return student; } private void judgeName(String name) throws NoSuchFieldException { Field nameField = studentCls.getDeclaredField("name"); if (nameField.isAnnotationPresent(NameBound.class)) { NameBound nameBound = nameField.getAnnotation(NameBound.class); int nameLimit = nameBound.nameLimit(); if (name.length() > nameLimit) { throw new IllegalArgumentException("?????,????" + name.length()); } } } private void judgeAge(int age) throws NoSuchFieldException { Field ageField = studentCls.getDeclaredField("age"); if (ageField.isAnnotationPresent(AgeBound.class)) { AgeBound ageBound = ageField.getAnnotation(AgeBound.class); int ageMax = ageBound.ageMax(); int ageMin = ageBound.ageMin(); if (age < ageMin || age > ageMax) { throw new IllegalArgumentException("????? :" + age); } } }} ????
??????Test???????????
import java.lang.reflect.InvocationTargetException;public class Test { public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, NoSuchFieldException, InstantiationException, IllegalAccessException { // ???????? Student student = new StudentFactory().getStudent(20, "??"); System.out.println(student); // ????????? try { Student student1 = new StudentFactory().getStudent(100, "??3333"); System.out.println(student1); } catch (Exception e) { System.out.println("?????" + e.getMessage()); } }} ????
- ???????????Student??????
- ??????????????????? IllegalArgumentException???????????
????????????Student???????????????????????????????????
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2026年05月27日 12时28分19秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!