Java反射讲解-实例(2)

package me.explain.work.base;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pro on 16-1-18.
 */
public class ClassBase {

    /**
     * 提供给sql使用 query
     *
     * @param target
     *      服务对象
     * @param <T>
     * @return
     * @throws NullPointerException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static <T> Map<Field, Method> ObtainNoMethodMap(final T target,final boolean isValue)
            throws NullPointerException, InstantiationException, IllegalAccessException, InvocationTargetException {
        if (target == null) {
            throw new NullPointerException("传过来的target的参数是null");
        }
        // 实例化一下method的数组
        Map<Field, Method> result = new HashMap<Field, Method>();
        Method[] methods = target.getClass().getDeclaredMethods();
        // 获取target对象下面的字段
        Field[] fields = target.getClass().getDeclaredFields();
        for (Method method : methods) {
            if (method.getName().substring(0, 3).equals(Variable.COMMON_GET.toLowerCase())) {
                if (isValue){
                    if(method.invoke(target,null)!=null){
                        for (Field field : fields) {
                            if (field.getName().equals(method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4))) {
                                result.put(field, method);
                            }
                        }
                    }
                }else{
                    for (Field field : fields) {
                        if (field.getName().equals(method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4))) {
                            result.put(field, method);
                        }
                    }
                }
            }
        }
        return result;
    }

    /**
     * 根据对象  获取对应的方法
     *
     * @param target  泛型对象
     * @param value   是get或者是set
     * @param isValue 如果是get方法 才执行 在get方法下面 是否有值的method
     * @param <T>
     * @return 返回list 的method的类型
     * @throws NullPointerException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static <T> List<Method> ObtainNoMthodList(final T target, final String value, final boolean isValue)
            throws NullPointerException, InstantiationException, IllegalAccessException, InvocationTargetException {
        if (value == null || "".equals(value)) {
            throw new NullPointerException("传过来的value的参数是null");
        }
        if (target == null) {
            throw new NullPointerException("传过来的target的参数是null");
        }
        // 实例化一下method的数组
        List<Method> result = new ArrayList<Method>();
        int index = 0;
        /** 获取target下面的所有的方法**/
        Method[] methods = target.getClass().getDeclaredMethods();
        for (Method method : methods) {
            String methodName = method.getName();
            /** 如果是get方法的话  那么就看是否有值  **/
            if (isValue && value.toUpperCase().equals(Variable.COMMON_GET)) {
                Object object = method.invoke(target, null);
                /** 如果有值的话  就加载进来 **/
                if (object != null) {
                    result.add(method);
                }
            }
            if (methodName.substring(0, 2).equals(value.toLowerCase())) {
                result.add(method);
            }
        }
        return result;
    }

}

写一个main运行一下。

User user = new User();
user.setId(1);

System.out.println(user.getId());
System.out.println(user.getSex());
System.out.println(user.getStatus());

Map<Field,Method> map = ClassBase.ObtainNoMethodMap(user,true);

System.out.println(map.size());

Set<Field> set = map.keySet();
for (Field field:set){
    System.out.println(field.getName());
}

运行一下 结果是对的。 OK

你可能感兴趣的:(java,反射)