Java Reflection Method Getter Get getGetterMethod(Class containingClass, String propertyName)

Here you can find the source of getGetterMethod(Class containingClass, String propertyName)

Description

get Getter Method

License

Apache License

Parameter

Parameter Description
containingClass never null
propertyName never null

Return

true if that getter exists

Declaration

public static Method getGetterMethod(Class containingClass, String propertyName) 

Method Source Code

//package com.java2s;
/*//w  ww.  j av a 2  s .  com
 * Copied from the Hibernate Validator project
 * Original authors: Hardy Ferentschik, Gunnar Morling and Kevin Pollet.
 *
 * 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.
 */

import java.lang.reflect.Method;

public class Main {
    private static final String PROPERTY_ACCESSOR_PREFIX_GET = "get";
    private static final String PROPERTY_ACCESSOR_PREFIX_IS = "is";

    /**
     * @param containingClass never null
     * @param propertyName never null
     * @return true if that getter exists
     */
    public static Method getGetterMethod(Class containingClass, String propertyName) {
        String getterName = PROPERTY_ACCESSOR_PREFIX_GET + (propertyName.isEmpty() ? ""
                : propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1));
        try {
            return containingClass.getMethod(getterName);
        } catch (NoSuchMethodException e) {
            // intentionally empty
        }
        String isserName = PROPERTY_ACCESSOR_PREFIX_IS + (propertyName.isEmpty() ? ""
                : propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1));
        try {
            Method method = containingClass.getMethod(isserName);
            if (method.getReturnType() == boolean.class) {
                return method;
            }
        } catch (NoSuchMethodException e) {
            // intentionally empty
        }
        return null;
    }
}

Related

  1. getGetterFields(final Class clazz)
  2. getGetterFor(Field field)
  3. getGetterFor(Object obj, Field field, Class objClass)
  4. getGetterFromCache(Class clazz)
  5. getGetterFromProperty(Class clazz, String property)
  6. getGetterMethod(Class type, String property)
  7. getGetterMethod(Class beanClass, String property)
  8. getGetterMethod(Class c, String field)
  9. getGetterMethod(Class clazz, String fieldName)