package com.secspace.lib.common.utils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class ReflectUtil {
    private static final String TAG = "ReflectUtil";

    public static void obtain(String className) {
        try {
            Class c = Class.forName(className);

            reflectDeclaredFields(c);
            DebugLog.d(TAG, "------------------------\n");
            reflectDeclaredConstructors(c);
            DebugLog.d(TAG, "------------------------\n");
            reflectDeclaredMethods(c);

            reflectFields(c);
            DebugLog.d(TAG, "------------------------\n");
            reflectConstructors(c);
            DebugLog.d(TAG, "------------------------\n");
            reflectMethods(c);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 反射所有变量
     *
     * @param c
     */
    public static void reflectDeclaredFields(Class c) {
            Field[] fields = c.getDeclaredFields();
            DebugLog.i(TAG, c.getName() + ":this class has " + fields.length + " field");
            for (Field field : fields) {
                DebugLog.i(TAG, c.getName() + ":  " + Modifier.toString(field.getModifiers()) + " " + field.getType().getCanonicalName() + " "
                        + field.getName());
            }

    }


    public static void reflectFields(Class c) {
            Field[] fields = c.getFields();
            DebugLog.i(TAG, c.getName() + ":  " + "this class has " + fields.length + " field");
            for (Field field : fields) {
                DebugLog.i(TAG, Modifier.toString(field.getModifiers()) + " " + field.getType().getCanonicalName() + " "
                        + field.getName());
            }

    }

    /**
     * 反射所有构造方法
     *
     * @param c
     */
    public static void reflectDeclaredConstructors(Class c) {
        try {
            Constructor[] constructors = c.getDeclaredConstructors();
            DebugLog.i(TAG, c.getName() + ":  " + "this class has " + constructors.length + " constructor");
            for (Constructor constructor : constructors) {
                StringBuilder sb = new StringBuilder(
                        Modifier.toString(constructor.getModifiers()) + " " + constructor.getName() + "(");
                Class[] types = constructor.getParameterTypes();
                if (types.length > 0) {
                    for (Class type : types) {
                        sb.append(type.getSimpleName() + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                }
                sb.append(")");
                DebugLog.i(TAG, c.getName() + ":  " + sb.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void reflectConstructors(Class c) {
        try {
            Constructor[] constructors = c.getConstructors();
            DebugLog.i(TAG, c.getName() + ":  " + "this class has " + constructors.length + " constructor");
            for (Constructor constructor : constructors) {
                StringBuilder sb = new StringBuilder(
                        Modifier.toString(constructor.getModifiers()) + " " + constructor.getName() + "(");
                Class[] types = constructor.getParameterTypes();
                if (types.length > 0) {
                    for (Class type : types) {
                        sb.append(type.getSimpleName() + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                }
                sb.append(")");
                DebugLog.i(TAG, c.getName() + ":  " + sb.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 反射所有成员方法
     *
     * @param c
     */
    public static void reflectDeclaredMethods(Class c) {
        try {
            Method[] methods = c.getDeclaredMethods();
            DebugLog.i(TAG, c.getName() + ":  " + "this class has " + methods.length + " method");
            for (Method method : methods) {
                StringBuilder sb = new StringBuilder(Modifier.toString(method.getModifiers()) + " "
                        + method.getReturnType().getSimpleName() + " " + method.getName() + "(");
                Class[] types = method.getParameterTypes();
                if (types.length > 0) {
                    for (Class type : types) {
                        sb.append(type.getSimpleName() + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                }
                sb.append(")");
                DebugLog.i(TAG, c.getName() + ":  " + sb.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void reflectMethods(Class c) {
        try {
            Method[] methods = c.getDeclaredMethods();
            DebugLog.i(TAG, c.getName() + ":  " + "this class has " + methods.length + " method");
            for (Method method : methods) {
                StringBuilder sb = new StringBuilder(Modifier.toString(method.getModifiers()) + " "
                        + method.getReturnType().getSimpleName() + " " + method.getName() + "(");
                Class[] types = method.getParameterTypes();
                if (types.length > 0) {
                    for (Class type : types) {
                        sb.append(type.getSimpleName() + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                }
                sb.append(")");
                DebugLog.i(TAG, c.getName() + ":  " + sb.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

