Java Reflection Method Getter Get getGetterMethodByProperty(String propertyName, Class beanClass, Class returnType)

Here you can find the source of getGetterMethodByProperty(String propertyName, Class beanClass, Class returnType)

Description

get Getter Method By Property

License

Open Source License

Declaration

public static Method getGetterMethodByProperty(String propertyName, Class<?> beanClass, Class<?> returnType) 

Method Source Code

//package com.java2s;
/*/*  ww  w.  ja v  a 2s  .  c o  m*/
   Milyn - Copyright (C) 2006 - 2010
    
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License (version 2.1) as published by the Free Software
   Foundation.
    
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
   See the GNU Lesser General Public License for more details:
   http://www.gnu.org/licenses/lgpl.txt
*/

import java.lang.reflect.Method;

public class Main {
    public static Method getGetterMethodByProperty(String propertyName, Class<?> beanClass, Class<?> returnType) {
        Method getter = getGetterMethod(toGetterName(propertyName), beanClass, returnType);
        if (getter == null) {
            getter = getGetterMethod(toIsGetterName(propertyName), beanClass, returnType);
        }
        return getter;
    }

    public static Method getGetterMethod(String getterName, Object bean, Class<?> returnType) {
        return getGetterMethod(getterName, bean.getClass(), returnType);
    }

    public static Method getGetterMethod(String getterName, Class beanclass, Class<?> returnType) {
        Method[] methods = beanclass.getMethods();

        for (Method method : methods) {
            if (method.getName().equals(getterName)) {
                if (returnType != null) {
                    if (method.getReturnType().isAssignableFrom(returnType)) {
                        return method;
                    }
                } else {
                    return method;
                }
            }
        }

        return null;
    }

    public static String toGetterName(String property) {
        StringBuffer getterName = new StringBuffer();

        // Add the property string to the buffer...
        getterName.append(property);
        // Uppercase the first character...
        getterName.setCharAt(0, Character.toUpperCase(property.charAt(0)));
        // Prefix with "get"...
        getterName.insert(0, "get");

        return getterName.toString();
    }

    public static String toIsGetterName(String property) {
        StringBuffer getterName = new StringBuffer();

        // Add the property string to the buffer...
        getterName.append(property);
        // Uppercase the first character...
        getterName.setCharAt(0, Character.toUpperCase(property.charAt(0)));
        // Prefix with "is"...
        getterName.insert(0, "is");

        return getterName.toString();
    }
}

Related

  1. getGetterMethod(Class clazz, String fieldName)
  2. getGetterMethod(Class clazz, String methodNameWithoutGetPrefix)
  3. getGetterMethod(Class clazz, String name)
  4. getGetterMethod(Class clazz, String propertyName)
  5. getGetterMethod(String getterName, Object bean, Class returnType)
  6. getGetterMethodForClass(Class cls, String beanName)
  7. getGetterMethodName(String fieldName, java.lang.Class fieldType)
  8. getGetterMethodName(String property, String javaType)
  9. getGetterMethodNames(Object o)