java 反射获取方法并赋值,反射获取注解

    /**
     * 获取方法并赋值
     * @param obj
     */
    public void give(Object obj){
        try {
        Class clas = obj.getClass();
        Field [] fields = clas.getDeclaredFields();
        for (Field field:fields){
            PropertyDescriptor pd = new PropertyDescriptor(field.getName(),clas);
            Method method = pd.getWriteMethod();
            String fieldType = field.getType().getSimpleName();
            if(!method.getName().equals("setId")){
                if ("String".equals(fieldType)) {
                    method.invoke(obj, "zxczxczxczx");
                } else if ("Date".equals(fieldType)) {
                    method.invoke(obj, new Date());
                } else if ("Integer".equals(fieldType)
                        || "int".equals(fieldType)) {
                    Integer intval = Integer.parseInt("10");
                    method.invoke(obj, intval);
                } else if ("Long".equalsIgnoreCase(fieldType)) {
                    Long temp = Long.parseLong("11");
                    method.invoke(obj, temp);
                } else if ("Double".equalsIgnoreCase(fieldType)) {
                    Double temp = Double.parseDouble("12.12");
                    method.invoke(obj, temp);
                } else if ("Boolean".equalsIgnoreCase(fieldType)) {
                    Boolean temp = Boolean.parseBoolean("true");
                    method.invoke(obj, temp);
                } else if("BigDecimal".equalsIgnoreCase(fieldType)){
                    method.invoke(obj, new BigDecimal("22.00"));
                } else {
                    System.out.println("not supper type" + fieldType);
                }
            }
        }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
@Target({ElementType.FIELD,ElementType.METHOD})//定义注解的作用目标**作用范围字段、枚举的常量/方法
@Documented//说明该注解将被包含在javadoc中
public @interface ExcelExport {
    /**
     * 字段名称
     * @return
     */
    String name() default "";
    /**
     * 排序字段
     * @return
     */
    int order() default 0;
}
public class User {
    private Integer id;

    private String userName;

    private String password;

    @ExcelExport(name="gg")
    private String roleId;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @ExcelExport(name="用户姓名",order=1)
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @ExcelExport(name="用户密码",order=2)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRoleId() {
        return roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", roleId='" + roleId + '\'' +
                '}';
    }
}
  /**
     * 获取方法上的注解
     * @param obj
     */
    public void getAnnoMethod(Object obj){
        Class clas = obj.getClass();
        Method [] methods = clas.getDeclaredMethods();
        for (Method metho:methods){
            Annotation [] annotations = metho.getDeclaredAnnotations();
            if(annotations.length<0){
                return;
            }
            for (Annotation anno:annotations) {
                ExcelExport excelExport = (ExcelExport)anno;
                System.out.println(excelExport.name());
                System.out.println(excelExport.order());
            }
        }

    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值