Java Reflection Method Setter Get getSetter(Object target, String property, Class parameterType)

Here you can find the source of getSetter(Object target, String property, Class parameterType)

Description

Get setter method

License

LGPL

Parameter

Parameter Description
target target object
property property
parameterType setter parameter type

Return

setter method

Declaration

public static Method getSetter(Object target, String property, Class<?> parameterType) 

Method Source Code

//package com.java2s;
/*//www  .ja v a2s  . c o m
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */

import java.lang.reflect.Method;

public class Main {
    /**
     * Get setter method
     *
     * @param target target object
     * @param property property
     * @param parameterType setter parameter type
     *
     * @return setter method
     */
    public static Method getSetter(Object target, String property, Class<?> parameterType) {
        String setterMethodName = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
        Method setter = getMethod(target, setterMethodName, parameterType);
        setter.setAccessible(true);
        return setter;
    }

    /**
     * Get target method
     *
     * @param target target object
     * @param methodName method name
     * @param parameterTypes method parameter types
     *
     * @return return value
     */
    public static Method getMethod(Object target, String methodName, Class... parameterTypes) {
        try {
            return target.getClass().getMethod(methodName, parameterTypes);
        } catch (NoSuchMethodException e) {
            throw new IllegalArgumentException(e);
        }
    }
}

Related

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