Java Reflection Method Setter Get getSetter(Object instance, String methodName)

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

Description

get Setter

License

Apache License

Declaration

private static Method getSetter(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> setters = new HashMap<String, Method>();
    private static StringBuilder bld = new StringBuilder();

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

        bld.setLength(0);/*from w  w  w  .  j av a 2  s.  com*/
        String key = bld.append(instance.getClass().getName()).append(".").append(methodName).toString();

        Method setter = setters.get(key);

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

Related

  1. getSetter(final Class targetClass, final String propertyName)
  2. getSetter(final Class clazz, final String fieldName, final Class... fieldClass)
  3. getSetter(final Class clz, final String propertyName, final Class propertyClass)
  4. getSetter(final Object o, final String fieldName)
  5. getSetter(Method m)
  6. getSetter(Object object, String name, Class type)
  7. getSetter(Object target, String property, Class parameterType)
  8. getSetter(String fieldName, Class clazz, Class fieldType)
  9. getSetter(String key, Class clazz, Class paramClazz)