java泛型和反射机制实现的不同类型的bean组件复制(备忘参考)

package com.excellent.archimedes.util;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zyo
 * @Date 2022/10/10
 * @Description 表格渲染
 */
public class TableRenderUtil<E> {

    public static <T> TableView<T> getTableView( Class<T> recordClass, Map<String,String> columnInfo, List<T> dataList) {
        TableView<T> tableView= new TableView<T>();
        if (recordClass.getDeclaredFields().length>0) {
            for (Field field : recordClass.getDeclaredFields()) {
                TableColumn tableColumn = new TableColumn(columnInfo.get(field.getName()));
                tableColumn.setMinWidth(200l);
                tableColumn.setCellValueFactory(
                        new PropertyValueFactory<>(field.getName()));//必须和成员属性名称对上才能渲染出来
                tableView.getColumns().add(tableColumn);//tableView添加列头

            }

            // ObservableList<T> data = FXCollections.observableArrayList();

        }
        ObservableList<T> data = FXCollections.observableArrayList(dataList);
        tableView.setItems(data);
        return tableView;
    }

    public static <T, E> List<T> convertPojoToRecord(Class<T> clazzRecord, Class<E> pojoClass, List<E> pojoList) {
        List<Method> tRecordSetterMethods = new ArrayList<Method>();

        Map<String, Class[]> setterMethodsBinding = new HashMap<String, Class[]>();
        Map<String, Class[]> getterMethodsBinding = new HashMap<String, Class[]>();

        Method[] methods = clazzRecord.getMethods();
        for (Method method : methods) {
            if (method.getName().startsWith("set")) {
                setterMethodsBinding.put(method.getName().substring(3), method.getParameterTypes());
            }
            if (method.getName().startsWith("get")) {
                getterMethodsBinding.put(method.getName().substring(3), method.getParameterTypes());
            }
        }


        List<T> dataList = new ArrayList<T>();
        try {
            if (!CollectionUtils.isEmpty(pojoList)) {
                for (E pojo : pojoList) {
                    T record = clazzRecord.newInstance();
                    for (Map.Entry<String, Class[]> entry : setterMethodsBinding.entrySet()) {
                        String methodFeature = entry.getKey();
                        Class[] getter_paramsTypes = getterMethodsBinding.get(methodFeature);
                        Class[] setter_paramsTypes = entry.getValue();
                        Method getter = pojo.getClass().getMethod("get" + methodFeature, getter_paramsTypes);
                        Object getter_value = getter.invoke(pojo, new Object[]{});
                        Method setter = clazzRecord.getMethod("set" + methodFeature, setter_paramsTypes);
                        setter.invoke(record, new Object[]{getter_value});
                    }
                    dataList.add(record);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataList;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值