集册 Java实例教程 获取属性集方法

获取属性集方法

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

362
获取属性集方法

/**

 *all rights reserved,@copyright 2003

 */
 /**
  * 时 代 J a v a - nowjava.com 提 供 
 **/

import java.lang.reflect.Method;

import java.lang.reflect.InvocationTargetException;

import java.util.ArrayList;

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(getPropertySetMethods(clz)));

    }

    public static final String PROPERTY_SET_METHOD_PREFIX = "set";

    /**

     *

     * @param clz

     * @return

     */

    public static PropertySetMethod[] getPropertySetMethods(Class clz) {

        int count;
        /**来自 
         N o w J a v a . c o m - 时代Java**/

        ArrayList al = new ArrayList();


        Method[] ms = clz.getMethods();

        count = ms.length;

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

            Method m = ms[i];

            String name = m.getName();

            if (name.startsWith(PROPERTY_SET_METHOD_PREFIX)) {

                //the setXXX  method should have only one Parameter;

                Class[] paramClasses = m.getParameterTypes();

                if (paramClasses.length == 1) {

                    Class c = paramClasses[0];

                    //note: the name of the property should be the substring from the index 3;

                    if (c == byte[].class || c == char[].class) {

                        al.add(0, new PropertySetMethod(name.substring(3),

                                paramClasses[0
展开阅读全文