Java Reflection Method Setter Get getSetter(Object object, String name, Class type)

Here you can find the source of getSetter(Object object, String name, Class type)

Description

get setter method by property name

License

Apache License

Parameter

Parameter Description
object container of getter
name name of property
type type of property

Return

requested setter

Declaration

public static Method getSetter(Object object, String name, Class type) 

Method Source Code

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

import java.lang.reflect.Method;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /***//from w  w w. j a v a  2  s  .c o m
     * get setter method by property name
     *
     * @param object container of getter
     * @param name name of property
     * @param type type of property
     * @return requested setter
     */
    public static Method getSetter(Object object, String name, Class type) {
        String setterName = "set" + capitaliseName(name);
        return getMethod(object, setterName, type);
    }

    private static String capitaliseName(String name) {
        return name.substring(0, 1).toUpperCase() + name.substring(1);
    }

    /***
     * get method by name
     *
     * @param object container of method
     * @param name name of method
     * @param params params of method
     * @return requested method
     */
    public static Method getMethod(Object object, String name, Object... params) {
        try {
            List<Class> classParams = new ArrayList<>();
            for (int i = 0; i < params.length; i++) {
                classParams.add((Class) params[i]);
            }
            Class<?> classParamsArray[] = new Class[classParams.size()];
            classParams.toArray(classParamsArray);
            return object.getClass().getMethod(name, classParamsArray);
        } catch (NoSuchMethodException e) {
        }
        return null;
    }
}

Related

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