Spring原理自我实践之IOC的模拟实现
2009-09-18 00:00:00 来源:WEB开发网Class_3:容器生成对象时用到的反射类
Java代码
package com.java.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectHelper {
/**
* 获得对象的实例变量值
*
* @param owner
* 对象实例
* @param name
* 实例变量的名称
* @return 返回实例变量的值
*/
public static Object getProperty(Object owner, String name)
throws Exception {
Class ownerClass = owner.getClass();
Field field = ownerClass.getField(name);
return field.get(owner);
}
/**
* 获得类的静态变量值
*
* @param className
* 类的名称(包含包路径)
* @param fieldName
* 静态变量的名称
* @return 返回静态变量的值
* @throws Exception
*/
public static Object getStaticProperty(String className, String fieldName)
throws Exception {
Class classInfo = Class.forName(className);
Field field = classInfo.getField(fieldName);
return field.get(null);
}
/**
* 执行实例对象的方法
*
* @param owner
* 实例对象
* @param methodName
* 方法名称
* @param args
* 方法的参数
* @return 返回所执行方法的返回值
* @throws Exception
*/
public static Object invokeMethod(Object owner, String methodName,
Object[] args) throws Exception {
Class ownerClass = owner.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0; i < args.length; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
return method.invoke(owner, args);
}
/**
* 调用静态方法
*
* @param className
* 类的名称(包含包路径)
* @param methodName
* 静态方法的名称
* @param args
* 方法的参数
* @return 执行静态方法的返回值
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static Object invokeStaticMethod(String className,
String methodName, Object[] args) throws Exception {
Class ownerClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0; i < args.length; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
return method.invoke(null, args);
}
/**
* 创建一个新实例
*
* @param className
* 类的名称(包含包路径)
* @param args
* 构造器的参数
* @return 返回新创建的实例对象
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static Object newInstance(String className, Object[] args)
throws Exception {
Class newObjClass = Class.forName(className);
if (args != null) {
Class[] argsClass = new Class[args.length];
for (int i = 0; i < args.length; i++) {
argsClass[i] = args[i].getClass();
}
Constructor cons = newObjClass.getConstructor(argsClass);
return cons.newInstance(args);
} else {
Constructor cons = newObjClass.getConstructor(null);
return cons.newInstance(args);
}
}
/**给没有set方法的属性赋值
* @param object
* @param fieldName
* @param value
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void setFieldValue(final Object object,final String fieldName,final Object value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
Field field=object.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(object, value);
}
@SuppressWarnings("unused")
private static String getMethodNameByFieldName(String fieldName) {
return "set" + fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
}
}
更多精彩
赞助商链接