Java Reflection Method Getter Get getGetter(String key, Class clazz)

Here you can find the source of getGetter(String key, Class clazz)

Description

get Getter

License

Open Source License

Parameter

Parameter Description
key a parameter
clazz a parameter

Return

null

Declaration

public static Method getGetter(String key, Class<?> clazz) 

Method Source Code

//package com.java2s;
/************************************************************************************
 * @File name   :      ReflectionUtil.java
 *
 * @Author      :      JUNJZHU//from   w  ww  . j  av a  2 s  .  c  o 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 null
     */
    public static Method getGetter(Method method) {
        if (isGetter(method))
            return method;
        if (isSetter(method)) {
            try {
                Method getter = method.getDeclaringClass().getMethod(getSetterName(method), new Class[0]);
                if (isGetter(method) && getPropertyClass(getter) == getPropertyClass(method))
                    return getter;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * @Author : XIAOXCHE
     * @Date : 2012-12-10
     * @param method
     * @param declaringClass
     * @return null
     */
    public static Method getGetter(Method method, Class<?> declaringClass) {
        if (isGetter(method))
            return method;
        try {
            return declaringClass.getMethod(getGetterName(method), new Class[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Author : XIAOXCHE
     * @Date : 2012-12-10
     * @param key
     * @param clazz
     * @return null
     */
    public static Method getGetter(String key, Class<?> clazz) {
        if (!key.startsWith("get")) {
            key = "get" + getDefaultProperty(key);
        }
        try {
            return clazz.getMethod(key, new Class[0]);
        } catch (Exception e) {
            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 methodName
     * @param clazz
     * @return null
     */
    public static Method getMethod(String methodName, Class<?> clazz) {
        Method[] methods = clazz.getMethods();
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (method.getName().equals(methodName)) {
                return method;
            }
        }
        return null;
    }

    /**
     * @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 null
     */
    public static Class<?> getPropertyClass(Method method) {
        if (isGetter(method)) {
            return method.getReturnType();
        }
        if (isSetter(method)) {
            return method.getParameterTypes()[0];
        }
        return null;
    }

    /**
     * @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 property
     * @return propertyName
     */
    public static String getDefaultProperty(String property) {

        char[] chars = property.toCharArray();
        chars[0] = Character.toUpperCase(chars[0]);
        String propertyName = String.valueOf(chars);
        return propertyName;
    }

    /**
     * @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;
    }
}

Related

  1. getGetter(final Object o, final String fieldName)
  2. getGetter(Object bean, String property)
  3. getGetter(Object instance, String methodName)
  4. getGetter(Object o, String name)
  5. getGetter(Object object, String name)
  6. getGetter(String name, Type type)
  7. getGetter(String propertyName, Class clazz)
  8. getGetterAttributeType(Method m)
  9. getGetterFieldName(Method method)