1. 程式人生 > >Reflectiong 根據反射來進行賦值

Reflectiong 根據反射來進行賦值

employee print out 參數 泛型參數 ngs 獲得 res sta

package com.hu;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
* 反射的 Utils 函數集合
* 提供訪問私有變量, 獲取泛型類型 Class, 提取集合中元素屬性等 Utils 函數
* @author Administrator
*
*/
public class ReflectionUtils {


/**
* 通過反射, 獲得定義 Class 時聲明的父類的泛型參數的類型
* 如: public EmployeeDao extends BaseDao<Employee, String>
* @param clazz
* @param index
* @return
*/
@SuppressWarnings("unchecked")
public static Class getSuperClassGenricType(Class clazz, int index){
Type genType = clazz.getGenericSuperclass();

if(!(genType instanceof ParameterizedType)){
return Object.class;
}

Type [] params = ((ParameterizedType)genType).getActualTypeArguments();

if(index >= params.length || index < 0){
return Object.class;
}

if(!(params[index] instanceof Class)){
return Object.class;
}

return (Class) params[index];
}

/**
* 通過反射, 獲得 Class 定義中聲明的父類的泛型參數類型
* 如: public EmployeeDao extends BaseDao<Employee, String>
* @param <T>
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public static<T> Class<T> getSuperGenericType(Class clazz){
return getSuperClassGenricType(clazz, 0);
}

/**
* 循環向上轉型, 獲取對象的 DeclaredMethod
* @param object
* @param methodName
* @param parameterTypes
* @return
*/
public static Method getDeclaredMethod(Object object, String methodName, Class<?>[] parameterTypes){

for(Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()){
try {
//superClass.getMethod(methodName, parameterTypes);
return superClass.getDeclaredMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {
//Method 不在當前類定義, 繼續向上轉型
}
//..
}

return null;
}

/**
* 使 filed 變為可訪問
* @param field
*/
public static void makeAccessible(Field field){
if(!Modifier.isPublic(field.getModifiers())){
field.setAccessible(true);
}
}

/**
* 循環向上轉型, 獲取對象的 DeclaredField
* @param object
* @param filedName
* @return
*/
public static Field getDeclaredField(Object object, String filedName){

for(Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()){
try {
return superClass.getDeclaredField(filedName);
} catch (NoSuchFieldException e) {
//Field 不在當前類定義, 繼續向上轉型
}
}
return null;
}

/**
* 直接調用對象方法, 而忽略修飾符(private, protected)
* @param object
* @param methodName
* @param parameterTypes
* @param parameters
* @return
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public static Object invokeMethod(Object object, String methodName, Class<?> [] parameterTypes,
Object [] parameters) throws InvocationTargetException{

Method method = getDeclaredMethod(object, methodName, parameterTypes);

if(method == null){
throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + object + "]");
}

method.setAccessible(true);

try {
return method.invoke(object, parameters);
} catch(IllegalAccessException e) {
System.out.println("不可能拋出的異常");
}

return null;
}

/**
* 直接設置對象屬性值, 忽略 private/protected 修飾符, 也不經過 setter
* @param object
* @param fieldName
* @param value
*/
public static void setFieldValue(Object object, String fieldName, Object value){
Field field = getDeclaredField(object, fieldName);

if (field == null)
throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");

makeAccessible(field);

try {
field.set(object, value);
} catch (IllegalAccessException e) {
System.out.println("不可能拋出的異常");
}
}

/**
* 直接讀取對象的屬性值, 忽略 private/protected 修飾符, 也不經過 getter
* @param object
* @param fieldName
* @return
*/
public static Object getFieldValue(Object object, String fieldName){
Field field = getDeclaredField(object, fieldName);

if (field == null)
throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]");

makeAccessible(field);

Object result = null;

try {
result = field.get(object);
} catch (IllegalAccessException e) {
System.out.println("不可能拋出的異常");
}

return result;
}
}

Reflectiong 根據反射來進行賦值