Java Utililty Methods Reflection Method Setter Get

List of utility methods to do Reflection Method Setter Get

Description

The list of methods to do Reflection Method Setter Get are organized into topic(s).

Method

ClassgetSetterAttributeType(Method m)
get Setter Attribute Type
return m.getParameterTypes()[0];
StringgetSetterFieldName(Method method)
Extract field name from the setter method
String methodName = method.getName();
if (methodName.startsWith("set")) {
    return getFieldName(methodName.substring(3));
throw new IllegalArgumentException("Invalid setter method" + method.getName());
MethodgetSetterForGetter(Method[] methods, Method getter)
get Setter For Getter
for (int i = 0; i < methods.length; i++) {
    Method method = methods[i];
    if (method.getName().startsWith("set")) {
        if (method.getName().substring(1).equals(getter.getName().substring(1))) {
            return method;
return null;
ListgetSetterFromCache(Class clazz)
get Setter From Cache
List<Method> methods = setterCache.get(clazz);
if (methods == null) {
    methods = new ArrayList<>();
    for (Method method : clazz.getDeclaredMethods()) {
        String name = method.getName();
        if (name.startsWith("set") && method.getParameterTypes().length == 1 && name.length() > 3) {
            methods.add(method);
    setterCache.put(clazz, methods);
return methods;
MapgetSetterMap(Class cls)
get Setter Map
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;
MethodgetSetterMethod(Class beanClass, String fieldName, List supportedTypes)
get Setter Method
for (Class clazz : supportedTypes) {
    try {
        Class[] argList = new Class[1];
        argList[0] = clazz;
        return beanClass.getMethod("set" + fieldName, argList);
    } catch (NoSuchMethodException nsme) {
return null;
MethodgetSetterMethod(Class c, String methodName)
get Setter Method
Method result = null;
Method[] methods = c.getMethods();
for (Method m : methods) {
    String name = m.getName();
    if (name.equalsIgnoreCase(methodName)) {
        Class[] params = m.getParameterTypes();
        if (params.length == 1 && PRIMITIVES.contains(params[0])) {
            result = m;
...
MethodgetSetterMethod(Class containingClass, Class propertyType, String propertyName)
get Setter Method
String setterName = PROPERTY_MUTATOR_PREFIX + (propertyName.isEmpty() ? ""
        : propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1));
try {
    return containingClass.getMethod(setterName, propertyType);
} catch (NoSuchMethodException e) {
    return null;
MethodgetSetterMethod(Class type, String property)
get Setter Method
String setterName = getEtterMethodName(property, "set");
Method setterMethod = null;
Method[] methods = type.getMethods();
for (int i = 0; i < methods.length; i++) {
    Method method = methods[i];
    if (method.getName().equals(setterName)) {
        if (setterMethod != null)
            throw new IllegalStateException(String.format(
...
MethodgetSetterMethod(Class beanClass, String property)
Returns setter method for property in specified beanClass
Method getter = getGetterMethod(beanClass, property);
if (getter == null)
    return null;
else
    return getSetterMethod(beanClass, property, getter.getReturnType());