/**
 * Copyright (c) 2015-2021, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jboot.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;

/**
 * 反射相关操作的工具类
 */
public class ReflectUtil {

    public static <T> T getStaticFieldValue(Class<?> dClass, String fieldName) {
        return getFieldValue(dClass, fieldName, null);
    }

    public static <T> T getFieldValue(Class<?> dClass, String fieldName, Object from) {
        try {
            if (StrUtil.isBlank(fieldName)) {
                throw new IllegalArgumentException("fieldName must not be null or empty.");
            }
            Field field = searchField(dClass, f -> f.getName().equals(fieldName));
            if (field == null) {
                throw new NoSuchFieldException(fieldName);
            }
            field.setAccessible(true);
            return (T) field.get(from);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }


    public static Field searchField(Class<?> dClass, Predicate<Field> filter) {
        if (dClass == null) {
            return null;
        }
        Field[] fields = dClass.getDeclaredFields();
        for (Field field : fields) {
            if (filter.test(field)) {
                return field;
            }
        }
        return searchField(dClass.getSuperclass(), filter);
    }


    public static List<Field> searchFieldList(Class<?> dClass, Predicate<Field> filter) {
        List<Field> fields = new ArrayList<>();
        doSearchFieldList(fields, dClass, filter);
        return fields;
    }


    private static void doSearchFieldList(List<Field> searchToList, Class<?> dClass, Predicate<Field> filter) {
        if (dClass == null || dClass == Object.class) {
            return;
        }

        Field[] fields = dClass.getDeclaredFields();
        if (fields.length > 0) {
            if (filter != null) {
                for (Field field : fields) {
                    if (filter.test(field)) {
                        searchToList.add(field);
                    }
                }
            } else {
                searchToList.addAll(Arrays.asList(fields));
            }
        }

        doSearchFieldList(searchToList, dClass.getSuperclass(), filter);
    }


    public static Method searchMethod(Class<?> dClass, Predicate<Method> filter) {
        if (dClass == null) {
            return null;
        }
        Method[] methods = dClass.getDeclaredMethods();
        for (Method method : methods) {
            if (filter.test(method)) {
                return method;
            }
        }
        return searchMethod(dClass.getSuperclass(), filter);
    }


    public static List<Method> searchMethodList(Class<?> dClass, Predicate<Method> filter) {
        List<Method> methods = new LinkedList<>();
        doSearchMethodList(dClass, filter, methods);
        return methods;
    }

    private static void doSearchMethodList(Class<?> dClass, Predicate<Method> filter, List<Method> list) {
        if (dClass == null) {
            return;
        }
        Method[] methods = dClass.getDeclaredMethods();
        if (methods.length > 0) {
            if (filter != null) {
                for (Method method : methods) {
                    if (filter.test(method)) {
                        list.add(method);
                    }
                }
            } else {
                list.addAll(Arrays.asList(methods));
            }
        }

        doSearchMethodList(dClass.getSuperclass(), filter, list);
    }
}

@@ -728,36 +728,8 @@
 od;%0A
-import java.util.ArrayList;%0A
 impo
@@ -2201,21 +2201,22 @@
 s = new 
-Array
+Linked
 List%3C%3E()
@@ -2247,16 +2247,8 @@
 ist(
-fields, 
 dCla
@@ -2253,24 +2253,32 @@
 lass, filter
+, fields
 );%0A        r
@@ -2345,34 +2345,8 @@
 ist(
-List%3CField%3E searchToList, 
 Clas
@@ -2373,32 +2373,58 @@
 te%3CField%3E filter
+, List%3CField%3E searchToList
 ) %7B%0A        if (
@@ -2934,22 +2934,8 @@
 ist(
-searchToList, 
 dCla
@@ -2952,32 +2952,46 @@
 rclass(), filter
+, searchToList
 );%0A    %7D%0A%0A%0A    p
@@ -3616,24 +3616,25 @@
 ods;%0A    %7D%0A%0A
+%0A
     private 
@@ -3720,17 +3720,25 @@
 Method%3E 
-l
+searchToL
 ist) %7B%0A 
@@ -4035,25 +4035,33 @@
             
-l
+searchToL
 ist.add(meth
@@ -4142,17 +4142,25 @@
         
-l
+searchToL
 ist.addA
@@ -4271,17 +4271,25 @@
 filter, 
-l
+searchToL
 ist);%0A  

添加 ReflectUtil.searchFieldList() 方法