Here you can find the source of getSetterName(Method method)
Parameter | Description |
---|---|
method | a parameter |
public static String getSetterName(Method method)
//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; } }