Here you can find the source of getSetter(String key, Class> clazz, Class> paramClazz)
Parameter | Description |
---|---|
key | a parameter |
clazz | a parameter |
paramClazz | a parameter |
public static Method getSetter(String key, Class<?> clazz, Class<?> paramClazz)
//package com.java2s; /************************************************************************************ * @File name : ReflectionUtil.java * * @Author : JUNJZHU/*from w ww . j a va2 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 getSetter(Method method) { if (isSetter(method)) return method; if (isGetter(method)) { Class<?> clazz = getPropertyClass(method); try { Method setter = method.getDeclaringClass().getMethod(getSetterName(method), clazz); if (isSetter(setter) && getPropertyClass(setter) == getPropertyClass(method)) return setter; } catch (Exception e) { e.printStackTrace(); } } return null; } /** * @Author : XIAOXCHE * @Date : 2012-12-10 * @param method * @param declaringClass * @return null */ public static Method getSetter(Method method, Class<?> declaringClass) { if (isSetter(method)) return method; Class<?> clazz = getPropertyClass(method); try { return declaringClass.getMethod(getSetterName(method), clazz); } catch (Exception e) { e.printStackTrace(); } return null; } /** * @Author : XIAOXCHE * @Date : 2012-12-10 * @param key * @param clazz * @param paramClazz * @return null */ public static Method getSetter(String key, Class<?> clazz, Class<?> paramClazz) { if (!key.startsWith("set")) { key = "set" + getDefaultProperty(key); } try { return clazz.getMethod(key, new Class[] { paramClazz }); } catch (Exception e) { return null; } } /** * @Author : XIAOXCHE * @Date : 2012-12-10 * @param methodName * @param clazz * @return null */ public static Method getSetter(String methodName, Class<?> clazz) { Method[] methods = clazz.getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (!isSetter(method)) { continue; } if (getDefaultProperty(methodName).equals(getDefaultPropertyName(method))) { return method; } } return null; } /** * @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 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 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 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 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; } }