集册 Java实例教程 获取链值的吸气剂方法

获取链值的吸气剂方法

欢马劈雪     最近更新时间:2020-01-02 10:19:05

471
获取链值的吸气剂方法


import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;
/* from 
nowjava.com - 时  代  Java*/

import java.util.Collection;

import java.util.Iterator;

import java.util.LinkedList;

import java.util.List;

import java.util.logging.Level;

import java.util.logging.Logger;


public class Main{

    public static void main(String[] argv) throws Exception{

        Class baseObjectClass = String.class;

        String fieldNameChain = "nowjava.com";

        System.out.println(getGetterMethodForChainValue(baseObjectClass,fieldNameChain));

    }

    private static Logger log = Logger.getLogger(BeanUtil.class

            .getSimpleName());

    public static Method getGetterMethodForChainValue(
    /*
    n o w j a v a . c o m - 时  代  Java 提 供
    */

            Class baseObjectClass, String fieldNameChain)

            throws NoSuchMethodException, NoSuchFieldException {

        if (Collection.class.isAssignableFrom(baseObjectClass))

            baseObjectClass = getCollectionGenericType(baseObjectClass);

        String[] chain = fieldNameChain.split("[.]");

        if (chain.length == 1)

            return getGetterMethodForField(baseObjectClass, chain[0]);


        return getGetterMethodForChainValue(

                getPropertyType(baseObjectClass, chain[0]),

                fieldNameChain.substring(chain[0].length() + 1));

    }

    public static Class getCollectionGenericType(Class<Collection> c) {

        return c.getClass().getTypeParameters()[0].getGenericDeclaration();

    }

    public static Method getGetterMethodForField(Object obj, Field field)

            throws NoSuchMethodException {

        try {

            return obj.getClass().getDeclaredMethod(

                    getGetterMethodNameForField(obj, field));

        } catch (NoSuchMethodException nsme) {

            log.log(Level.WARNING, "Can't find method.", nsme);

            throw nsme;

        }

    }

    public static Method getGetterMethodForField(Object obj,

            String fieldName) throws NoSuchMethodException,

            NoSuchFieldException {

        return obj.getClass().getDeclaredMethod(

                getGetterMethodNameForField(fieldName, obj.getClass()

                        .getDeclaredField(fieldName)));

    }

    public static Method getGetterMethodForField(Class obj, String fieldName)

            throws NoSuchMethodException, NoSuchFieldException {

        return obj.getDeclaredMethod(getGetterMethodNameForField(fieldName,

                obj.getDeclaredField(fieldName)));

    }

    public static Class getPropertyType(Class baseClass, String fieldName)

            throws NoSuchFieldException {

        if (Collection.class.isAssignableFrom(baseClass))

            baseClass = getCollectionGenericType(baseClass);

        return baseClass.getDeclaredField(fieldName).getType();

    }

    public static String getGetterMethodNameForField(Object obj, Field field) {

        
展开阅读全文