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 {

    public static  TableView getTableView( Class recordClass, Map columnInfo, List dataList) {
        TableView tableView= new TableView();
        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 data = FXCollections.observableArrayList();

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

    public static  List convertPojoToRecord(Class clazzRecord, Class pojoClass, List pojoList) {
        List tRecordSetterMethods = new ArrayList();

        Map setterMethodsBinding = new HashMap();
        Map getterMethodsBinding = new HashMap();

        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 dataList = new ArrayList();
        try {
            if (!CollectionUtils.isEmpty(pojoList)) {
                for (E pojo : pojoList) {
                    T record = clazzRecord.newInstance();
                    for (Map.Entry 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;
    }

}

你可能感兴趣的:(java基础,java,servlet,mybatis)