Java Reflection Method Getter Get getGetterName(Method method)

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

Description

get Getter Name

License

Open Source License

Parameter

Parameter Description
method a parameter

Return

getterName

Declaration

public static String getGetterName(Method method) 

Method Source Code

//package com.java2s;
/************************************************************************************
 * @File name   :      ReflectionUtil.java
 *
 * @Author      :      JUNJZHU//from w w  w.  j  a v  a 2s.co m
 *
 * @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 getterName
     */
    public static String getGetterName(Method method) {
        String name = getDefaultPropertyName(method);
        if (name == null)
            return null;
        return "get" + 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. getGetterName(Field field)
  2. getGetterName(Field field)
  3. getGetterName(final Field field)
  4. getGetterName(Method m)
  5. getGetterName(Method m)
  6. getGetterName(Method mtd)
  7. getGetterOrNull(Class clazz, String name, Class type)
  8. getGetterPropertyName(Method getterMethod)
  9. getGetters(Class c)