Java Reflection Method Setter Get getSetterMap(Class cls)

Here you can find the source of getSetterMap(Class cls)

Description

get Setter Map

License

Apache License

Parameter

Parameter Description
cls a parameter

Return

setter map

Declaration

public static Map getSetterMap(Class cls) 

Method Source Code

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

import java.lang.reflect.Method;

import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;

public class Main {
    /**//  ww w .  j a  v  a2s.com
     * 
     * @param cls
     * @return setter map
     * @deprecated
     *///FIXME: java docs @return, check if this is used?
    public static Map getSetterMap(Class cls) {
        TreeMap map = new TreeMap();
        Method methods[] = getAllSetters(cls);
        String memberName;
        for (int i = 0; i < methods.length; i++) {
            memberName = methods[i].getName();
            memberName = memberName.substring(3);
            map.put(memberName, methods[i]);
        }
        return map;
    }

    public static Method[] getAllSetters(Class cls) {
        Map map = getAllSettersMap(cls);
        if (map == null) {
            return null;
        }
        Collection col = map.values();
        return (Method[]) col.toArray(new Method[0]);
    }

    public static Map getAllSettersMap(Class cls) {
        if (cls == null) {
            return null;
        }
        Method methods[] = cls.getMethods();
        TreeMap map = new TreeMap();
        Method method;
        String name;
        try {
            for (int i = 0; i < methods.length; i++) {
                method = methods[i];
                name = method.getName();
                if (name.length() <= 3 || name.startsWith("set") == false) {
                    continue;
                }
                if (method.getParameterTypes().length == 1) {
                    map.put(method.getName(), method);
                }
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return map;
    }
}

Related

  1. getSetter(String s)
  2. getSetterAttributeType(Method m)
  3. getSetterFieldName(Method method)
  4. getSetterForGetter(Method[] methods, Method getter)
  5. getSetterFromCache(Class clazz)
  6. getSetterMethod(Class beanClass, String fieldName, List supportedTypes)
  7. getSetterMethod(Class c, String methodName)
  8. getSetterMethod(Class containingClass, Class propertyType, String propertyName)
  9. getSetterMethod(Class type, String property)