Java Reflection Method Setter Get getSetterMethod(Class beanClass, String property)

Here you can find the source of getSetterMethod(Class beanClass, String property)

Description

Returns setter method for property in specified beanClass

License

Open Source License

Parameter

Parameter Description
beanClass bean class
property name of the property

Return

setter method. null if property is not found, or it is readonly property

Declaration

public static Method getSetterMethod(Class<?> beanClass, String property) 

Method Source Code

//package com.java2s;
/**/*from   www  .j a va2s  .  com*/
 * JLibs: Common Utilities for Java
 * Copyright (C) 2009  Santhosh Kumar T <santhosh.tekuri@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * 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.
 */

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

public class Main {
    /** prefix used by non-boolean getter methods */
    public static final String GET = "get";
    /** prefix used by boolean getter methods */
    public static final String IS = "is";
    /** prefix used by setter methods */
    public static final String SET = "set";

    /**
     * Returns setter method for <code>property</code> in specified <code>beanClass</code>
     *
     * @param beanClass bean class
     * @param property  name of the property
     *
     * @return setter method. null if <code>property</code> is not found, or it is readonly property
     *
     * @see #getSetterMethod(Class, String, Class)
     */
    public static Method getSetterMethod(Class<?> beanClass, String property) {
        Method getter = getGetterMethod(beanClass, property);
        if (getter == null)
            return null;
        else
            return getSetterMethod(beanClass, property, getter.getReturnType());
    }

    /**
     * Returns setter method for <code>property</code> in specified <code>beanClass</code>
     *
     * @param beanClass bean class
     * @param property  name of the property
     * @param propertyType type of the property. This is used to compute setter method name.
     *
     * @return setter method. null if <code>property</code> is not found, or it is readonly property
     *
     * @see #getSetterMethod(Class, String)
     */
    public static Method getSetterMethod(Class<?> beanClass, String property, Class propertyType) {
        try {
            return beanClass.getMethod(SET + getMethodSuffix(property), propertyType);
        } catch (NoSuchMethodException ex) {
            return null;
        }
    }

    /**
     * Returns getter method for <code>property</code> in specified <code>beanClass</code>
     *
     * @param beanClass bean class
     * @param property  name of the property
     *
     * @return getter method. null if <code>property</code> is not found
     *
     * @see #getGetterMethod(Class, String, Class)
     */
    public static Method getGetterMethod(Class<?> beanClass, String property) {
        try {
            return beanClass.getMethod(GET + getMethodSuffix(property));
        } catch (NoSuchMethodException ex1) {
            try {
                return beanClass.getMethod(IS + getMethodSuffix(property));
            } catch (NoSuchMethodException ex2) {
                return null;
            }
        }
    }

    /**
     * Returns getter method for <code>property</code> in specified <code>beanClass</code>
     *
     * @param beanClass bean class
     * @param property  name of the property
     * @param propertyType type of the property. This is used to compute getter method name.
     *
     * @return getter method. null if <code>property</code> is not found
     *
     * @see #getGetterMethod(Class, String)
     */
    public static Method getGetterMethod(Class<?> beanClass, String property, Class propertyType) {
        try {
            try {
                if (propertyType == boolean.class)
                    return beanClass.getMethod(IS + getMethodSuffix(property));
            } catch (NoSuchMethodException ignore) {
                // ignore
            }
            return beanClass.getMethod(GET + getMethodSuffix(property));
        } catch (NoSuchMethodException ex) {
            return null;
        }
    }

    public static String getMethodSuffix(String property) {
        switch (property.length()) {
        case 0:
            throw new IllegalArgumentException("invalid property name: " + property);
        case 1:
            return property.toUpperCase(Locale.ENGLISH);
        default:
            char char0 = property.charAt(0);
            boolean upper0 = Character.isUpperCase(char0);
            char char1 = property.charAt(1);
            boolean upper1 = Character.isUpperCase(char1);
            if (upper0 && upper1) // XCoordinate ==> getXCordinate()
                return property;
            if (!upper0 && !upper1) // xcoordinate ==> getXcoordinate()
                return Character.toUpperCase(char0) + property.substring(1);
            if (!upper0 && upper1) // xCoordinate ==> getxCoordinate()
                return property;
            throw new IllegalArgumentException("invalid property name: " + property);
        }
    }
}

Related

  1. getSetterMap(Class cls)
  2. getSetterMethod(Class beanClass, String fieldName, List supportedTypes)
  3. getSetterMethod(Class c, String methodName)
  4. getSetterMethod(Class containingClass, Class propertyType, String propertyName)
  5. getSetterMethod(Class type, String property)
  6. getSetterMethod(Class classType, String fieldName, Class paramType)
  7. getSetterMethod(Class clazz, Field field)
  8. getSetterMethod(Class clazz, String name)
  9. getSetterMethod(Class cls, String property, Class valueCls)