Java Reflection Method Getter Get getGetterMethod(String getterName, Object bean, Class returnType)

Here you can find the source of getGetterMethod(String getterName, Object bean, Class returnType)

Description

get Getter Method

License

Open Source License

Declaration

public static Method getGetterMethod(String getterName, Object bean, Class<?> returnType) 

Method Source Code

//package com.java2s;
/*/*from w  ww .  j  a  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 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;
    }
}

Related

  1. getGetterMethod(Class c, String field)
  2. getGetterMethod(Class clazz, String fieldName)
  3. getGetterMethod(Class clazz, String methodNameWithoutGetPrefix)
  4. getGetterMethod(Class clazz, String name)
  5. getGetterMethod(Class clazz, String propertyName)
  6. getGetterMethodByProperty(String propertyName, Class beanClass, Class returnType)
  7. getGetterMethodForClass(Class cls, String beanName)
  8. getGetterMethodName(String fieldName, java.lang.Class fieldType)
  9. getGetterMethodName(String property, String javaType)