package com.jay.monitor.data.server.utils;

import com.jay.monitor.data.core.model.serializable.MQDataDTO;
import com.jay.monitor.data.core.model.serializable.SQLMonitorDTO;
import com.jay.monitor.data.core.model.serializable.URLDataDTO;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author liukaixiong
 * @Email liukx@elab-plus.com
 * @date 2021/9/17 - 10:55
 */
public class ClassFieldPrintCase {

    @Test
    public void printTest() {
        printClazz(MQDataDTO.class, true);
        printClazz(SQLMonitorDTO.class, false);
        printClazz(URLDataDTO.class, false);
    }

    private void printClazz(Class clazz, boolean isFindSuper) {

        if (isFindSuper && clazz.getSuperclass() != null) {
            printClazz(clazz.getSuperclass(), true);
        }

        Field[] declaredFields = clazz.getDeclaredFields();
        Map<String, String> fieldMap = new LinkedHashMap<>();
        for (int i = 0; i < declaredFields.length; i++) {
            Field declaredField = declaredFields[i];
            String name = declaredField.getName();
            Class<?> type = declaredField.getType();
            fieldMap.put(name, type.getSimpleName());
        }

        fieldMap.forEach((K, V) -> {
            System.out.println(clazz.getSimpleName() + "\t" + K + "\t" + V);
        });
    }

}
