Java Reflection Method Setter Get getSetterName(Method method)

Here you can find the source of getSetterName(Method method)

Description

get Setter Name

License

Open Source License

Parameter

Parameter Description
method a parameter

Return

setterName

Declaration

public static String getSetterName(Method method) 

Method Source Code

//package com.java2s;
/************************************************************************************
 * @File name   :      ReflectionUtil.java
 *
 * @Author      :      JUNJZHU/*ww w.  j a va 2 s .c  om*/
 *
 * @Date        :      2012-11-16
 *
 * @Copyright Notice: 
 * Copyright (c) 2012 Shanghai OnStar, Inc. All  Rights Reserved.
 * This software is published under the terms of the Shanghai OnStar Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------------
 * Date                        Who               Version            Comments
 * 2012-11-16 ????10:26:21         JUNJZHU         1.0            Initial Version
 ************************************************************************************/

import java.lang.reflect.Method;

public class Main {
    /**
     * @Author : XIAOXCHE
     * @Date : 2012-12-10
     * @param method
     * @return setterName
     */
    public static String getSetterName(Method method) {
        String name = getDefaultPropertyName(method);
        if (name == null)
            return null;
        return "set" + name;
    }

    /**
     * @Author : XIAOXCHE
     * @Date : 2012-12-10
     * @param method
     * @return defaultPropertyName
     */
    public static String getDefaultPropertyName(Method method) {
        if (isGetter(method) || isSetter(method)) {
            return method.getName().substring(3);
        }
        return null;
    }

    /**
     * @Author : XIAOXCHE
     * @Date : 2012-12-10
     * @param method
     * @return getter
     */
    public static boolean isGetter(Method method) {
        return method.getName().startsWith("get") && method.getParameterTypes().length == 0
                && method.getReturnType() != void.class;
    }

    /**
     * @Author : XIAOXCHE
     * @Date : 2012-12-10
     * @param method
     * @param clazz
     * @return getter
     */
    public static boolean isGetter(Method method, Class<?> clazz) {
        return isGetter(method) && getPropertyClass(method) == clazz;
    }

    /**
     * @Author : XIAOXCHE
     * @Date : 2012-12-10
     * @param method
     * @return method
     */
    public static boolean isSetter(Method method) {
        return method.getName().startsWith("set") && method.getParameterTypes().length == 1
                && method.getReturnType() == void.class;
    }

    /**
     * @Author : XIAOXCHE
     * @Date : 2012-12-10
     * @param method
     * @param clazz
     * @return setter
     */
    public static boolean isSetter(Method method, Class<?> clazz) {
        return isSetter(method) && getPropertyClass(method) == clazz;
    }

    /**
     * @Author : XIAOXCHE
     * @Date : 2012-12-10
     * @param method
     * @return null
     */
    public static Class<?> getPropertyClass(Method method) {
        if (isGetter(method)) {
            return method.getReturnType();
        }
        if (isSetter(method)) {
            return method.getParameterTypes()[0];
        }
        return null;
    }
}

Related

  1. getSetterMethods(final Class clazz)
  2. getSetterName(Field field)
  3. getSetterName(final Field field)
  4. getSetterName(Method m)
  5. getSetterName(Method m)
  6. getSetterName(String name)
  7. getSetterOrGetter(Class clazz, String name, boolean isSetter)
  8. getSetters(Class clazz)
  9. getSetters(Class clazz)