Java Reflection Method Setter Get getSetterMethodByProperty(String propertyName, Class beanClass, Class setterParamType)

Here you can find the source of getSetterMethodByProperty(String propertyName, Class beanClass, Class setterParamType)

Description

get Setter Method By Property

License

Open Source License

Declaration

public static Method getSetterMethodByProperty(String propertyName, Class<?> beanClass,
            Class<?> setterParamType) 

Method Source Code

//package com.java2s;
/*//from   w  w  w .  java 2 s .  c  o  m
   Milyn - Copyright (C) 2006 - 2010
    
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License (version 2.1) as published by the Free Software
   Foundation.
    
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
   See the GNU Lesser General Public License for more details:
   http://www.gnu.org/licenses/lgpl.txt
*/

import java.lang.reflect.Method;

public class Main {
    public static Method getSetterMethodByProperty(String propertyName, Class<?> beanClass,
            Class<?> setterParamType) {
        return getSetterMethod(toSetterName(propertyName), beanClass, setterParamType);
    }

    public static Method getSetterMethod(String setterName, Object bean, Class<?> setterParamType) {
        return getSetterMethod(setterName, bean.getClass(), setterParamType);
    }

    public static Method getSetterMethod(String setterName, Class beanclass, Class<?> setterParamType) {
        Method[] methods = beanclass.getMethods();

        for (Method method : methods) {
            if (method.getName().equals(setterName)) {
                Class<?>[] params = method.getParameterTypes();
                if (params != null && params.length == 1 && params[0].isAssignableFrom(setterParamType)) {
                    return method;
                }
            }
        }

        return null;
    }

    public static String toSetterName(String property) {
        StringBuffer setterName = new StringBuffer();

        // Add the property string to the buffer...
        setterName.append(property);
        // Uppercase the first character...
        setterName.setCharAt(0, Character.toUpperCase(property.charAt(0)));
        // Prefix with "set"...
        setterName.insert(0, "set");

        return setterName.toString();
    }
}

Related

  1. getSetterMethod(final String methodName, final Class pojoClass, final Class attributeClass)
  2. getSetterMethod(Object obj, String name)
  3. getSetterMethod(String getterName, Class returnType, Class containingClass)
  4. getSetterMethod(String setterName, Object bean, Class setterParamType)
  5. getSetterMethodByFieldName(String fieldName, Field field)
  6. getSetterMethodForClass(Class cls, String beanName, Class type)
  7. getSetterMethodFromGetter(final Method getter)
  8. getSetterMethods(Class clazz)
  9. getSetterMethods(Class clazz)