Java Reflection Method Getter Get getGetter(Object instance, String methodName)

Here you can find the source of getGetter(Object instance, String methodName)

Description

get Getter

License

Apache License

Declaration

private static Method getGetter(Object instance, String methodName) 

Method Source Code


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

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class Main {
    private static Map<String, Method> getters = new HashMap<String, Method>();
    private static StringBuilder bld = new StringBuilder();

    private static Method getGetter(Object instance, String methodName) {

        bld.setLength(0);/*w w  w.  ja v a2 s  .co  m*/
        String key = bld.append(instance.getClass().getName()).append(".").append(methodName).toString();

        Method getter = getters.get(key);

        if (getter == null) {
            Method[] ms = instance.getClass().getMethods();
            for (Method m : ms) {
                if (m.getName().equals(methodName) && m.getParameterTypes().length == 0
                        && m.getReturnType() != null) {
                    getter = m;
                    getter.setAccessible(true);
                    getters.put(key, getter);
                    break;
                }
            }
        }
        return getter;
    }
}

Related

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