Java Reflection Method Get from Object getMethod(Object bean, String propertyName)

Here you can find the source of getMethod(Object bean, String propertyName)

Description

Return the named getter method on the bean or null if not found.

License

LGPL

Parameter

Parameter Description
bean The bean
propertyName The property to get the getter for

Return

the named getter method

Declaration

private static Method getMethod(Object bean, String propertyName) 

Method Source Code

//package com.java2s;
/*/*from w  ww  . j a  va  2  s  .c o  m*/
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */

import java.lang.reflect.Method;

public class Main {
    /**
     * Return the named getter method on the bean or null if not found.
     *
     * @param bean The bean
     * @param propertyName The property to get the getter for
     *
     * @return the named getter method
     */
    private static Method getMethod(Object bean, String propertyName) {
        final StringBuilder sb = new StringBuilder("get").append(Character.toUpperCase(propertyName.charAt(0)));
        if (propertyName.length() > 1) {
            sb.append(propertyName.substring(1));
        }
        final String getterName = sb.toString();
        for (Method m : bean.getClass().getMethods()) {
            if (getterName.equals(m.getName()) && m.getParameterTypes().length == 0) {
                return m;
            }
        }
        return null;
    }
}

Related

  1. getMethod(final Object object, final String methodName, final Object... arguments)
  2. getMethod(final Object object, final String methodName, final Object... arguments)
  3. getMethod(final Object object, String methodName, final Class[] argTypes)
  4. getMethod(final Object target, final String methodName, final Class... argumentTypes)
  5. getMethod(final String methodName, final Object obj, final Class... argTypes)
  6. getMethod(Object instance, String methodName, Class... argsClass)
  7. getMethod(Object instance, String methodName, Class[] parameters)
  8. getMethod(Object o, String methodName)
  9. getMethod(Object o, String methodName, Class[] args)