集册 Java实例教程 在目标类上查找setter方法。

在目标类上查找setter方法。

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

496
在目标类上查找setter方法。

/*

    Copyright 1996-2008 Ariba, Inc.


    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

        http://www.apache.org/licenses/LICENSE-2.0


    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.


    $Id: //ariba/platform/util/core/ariba/util/fieldvalue/FieldValueAccessorUtil.java#5 $

 */
 /*
 N o w J a v a . c o m 提 供
 */

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Member;

import java.lang.reflect.Method;

import java.lang.reflect.Modifier;


public class Main{

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

        Class targetClass = String.class;

        String targetFieldName = "nowjava.com";

        System.out.println(lookupSetterMethod(targetClass,targetFieldName));

    }

    /**

    A convenience which does a lookup of a setter Method on a target class.

    This recurses up the class hierarchy until it finds the method (terminates

    at Object, of course).  The argCount parameter allows this method to perform

    lookups for either regular setters (argCount == 1) or for ClassExtension

    setters (argCount == 2).


    @param targetClass the class upon which to look for the method.  If method

    does not exist on targetClass, the superclass will be used and this method

    will be called recursively.

    @param targetFieldName the name of the field for which the setter method applies.

    Note that this would not include the "set" prefix.

    @param argCount the number of arguments expected for the target setter method.

    Generally, this should be 1, but for ClassExtension methods, this will be 2.

     */

    protected static Method lookupSetterMethod(Class targetClass,

            String targetFieldName, int argCount) {

        Method methodArray[] = getDeclaredMethods(targetClass);

        for (int index = methodArray.length - 1; index > -1; index--) {

            Method currentPrimitiveMethod = methodArray[index];/**来 自 时 代 J a v a**/

            if ((currentPrimitiveMethod.getParameterTypes().length == argCount)

                    && matchForSetter(targetFieldName,

                            currentPrimitiveMethod.getName())) {

                return currentPrimitiveMethod;

            }

        }

        Method setterMethod = null;

        Class targetSuperclass = targetClass.getSuperclass();

        if (targetSuperclass != null) {

            setterMethod = lookupSetterMethod(targetSuperclass,

                    targetFieldName, argCount);

        }

        return setterMethod;

    }

    /**

    A convenicence method which allows for lookup of single argument setter

    method without specifying the number of arguments (assumes argCount = 1).

     */

    protected static Method lookupSetterMethod(Class targetClass,

            String targetFieldName) {

        return lookupSetterMethod(targetClass, targetFieldName, 1);

    }

    /**

    A cover which hides the try/catch block.


    @param targetClass the class on which to perform getDeclaredMethods

    @return an array of Methods

     */

    protected static Method[] getDeclaredMethods(Class targetClass) {

        try {

            return targetClass.getDeclaredMethods();

        } catch (SecurityException securityException) {

            throw new FieldValueException(securityException);

        }

    }

    /**

    Determines if the methodName can be used given the targetFieldName.

    The 'set' prefix is applied and the first letter of targetFieldName is capitalized.


    @param targetFieldName the desired field name

    @param methodName the actual method name

    @return wheter or not the two match according to the rules for matching setters

     */

    public static boolean matchForSetter(String targetFieldName,

            String methodName) {

        return FieldValueAccessorUtil.matchWithPrefix(targetFieldName,

                methodName, "set");

    }

    
展开阅读全文