Search for getter method - Java Reflection

Java examples for Reflection:Getter

Description

Search for getter method

Demo Code

/*//from   ww  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{

    /**
    Same as lookupSetterMethod except uses matchForGetter rather than matchForSetter.
     */
    protected static Method lookupGetterMethod(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)
                    && matchForGetter(targetFieldName,
                            currentPrimitiveMethod.getName())) {
                return currentPrimitiveMethod;
            }
        }
        Method getterMethod = null;
        Class targetSuperclass = targetClass.getSuperclass();
        if (targetSuperclass != null) {
            getterMethod = lookupGetterMethod(targetSuperclass,
                    targetFieldName, argCount);
        }
        return getterMethod;
    }
    /**
    A convenicence method which allows for lookup of single argument getter
    method without specifying the number of arguments (assumes argCount = 1).
     */
    protected static Method lookupGetterMethod(Class targetClass,
            String targetFieldName) {
        return lookupGetterMethod(targetClass, targetFieldName, 0);
    }
    /**
    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.
    First, an excat match is attempted, then  the 'get' 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 getters
     */
    public static boolean matchForGetter(String targetFieldName,
            String methodName) {
        return targetFieldName.equals(methodName)
                || FieldValueAccessorUtil.matchWithPrefix(targetFieldName,
                        methodName, "get");
    }
    /**
    Determines if firstString equals (secondStringPrefix + secondStringSuffix)
    without actually concatanting the strings.

    @param firstString any String
    @param secondStringPrefix any String
    @param secondStringSuffix any String
    @return a boolean indicating a match
     */
    protected static boolean equals(String firstString,
            String secondStringPrefix, String secondStringSuffix) {
        int secondStringPrefixLength = secondStringPrefix.length();
        int secondStringSuffixLength = secondStringSuffix.length();
        int secondStringLength = secondStringPrefixLength
                + secondStringSuffixLength;
        return (firstString.length() == secondStringLength)
                && firstString.regionMatches(0, secondStringPrefix, 0,
                        secondStringPrefixLength)
                && firstString.regionMatches(secondStringPrefixLength,
                        secondStringSuffix, 0, secondStringSuffixLength);
    }
    /**
    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