Java Reflection Method Setter Get getSetterMethods(Class pojoClass)

Here you can find the source of getSetterMethods(Class pojoClass)

Description

Gets the setters of a pojo as a map of String as key and Method as value.

License

Apache License

Declaration

public static Map<String, Method> getSetterMethods(Class<?> pojoClass) 

Method Source Code


//package com.java2s;
//Licensed under the Apache License, Version 2.0 (the "License");

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

public class Main {
    static final String SET = "set";

    /**/*  w w  w  .j  av a 2  s  .  co m*/
     * Gets the setters of a pojo as a map of {@link String} as key and 
     * {@link Method} as value.
     */
    public static Map<String, Method> getSetterMethods(Class<?> pojoClass) {
        HashMap<String, Method> methods = new HashMap<String, Method>();
        fillSetterMethods(pojoClass, methods);
        return methods;
    }

    private static void fillSetterMethods(Class<?> pojoClass, Map<String, Method> baseMap) {
        if (pojoClass.getSuperclass() != Object.class)
            fillSetterMethods(pojoClass.getSuperclass(), baseMap);

        Method[] methods = pojoClass.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            Method m = methods[i];
            if (!Modifier.isStatic(m.getModifiers()) && m.getParameterTypes().length == 1
                    && m.getName().startsWith(SET) && Modifier.isPublic(m.getModifiers())) {
                baseMap.put(toProperty(SET.length(), m.getName()), m);
            }
        }
    }

    /**
     * Converts a method name into a camel-case field name, starting from {@code start}.
     */
    public static String toProperty(int start, String methodName) {
        char[] prop = new char[methodName.length() - start];
        methodName.getChars(start, methodName.length(), prop, 0);
        int firstLetter = prop[0];
        prop[0] = (char) (firstLetter < 91 ? firstLetter + 32 : firstLetter);
        return new String(prop);
    }
}

Related

  1. getSetterMethodFromGetter(final Method getter)
  2. getSetterMethods(Class clazz)
  3. getSetterMethods(Class clazz)
  4. getSetterMethods(Class clazz)
  5. getSetterMethods(Class objectType)
  6. getSetterMethods(final Class clazz)
  7. getSetterName(Field field)
  8. getSetterName(final Field field)
  9. getSetterName(Method m)