Java Reflection Method Setter Get getSetterMethod(final String methodName, final Class pojoClass, final Class attributeClass)

Here you can find the source of getSetterMethod(final String methodName, final Class pojoClass, final Class attributeClass)

Description

Returns setter method for provided POJO class and class of attribute.

License

Open Source License

Parameter

Parameter Description
methodName method name
pojoClass class of POJO
attributeClass class of attribute of setter

Return

setter method or null

Declaration

static Method getSetterMethod(final String methodName, final Class<?> pojoClass,
        final Class<?> attributeClass) 

Method Source Code


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

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Main {
    /**/*  w w  w  .java2  s .  c  om*/
     * Returns setter method for provided POJO class and class of attribute.
     * If class of attribute is a wrapper around primitive Java type method looks
     * for both setters accepting wrapper or primitive.
     * If setter not found returns null.
     *
     * @param methodName method name
     * @param pojoClass class of POJO
     * @param attributeClass class of attribute of setter
     *
     * @return setter method or null
     */
    static Method getSetterMethod(final String methodName, final Class<?> pojoClass,
            final Class<?> attributeClass) {
        try {
            return pojoClass.getDeclaredMethod(methodName, attributeClass);
        } catch (NoSuchMethodException setterNotFoundException) {

            /* Try to find with primitive type in signature */
            if (hasPrimitiveType(attributeClass)) {
                try {
                    return pojoClass.getDeclaredMethod(methodName, getPrimitiveType(attributeClass));
                } catch (NoSuchMethodException setterNotFoundAgainException) {
                    /* Ignore this exception because null will be returned */
                }
            }
        }

        return null;
    }

    /**
     * Checks if provided class has equivalent primitive Java type.
     *
     * @param clazz class
     * @return true - class has equivalent Java type, false - doesn't have
     */
    static boolean hasPrimitiveType(final Class<?> clazz) {
        for (Field field : clazz.getFields()) {
            if ("TYPE".equals(field.getName())) {
                return true;
            }
        }

        return false;
    }

    /**
     * Returns class that represents a primitive Java type of wrapper class. If
     * provided class doesn't have primitive type returns null.
     *
     * @param clazz wrapper class
     * @return class of primitive Java type or null
     */
    static Class<?> getPrimitiveType(final Class<?> clazz) {
        try {
            return (Class<?>) clazz.getField("TYPE").get(null);
        } catch (IllegalAccessException | NoSuchFieldException unexpectedException) {
            return null;
        }
    }
}

Related

  1. getSetterMethod(Class clazz, Field field)
  2. getSetterMethod(Class clazz, String name)
  3. getSetterMethod(Class cls, String property, Class valueCls)
  4. getSetterMethod(final Class clazz, final Class annotation, final String name, final String value)
  5. getSetterMethod(final Class clazz, final String propertyName, final Class type)
  6. getSetterMethod(Object obj, String name)
  7. getSetterMethod(String getterName, Class returnType, Class containingClass)
  8. getSetterMethod(String setterName, Object bean, Class setterParamType)
  9. getSetterMethodByFieldName(String fieldName, Field field)