Java Utililty Methods Reflection Method Setter Get

List of utility methods to do Reflection Method Setter Get

Description

The list of methods to do Reflection Method Setter Get are organized into topic(s).

Method

MethodgetSetterMethod(Class classType, String fieldName, Class paramType)
Retrieves setter method of a field.
String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
Method m = null;
NoSuchMethodException firstException = null;
while (paramType != null && m == null) {
    try {
        m = classType.getDeclaredMethod(methodName, paramType);
    } catch (NoSuchMethodException e) {
        if (firstException == null) {
...
MethodgetSetterMethod(Class clazz, Field field)
get Setter Method
String fieldName = field.getName();
String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try {
    return getInheritMethod(clazz, methodName, new Class<?>[] { field.getType() });
} catch (NoSuchMethodException e) {
    throw new RuntimeException(e);
MethodgetSetterMethod(Class clazz, String name)
get Setter Method
String methodName = convertToMehtodName("set", name);
Method method = null;
try {
    method = clazz.getMethod(methodName, String.class);
} catch (Exception e) {
    throw new RuntimeException("get method exception - ", e);
return method;
...
MethodgetSetterMethod(Class cls, String property, Class valueCls)
get Setter Method
String name = "set" + property.substring(0, 1).toUpperCase() + property.substring(1);
try {
    return cls.getMethod(name, valueCls);
} catch (NoSuchMethodException e) {
    for (Method method : cls.getMethods()) {
        if (Modifier.isPublic(method.getModifiers()) && method.getDeclaringClass() != Object.class
                && method.getParameterTypes().length == 1 && method.getName().equals(name)) {
            return method;
...
MethodgetSetterMethod(final Class clazz, final Class annotation, final String name, final String value)
Return the setter method using the specified annotation attribute and value.
Method setterMethod = null;
for (Method possibleMethod : clazz.getMethods()) {
    if (possibleMethod.isAnnotationPresent(annotation)) {
        Annotation[] methodAnnotations = possibleMethod.getDeclaredAnnotations();
        for (Annotation methodAnnotation : methodAnnotations) {
            if (annotation.isInstance(methodAnnotation)) {
                final Class<? extends Annotation> type = methodAnnotation.annotationType();
                for (Method annotationMethod : type.getDeclaredMethods()) {
...
MethodgetSetterMethod(final Class clazz, final String propertyName, final Class type)
Returns the first public setter for the specified property and parameter type, or null for none
Method result = null;
final int excludedModifiers = Modifier.ABSTRACT | Modifier.PRIVATE | Modifier.PROTECTED | Modifier.STATIC;
final String name = "set" + propertyName;
MAIN_LOOP: for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
    for (final Method m : c.getDeclaredMethods()) {
        final int methodModifiers = m.getModifiers();
        if ((methodModifiers & excludedModifiers) != 0)
            continue;
...
MethodgetSetterMethod(final String methodName, final Class pojoClass, final Class attributeClass)
Returns setter method for provided POJO class and class of attribute.
try {
    return pojoClass.getDeclaredMethod(methodName, attributeClass);
} catch (NoSuchMethodException setterNotFoundException) {
    if (hasPrimitiveType(attributeClass)) {
        try {
            return pojoClass.getDeclaredMethod(methodName, getPrimitiveType(attributeClass));
        } catch (NoSuchMethodException setterNotFoundAgainException) {
return null;
MethodgetSetterMethod(Object obj, String name)
get Setter Method
Method[] methods = obj.getClass().getMethods();
for (Method method : methods) {
    if (method.getName().equals(name) && method.getReturnType().equals(Void.class)
            && method.getParameterTypes().length == 1) {
        return method;
return null;
...
MethodgetSetterMethod(String getterName, Class returnType, Class containingClass)
Gets the setter name of a property given the name of the getter
HashMap<String, Method> cachedSetters = s_setMethods.get(containingClass);
if (cachedSetters != null && cachedSetters.containsKey(getterName))
    return cachedSetters.get(getterName);
else
    synchronized (s_setMethods) {
        cachedSetters = new HashMap<String, Method>();
        if (!s_setMethods.containsKey(containingClass))
            s_setMethods.put(containingClass, cachedSetters);
...
MethodgetSetterMethod(String setterName, Object bean, Class setterParamType)
get Setter Method
return getSetterMethod(setterName, bean.getClass(), setterParamType);