集册 Java实例教程 从Bean类获取所有属性;

从Bean类获取所有属性;

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

534
从Bean类获取所有属性;

/**

 *all rights reserved,@copyright 2003

 */

import java.lang.reflect.Method;

import java.lang.reflect.InvocationTargetException;

import java.util.ArrayList;//NowJava.com - 时  代  Java

import java.util.HashMap;


public class Main{

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

        Class clz = String.class;

        System.out.println(java.util.Arrays.toString(getProperties(clz)));

    }

    public static final String PROPERTY_SET_METHOD_PREFIX = "set";

    public static final String PROPERTY_GET_METHOD_PREFIX = "get";

    public static final String PROPERTY_IS_METHOD_PREFIX = "is";

    /**

     * get all the properties from a Bean Class;

     *

     * @param clz

     * @return

     */

    public static BeanProperty[] getProperties(Class clz) {

        HashMap p = new HashMap();

        Method[] methods = clz.getMethods();

        int count = methods.length;

        for (int i = 0; i < count; i++) {

            String methodName = methods[i].getName();
            /*来自 
             N o w J a v a . c o m - 时  代  Java*/

            if (methodName.startsWith(PROPERTY_SET_METHOD_PREFIX)) {

                Class[] paramTypes = methods[i].getParameterTypes();

                if (paramTypes != null && paramTypes.length == 1

                        && PrimitiveType.isPrimitive(paramTypes[0])) {

                    String propName = Character.toLowerCase(methodName

                            .charAt(PROPERTY_SET_METHOD_PREFIX.length()))

                            + methodName

                                    .substring(PROPERTY_SET_METHOD_PREFIX

                                            .length() + 1);

                    BeanProperty bp = (BeanProperty) p.get(propName);

                    if (bp == null) {

                        bp = new BeanProperty(propName,

                                PrimitiveType.getTypeName(paramTypes[0]));

                        bp.setMethodName = methodName;

                        p.put(propName, bp);

                    } else {

                        if (bp.getType().equalsIgnoreCase(

                                PrimitiveType.getTypeName(paramTypes[0]))) {

                            bp.setMethodName = methodName;

                        }

                    }

                }

            } else if (methodName.startsWith(PROPERTY_GET_METHOD_PREFIX)) {

                Class[] paramTypes = methods[i].getParameterTypes();

                Class returnType = methods[i].getReturnType();

                if ((paramTypes == null || paramTypes.length == 0)

                        && PrimitiveType.isPrimitive(returnType)) {

                    String propName = Character.toLowerCase(methodName

                            .charAt(PROPERTY_SET_METHOD_PREFIX.length()))

                            + methodName

                                    .substring(PROPERTY_SET_METHOD_PREFIX

                                            .length() + 1);

                    BeanProperty bp = (BeanProperty) p.get(propName);

                    if (bp == null) {

                        bp = new BeanProperty(propName,

                                PrimitiveType.getTypeName(returnType));

                        bp.getMethodName = methodName;

                        p.put(propName, bp);

                    } else {

                        if (bp.getType().equalsIgnoreCase(

                                PrimitiveType.getTypeName(returnType))) {

                            bp.getMethodName = methodName;

                        }

                    }

                }

            } else if (methodName.startsWith(PROPERTY_IS_METHOD_PREFIX)) {

                Class[] paramTypes = methods[i].getParameterTypes();

                Class returnType = methods[i].getReturnType();

                if ((paramTypes == null || paramTypes.length == 0)

                        && PrimitiveType.isPrimitive(returnType)) {

                    String propName = Character.toLowerCase(methodName

                            .charAt(PROPERTY_SET_METHOD_PREFIX.length()))

                            + methodName

                                    .substring(PROPERTY_SE
展开阅读全文