java运行时反射注解并赋值

注解的分类

  1. 只有一个属性的注解
    例如下面的Parameter注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER,ElementType.FIELD})

public @interface Parameter {
    String value() default "";
}

使用注解

public class Test {
    @Parameter("testName")
    private String username;
}

在运行时进行反射的类编写

//target表示运行的类
public static void initParamemterAnnotation(Object target){
	 Class clz = target.getClass();
     Field[] fieldsList = clz.getFields();
     for(Field field:fieldsList){
     if (field.getAnnotation(Parameter.class) != null) {
                Parameter parameter = field.getAnnotation(Parameter.class);
                field.setAccessible(true);
                }
      }	
       try {
                v = parameter.value();
                field.set(target, v);
       } catch (IllegalAccessException e) {
                    throw new RuntimeException(
                        

}

2.有多个属性的注解(一个对象的注解)
例如下面的Account注解

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER,ElementType.FIELD})
public @interface Account {
    String account() default "";
    String password() default "";
    String userName() default "";
    String uid() default "";
}

需要有一个注解对应的类AccountInfoDo

public class AccountInfoDo {
    public String account;
    public String userName;
    public String password;
    public String uid;

    @Override
    public String toString() {
        return "AccountInfoDo{" +
                "account='" + account + '\'' +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", uid='" + uid + '\'' +
                '}';
 	   }
}

使用注解

public class Test {
    @Account(account = "AC_54833236_12", userName = "霍雅", password = "123456a!", uid = "144112762")
    public static AccountInfoDo MainAccount = new AccountInfoDo();
    }
    

在运行时进行反射的类编写

 public static void initAccountAnnotation(Object target){

        Class clz = target.getClass();
        //Field[] fields = clz.getFields();
        //获取运行的类和父类中属性 都检查是否有Account注解 最后写这个方法 
        List<Field> fields = getAllFields(clz);
        for (Field field : fields) {
            if(field.getAnnotation(Account.class) != null) {
            	 Account account = field.getAnnotation(Account.class);
                 field.setAccessible(true);
                 List<Field> accountFields = getAllFields(AccountInfoDo.class);
                for (Field property : accountFields) {
                 value = Acconut.class.getMethod(property.getName()).invoke(account);
                 }
                 try{
                  property.set(field.get(target), value);
                 }catche(Exception ex) {
                        System.out.println("initAccountAnnotation failed!");
                    }
            }
            
            

3.getAllFields方法

 private static List<Field> getAllFields(Class<?> clazz) {
        // 获取父类中的参数
        List<Field> allFields = new ArrayList<>();
        Class<?> classType = clazz;
        while (!Object.class.equals(classType) && classType != null) {
            Field[] fields = classType.getDeclaredFields();
            for (Field f : fields) {
                allFields.add(f);
            }
            classType = classType.getSuperclass();
        }
        return allFields;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值