一、前言
关于dyuproject-util-1.1.6.jar源码包中的com.dyuproject.util.reflect.ReflectUtil反向映射工具类,对获取get方法getGetterMethods、获取getSetterMethods方法、属性转换toProperty等操作。
二、源码说明
package com.dyuproject.util.reflect;@b@@b@import java.lang.reflect.Method;@b@import java.lang.reflect.Modifier;@b@import java.util.HashMap;@b@import java.util.Map;@b@@b@public final class ReflectUtil@b@{@b@ static final String GET = "get";@b@ static final String IS = "is";@b@ static final String SET = "set";@b@ static final String GET_INSTANCE = "getInstance";@b@ private static final Class<?>[] __emptyArg = new Class[0];@b@ private static final Object[] __getterArg = new Object[0];@b@@b@ public static Map<String, Method> getGetterMethods(Class<?> pojoClass)@b@ {@b@ HashMap methods = new HashMap();@b@ fillGetterMethods(pojoClass, methods);@b@ return methods;@b@ }@b@@b@ private static void fillGetterMethods(Class<?> pojoClass, Map<String, Method> baseMap)@b@ {@b@ if (pojoClass.getSuperclass() != Object.class)@b@ fillGetterMethods(pojoClass.getSuperclass(), baseMap);@b@@b@ Method[] methods = pojoClass.getDeclaredMethods();@b@ for (int i = 0; i < methods.length; ++i)@b@ {@b@ Method m = methods[i];@b@ if ((!(Modifier.isStatic(m.getModifiers()))) && (m.getParameterTypes().length == 0) && (m.getReturnType() != null) && (Modifier.isPublic(m.getModifiers())))@b@ {@b@ String name = m.getName();@b@ if (name.startsWith("is"))@b@ baseMap.put(toProperty("is".length(), name), m);@b@ else if (name.startsWith("get"))@b@ baseMap.put(toProperty("get".length(), name), m);@b@ }@b@ }@b@ }@b@@b@ public static Map<String, Method> getSetterMethods(Class<?> pojoClass)@b@ {@b@ HashMap methods = new HashMap();@b@ fillSetterMethods(pojoClass, methods);@b@ return methods;@b@ }@b@@b@ private static void fillSetterMethods(Class<?> pojoClass, Map<String, Method> baseMap)@b@ {@b@ if (pojoClass.getSuperclass() != Object.class)@b@ fillSetterMethods(pojoClass.getSuperclass(), baseMap);@b@@b@ Method[] methods = pojoClass.getDeclaredMethods();@b@ for (int i = 0; i < methods.length; ++i)@b@ {@b@ Method m = methods[i];@b@ if ((!(Modifier.isStatic(m.getModifiers()))) && (m.getParameterTypes().length == 1) && (m.getName().startsWith("set")) && (Modifier.isPublic(m.getModifiers())))@b@ {@b@ baseMap.put(toProperty("set".length(), m.getName()), m);@b@ }@b@ }@b@ }@b@@b@ public static String toProperty(int start, String methodName)@b@ {@b@ char[] prop = new char[methodName.length() - start];@b@ methodName.getChars(start, methodName.length(), prop, 0);@b@ int firstLetter = prop[0];@b@ prop[0] = (char)((firstLetter < 91) ? firstLetter + 32 : firstLetter);@b@ return new String(prop);@b@ }@b@@b@ public static String toField(int start, String methodName)@b@ {@b@ return toProperty(start, methodName);@b@ }@b@@b@ public static Object getInstance(Class<?> clazz)@b@ {@b@ Method m = null;@b@ try@b@ {@b@ m = clazz.getDeclaredMethod("getInstance", __emptyArg);@b@ }@b@ catch (Exception e)@b@ {@b@ try@b@ {@b@ m = clazz.getDeclaredMethod("get" + clazz.getSimpleName(), __emptyArg);@b@ }@b@ catch (Exception e1)@b@ {@b@ return null;@b@ }@b@ }@b@ if ((m != null) && (Modifier.isStatic(m.getModifiers())) && (Modifier.isPublic(m.getModifiers())))@b@ {@b@ try@b@ {@b@ return m.invoke(null, __getterArg);@b@ }@b@ catch (Exception e)@b@ {@b@ return null;@b@ }@b@ }@b@ return null;@b@ }@b@@b@ public static Object newInstance(Class<?> clazz)@b@ throws InstantiationException, IllegalAccessException@b@ {@b@ Object o = getInstance(clazz);@b@ return ((o == null) ? clazz.newInstance() : o);@b@ }@b@}