View Javadoc
1   package com.foxinmy.weixin4j.util;
2   
3   import java.lang.reflect.Field;
4   import java.lang.reflect.InvocationTargetException;
5   import java.lang.reflect.Method;
6   import java.lang.reflect.Modifier;
7   import java.lang.reflect.ParameterizedType;
8   import java.lang.reflect.Type;
9   import java.util.HashSet;
10  import java.util.Set;
11  
12  /**
13   * @title 反射工具类
14   * @description 提供对类,字段的反射调用
15   * @author jinyu(foxinmy@gmail.com) , 2012-10-26
16   */
17  public class ReflectionUtil {
18  
19  	/**
20  	 * 获取包包名
21  	 * 
22  	 * @param obj
23  	 * @return
24  	 */
25  	public static String getPackageName(Object obj) {
26  		return obj.getClass().getPackage().getName();
27  	}
28  
29  	/**
30  	 * 获取字段的泛型参数类型
31  	 * 
32  	 * @param obj
33  	 * @param fieldName
34  	 * @return
35  	 */
36  	public static Class<?> getFieldGenericType(Object obj, String fieldName) {
37  		Field field = getAccessibleField(obj, fieldName);
38  		Type type = field.getGenericType();
39  		if (type instanceof ParameterizedType) {
40  			return (Class<?>) ((ParameterizedType) type)
41  					.getActualTypeArguments()[0];
42  		}
43  		return null;
44  	}
45  
46  	/**
47  	 * 调用方法
48  	 * 
49  	 * @param object
50  	 *            对象
51  	 * 
52  	 * @param propertyName
53  	 *            属性名称
54  	 */
55  	public static Object invokeMethod(Object object, String propertyName) {
56  		try {
57  			Method getterMethod = object.getClass().getMethod(propertyName);
58  			return getterMethod.invoke(object);
59  		} catch (Exception e) {
60  			e.printStackTrace();
61  			return null;
62  		}
63  	}
64  
65  	public static Object invokeMethod(Object object, String propertyName,
66  			Object... args) {
67  		try {
68  			Method getterMethod = object.getClass().getMethod(propertyName);
69  			return getterMethod.invoke(object, args);
70  		} catch (Exception e) {
71  			e.printStackTrace();
72  			return null;
73  		}
74  	}
75  
76  	/**
77  	 * 调用Getter方法
78  	 * 
79  	 * @param object
80  	 *            对象
81  	 * 
82  	 * @param propertyName
83  	 *            属性名称
84  	 * @throws SecurityException
85  	 * @throws NoSuchMethodException
86  	 * @throws InvocationTargetException
87  	 * @throws IllegalArgumentException
88  	 * @throws IllegalAccessException
89  	 */
90  	public static Object invokeGetterMethod(Object object, String propertyName)
91  			throws Exception {
92  		String getterMethodName = null;
93  		Method getterMethod = null;
94  		String propertyNa = null;
95  		if (propertyName.contains(".")) {
96  			propertyNa = StringUtil.substringBefore(propertyName, ".");
97  			getterMethodName = "get" + StringUtil.capitalize(propertyNa);
98  			getterMethod = object.getClass().getMethod(getterMethodName);
99  			return invokeGetterMethod(getterMethod.invoke(object),
100 					StringUtil.substringAfter(propertyName, "."));
101 		} else {
102 			getterMethodName = "get" + StringUtil.capitalize(propertyName);
103 			getterMethod = object.getClass().getMethod(getterMethodName);
104 			return getterMethod.invoke(object);
105 		}
106 	}
107 
108 	/**
109 	 * 调用Setter方法
110 	 * 
111 	 * @param object
112 	 *            对象
113 	 * 
114 	 * @param propertyName
115 	 *            属性名称
116 	 * 
117 	 * @param propertyValue
118 	 *            属性值
119 	 */
120 	public static void invokeSetterMethod(Object object, String propertyName,
121 			Object propertyValue) {
122 		Class<?> setterMethodClass = propertyValue.getClass();
123 		invokeSetterMethod(object, propertyName, propertyValue,
124 				setterMethodClass);
125 	}
126 
127 	/**
128 	 * 调用Setter方法
129 	 * 
130 	 * @param object
131 	 *            对象
132 	 * 
133 	 * @param propertyName
134 	 *            属性名称
135 	 * 
136 	 * @param propertyValue
137 	 *            属性值
138 	 * 
139 	 * @param setterMethodClass
140 	 *            参数类型
141 	 */
142 	public static void invokeSetterMethod(Object object, String propertyName,
143 			Object propertyValue, Class<?> setterMethodClass) {
144 		String setterMethodName = "set" + StringUtil.capitalize(propertyName);
145 		try {
146 			Method setterMethod = object.getClass().getMethod(setterMethodName,
147 					setterMethodClass);
148 			setterMethod.invoke(object, propertyValue);
149 		} catch (Exception e) {
150 			e.printStackTrace();
151 		}
152 	}
153 
154 	/**
155 	 * 获取对象属性值,无视private/protected/getter
156 	 * 
157 	 * @param object
158 	 *            对象
159 	 * 
160 	 * @param fieldName
161 	 *            属性名称
162 	 */
163 	public static Object getFieldValue(Object object, String fieldName) {
164 		Field field = getAccessibleField(object, fieldName);
165 		if (field == null) {
166 			throw new IllegalArgumentException("Could not find field "
167 					+ fieldName);
168 		}
169 		Object result = null;
170 		try {
171 			result = field.get(object);
172 		} catch (IllegalAccessException e) {
173 
174 		}
175 		return result;
176 	}
177 
178 	/**
179 	 * 设置对象属性值,无视private/protected/setter
180 	 * 
181 	 * @param object
182 	 *            对象
183 	 * 
184 	 * @param fieldName
185 	 *            属性名称
186 	 */
187 	public static void setFieldValue(Object object, String fieldName,
188 			Object value) {
189 		Field field = getAccessibleField(object, fieldName);
190 		if (field == null) {
191 			throw new IllegalArgumentException("Could not find field "
192 					+ fieldName);
193 		}
194 		try {
195 			field.set(object, value);
196 		} catch (IllegalAccessException e) {
197 
198 		}
199 	}
200 
201 	// 获取字段的类型
202 	public static String getFieldType(Object object, String fieldName) {
203 		Field field = getAccessibleField(object, fieldName);
204 		return field.getType().getSimpleName();
205 	}
206 
207 	public static Field getAccessibleField(final Object object,
208 			final String fieldName) {
209 		for (Class<?> superClass = object.getClass(); superClass != Object.class;) {
210 			try {
211 				Field field = superClass.getDeclaredField(fieldName);
212 				field.setAccessible(true);
213 				return field;
214 			} catch (NoSuchFieldException e) {
215 				return null;
216 			}
217 		}
218 		return null;
219 	}
220 
221 	public static Set<Field> getAllField(Class<?> clazz) {
222 		Set<Field> fieldSet = new HashSet<Field>();
223 		while (clazz != Object.class) {
224 			Field[] fields = clazz.getDeclaredFields();
225 			for (Field field : fields) {
226 				int modifier = field.getModifiers();
227 				if (Modifier.isFinal(modifier) || Modifier.isStatic(modifier)) {
228 					continue;
229 				}
230 				fieldSet.add(field);
231 			}
232 			clazz = clazz.getSuperclass();
233 		}
234 		return fieldSet;
235 	}
236 }