自定义注解使用,和反射的使用

4 篇文章 0 订阅
3 篇文章 0 订阅

自定义注解使用,和反射的使用

一,自定义注解

此自定义注解用于导出表格
声明一个注解:此处的注解适用与实体类的属性上

package com.es.util;

import java.lang.annotation.*;

/**
 * <p>
 *
 * </p>
 *
 * @Author: fxp
 * @Date: 2021/11/11 16:56
 */
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TableAnnotation {
    /**
     * 表头名称
     * @return
     */
    String value();

    /**
     * 父级表头名
     * @return
     */
    String pName() default "";

    /**
     * 键
     * @return
     */
    String key() default "";

    /**
     * 表头级数
     * @return
     */
    int level() default 0;

    /**
     * 父级下标,该字段没用,还没想好
     * @return
     */
    int pNum() default -1;

    /**
     * 是否动态
     * @return
     */
    boolean dynamic() default false;

    /**
     * 0 表头   1 值
     * @return
     */
    int headerOrData() default 0;

    /**
     * 是否为数字,默认否
     * @return
     */
    boolean isNum() default false;

    /**
     * 样式,该功能还没开发,后续开发
     * @return
     */
    String style() default "";

    /**
     * 是否展示,默认展示
     * @return
     */
    boolean isDisplay() default true;

    /**
     * 列宽
     * @return
     */
    int width() default 3766;

    /**
     * 是否有子集
     * @return
     */
    boolean haveC() default false;

}

反射

Field[] declaredFields = cl.getDeclaredFields();------------>该类的获取所有属性,包括私有属性

TableAnnotation annotation = declaredField.getAnnotation(TableAnnotation.class);--------------->获取该属性上的TableAnnotation注解类,TableAnnotation为我们上面声明的注解

annotation.value();---------------------->获取注解上的value值

CustomTable 类,在动态多级表头导出中提到

public static CustomTable getCustomTable(List<?> list,String tableName,Integer numType) throws IllegalAccessException {
        CustomTable customTable = new CustomTable();
        customTable.setTableName(tableName);
        List<TableHeader> headers = new ArrayList<>();
        List<TableData> dataList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            Class<?> cl = list.get(i).getClass();
            Field[] declaredFields = cl.getDeclaredFields();
            TableData tableData = new TableData();
            Map<String,Object> map = new HashMap<>(10);
            for (Field declaredField : declaredFields) {
                TableAnnotation annotation = declaredField.getAnnotation(TableAnnotation.class);
                if (null != annotation){
                    System.out.println("value() = " +annotation.value());
                    if (i == 0){
                        TableHeader tableHeader = new TableHeader();
                        tableHeader.setKey(annotation.value());
                        tableHeader.setHeaderName(annotation.value());
                        tableHeader.setType(annotation.isNum()?1:0);
                        headers.add(tableHeader);
                    }
                    //取消私有方法限制
                    declaredField.setAccessible(true);
                    String name = declaredField.getName();
                    Object o = declaredField.get(list.get(i));
                    map.put(annotation.value(),o);
                }
            }
            tableData.setData(map);
            dataList.add(tableData);
        }
        customTable.setHeaders(headers);
        customTable.setDataList(dataList);
        customTable.setNumType(numType);
        return customTable;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值