Java Reflection Method Setter Get getSetterMethod(Class clazz, String name)

Here you can find the source of getSetterMethod(Class clazz, String name)

Description

get Setter Method

License

Open Source License

Declaration

public static Method getSetterMethod(Class<?> clazz, String name) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Method;

public class Main {

    public static Method getSetterMethod(Class<?> clazz, String name) {
        String methodName = convertToMehtodName("set", name);
        Method method = null;/*  w w  w . j a  v a2 s.  c  o  m*/
        try {
            method = clazz.getMethod(methodName, String.class);
        } catch (Exception e) {
            throw new RuntimeException("get method exception - ", e);
        }
        return method;
    }

    public static String convertToMehtodName(String getterOrSetter, String name) {
        String methodName = "";
        if (name != null && !name.equals("")) {
            String first = name.substring(0, 1);
            methodName = getterOrSetter + first.toUpperCase();
            if (name.length() > 1) {
                methodName = methodName + name.substring(1);
            }
        }
        return methodName;
    }
}

Related

  1. getSetterMethod(Class containingClass, Class propertyType, String propertyName)
  2. getSetterMethod(Class type, String property)
  3. getSetterMethod(Class beanClass, String property)
  4. getSetterMethod(Class classType, String fieldName, Class paramType)
  5. getSetterMethod(Class clazz, Field field)
  6. getSetterMethod(Class cls, String property, Class valueCls)
  7. getSetterMethod(final Class clazz, final Class annotation, final String name, final String value)
  8. getSetterMethod(final Class clazz, final String propertyName, final Class type)
  9. getSetterMethod(final String methodName, final Class pojoClass, final Class attributeClass)