lookup of a setter Method on a target class. - Java Reflection

Java examples for Reflection:Setter

Description

lookup of a setter Method on a target class.

Demo Code

/*/*from w w w .j  a v a 2 s .c o m*/
    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 $
 */
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 = "java2s.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];
            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");
    }
    /**
    Determines targetFieldName can be made to match the methodName is the
    prefixString is prepended and the first character of targetFieldName
    is uppercased.  This method does not actually concatenate any strings together.

    @param targetFieldName the fieldName we're trying to match
    @param methodName the actual method name we're trying to mathc with the targetFieldName
    @param prefixString the prefix for the targetFieldName (usually either 'set' or 'get')
    @return a boolean indicating a match
     */
    protected static boolean matchWithPrefix(String targetFieldName,
            String methodName, String prefixString) {
        boolean doesMatch = false;
        int keyLength = targetFieldName.length();
        if (methodName.length() == (prefixString.length() + keyLength)) {
            if (methodName.startsWith(prefixString)) {
                if (methodName.regionMatches(4, targetFieldName, 1,
                        (keyLength - 1))) {
                    if (methodName.charAt(3) == Character
                            .toUpperCase(targetFieldName.charAt(0))) {
                        doesMatch = true;
                    }
                }
            }
        }
        return doesMatch;
    }
}

Related Tutorials