Java Reflection Method Getter Get getGetter(Object object, String name)

Here you can find the source of getGetter(Object object, String name)

Description

get getter by name

License

Apache License

Parameter

Parameter Description
object container of setter
name name of property

Return

requester getter

Declaration

public static Method getGetter(Object object, String name) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Method;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /***/*from w  ww  .j av  a  2 s.  c  o m*/
     * get getter by name
     *
     * @param object container of setter
     * @param name name of property
     * @return requester getter
     */
    public static Method getGetter(Object object, String name) {
        String getterName = "get" + capitaliseName(name);
        return getMethod(object, getterName);
    }

    private static String capitaliseName(String name) {
        return name.substring(0, 1).toUpperCase() + name.substring(1);
    }

    /***
     * get method by name
     *
     * @param object container of method
     * @param name name of method
     * @param params params of method
     * @return requested method
     */
    public static Method getMethod(Object object, String name, Object... params) {
        try {
            List<Class> classParams = new ArrayList<>();
            for (int i = 0; i < params.length; i++) {
                classParams.add((Class) params[i]);
            }
            Class<?> classParamsArray[] = new Class[classParams.size()];
            classParams.toArray(classParamsArray);
            return object.getClass().getMethod(name, classParamsArray);
        } catch (NoSuchMethodException e) {
        }
        return null;
    }
}

Related

  1. getGetter(final Class clz, final String propertyName)
  2. getGetter(final Object o, final String fieldName)
  3. getGetter(Object bean, String property)
  4. getGetter(Object instance, String methodName)
  5. getGetter(Object o, String name)
  6. getGetter(String key, Class clazz)
  7. getGetter(String name, Type type)
  8. getGetter(String propertyName, Class clazz)
  9. getGetterAttributeType(Method m)