Java Reflection Method Getter Get getGetterWithPrefix(final Class target, final String property, final String prefix)

Here you can find the source of getGetterWithPrefix(final Class target, final String property, final String prefix)

Description

Internal method used by the getGetter method to try the different possible prefixes 'get', 'has' and 'is'.

License

Apache License

Parameter

Parameter Description
target Target object exposing the getter.
property Property being searched.
prefix Prefix for the supposed getter.

Return

The getter method if it exists, null otherwise.

Declaration

protected static Method getGetterWithPrefix(final Class target, final String property, final String prefix) 

Method Source Code

//package com.java2s;
/**//  w ww . j av a2s  .c  o m
 * A collection of small utilities for component management and reflection handling.
 * <p/>
 * <hr/> Copyright 2006-2012 Torsten Heup
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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.
 */

import java.lang.reflect.Method;

public class Main {
    /**
     * Internal method used by the getGetter method to try the different possible prefixes 'get', 'has' and 'is'.
     *
     * @param target   Target object exposing the getter.
     * @param property Property being searched.
     * @param prefix   Prefix for the supposed getter.
     * @return The getter method if it exists, null otherwise.
     */
    protected static Method getGetterWithPrefix(final Class target, final String property, final String prefix) {
        String name = prefix + Character.toUpperCase(property.charAt(0));
        if (property.length() > 1)
            name = name + property.substring(1);

        return getMethod(target, name);
    }

    /**
     * Returns a the specified method of the target object, or null if the method does not exist.  This is merely a
     * conveniance method to avoid reflection's exceptions.
     *
     * @param target        Target object to check
     * @param methodName    Name of the method being queried
     * @param argumentTypes Types of the method's arguments
     * @return Method instance if the method exists, false otherwise.
     */
    public static synchronized Method getMethod(final Object target, final String methodName,
            final Class... argumentTypes) {
        return getMethod(target.getClass(), methodName, argumentTypes);
    }

    /**
     * Attempts to find a method with the given name and argument types on the specified class. Other than Class's
     * getMethod(String, Class...) method, this method will also return protected and private methods.
     *
     * @param clazz         Class supposed to offer the method being searched.
     * @param methodName    Name of the method.
     * @param argumentTypes The arguments found in the method signature, if any.
     * @return a method with the given name and argument types on the specified class, if any.
     */
    public static synchronized Method getMethod(final Class clazz, final String methodName,
            final Class... argumentTypes) {
        try {
            return clazz.getMethod(methodName, argumentTypes);
        } catch (NoSuchMethodException e) {
            /* Ok, so there's no public method... */
        }

        Class runner = clazz;
        while (runner != null) {
            try {
                return runner.getDeclaredMethod(methodName, argumentTypes);
            } catch (NoSuchMethodException e) {
                /* No luck here either */
            }
            runner = runner.getSuperclass();
        }

        /* Still no luck, means there is no suitable method */
        return null;
    }
}

Related

  1. getGettersAndSetters(Object obj)
  2. getGetterSetterMethodsParameterType(Field f)
  3. getGetterShorthandName(Method method)
  4. getGettersMethods(Object object)
  5. getGetterValue(Method method, Object o)