Java Reflection Method Setter Get getSetter(Class clazz, Field field)

Here you can find the source of getSetter(Class clazz, Field field)

Description

get Setter

License

Apache License

Declaration

public static Method getSetter(Class<?> clazz, Field field) throws SecurityException, NoSuchMethodException 

Method Source Code


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

import java.lang.reflect.Field;

import java.lang.reflect.Method;
import java.util.Locale;

public class Main {
    public static Method getSetter(Object object, Field field) throws SecurityException, NoSuchMethodException {
        return getSetter(object.getClass(), field);
    }//from  w  w  w.  j  a va  2 s . c  om

    public static Method getSetter(Class<?> clazz, Field field) throws SecurityException, NoSuchMethodException {
        Method setter = clazz.getMethod(getSetterName(field), field.getType());
        if (!setter.getReturnType().equals(void.class)) {
            throw new NoSuchMethodException();
        }
        return setter;
    }

    public static String getSetterName(Field field) {
        String name = field.getName();
        return "set" + name.substring(0, 1).toUpperCase(Locale.US) + name.substring(1);
    }
}

Related

  1. getSetter(A instance, String strAttributeName, Class clazz)
  2. getSetter(Class _class, String fieldName)
  3. getSetter(Class clazz, String name)
  4. getSetter(Class clazz, Field field)
  5. getSetter(Class clazz, String property, Class type)
  6. getSetter(Class cls, final String fieldName)
  7. getSetter(final Class targetClass, final String propertyName)