package com.brilliance.mda.runtime.mda.util;

import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public abstract class Reflects {

    public static <T> T fieldGet(Object obj, String name){
        try {
            Field field = obj.getClass().getDeclaredField(name);
            field.setAccessible(true);
            Object result = field.get(obj);
            return (T)result;
        } catch (NoSuchFieldException | IllegalAccessException e) {
            log.warn(String.format("get object %s[%s]属性值异常！",obj.toString(),name));
            return null;
        }
    }

    public static void fieldSet(Object obj, String name,Object val){
        try {
            Field field = obj.getClass().getDeclaredField(name);
            field.setAccessible(true);
            field.set(obj,val);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            log.warn(String.format("get object %s[%s]属性值异常！",obj.toString(),name));
        }
    }

    public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
        try {
            return clazz.getMethod(methodName, parameterTypes);
        } catch (Exception ignore) {

        }
        return null;
    }


}
