2007-05-14
[常用代码整理]JAVA反射
关键字: JAVA反射
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
通过反射调用setName方法为name赋值:
public class Main {
public static void main(String[] args) {
try {
Class clazz = Class.forName("test.User");
Method mtd = clazz.getMethod("setName", new Class[]{String.class});
Object obj = (Object) clazz.newInstance();
mtd.invoke(obj,new Object[]{"Jacky"});
System.out.println(((User)obj).getName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
********************************************************************
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Java Reflection Cookbook
*
* @author Michael Lee
* @since 2006-8-23
* @version 0.1a
*/
public class Reflection {
/**
* 得到某个对象的公共属性
*
* @param owner, fieldName
* @return 该属性对象
* @throws Exception
*
*/
public Object getProperty(Object owner, String fieldName) throws Exception {
Class ownerClass = owner.getClass();
Field field = ownerClass.getField(fieldName);
Object property = field.get(owner);
return property;
}
/**
* 得到某类的静态公共属性
*
* @param className 类名
* @param fieldName 属性名
* @return 该属性对象
* @throws Exception
*/
public Object getStaticProperty(String className, String fieldName)
throws Exception {
Class ownerClass = Class.forName(className);
Field field = ownerClass.getField(fieldName);
Object property = field.get(ownerClass);
return property;
}
/**
* 执行某对象方法
*
* @param owner
* 对象
* @param methodName
* 方法名
* @param args
* 参数
* @return 方法返回值
* @throws Exception
*/
public Object invokeMethod(Object owner, String methodName, Object[] args)
throws Exception {
Class ownerClass = owner.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; 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
*/
public 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, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
return method.invoke(null, args);
}
/**
* 新建实例
*
* @param className
* 类名
* @param args
* 构造函数的参数
* @return 新建的实例
* @throws Exception
*/
public Object newInstance(String className, Object[] args) throws Exception {
Class newoneClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Constructor cons = newoneClass.getConstructor(argsClass);
return cons.newInstance(args);
}
/**
* 是不是某个类的实例
* @param obj 实例
* @param cls 类
* @return 如果 obj 是此类的实例,则返回 true
*/
public boolean isInstance(Object obj, Class cls) {
return cls.isInstance(obj);
}
/**
* 得到数组中的某个元素
* @param array 数组
* @param index 索引
* @return 返回指定数组对象中索引组件的值
*/
public Object getByArray(Object array, int index) {
return Array.get(array,index);
}
}
发表评论
- 浏览: 58355 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
我的相册
image008
共 11 张
共 11 张
最近加入圈子
最新评论
-
Struts2学习笔记4 ――国 ...
统一使用utf-8编码,properties文件中的中文会变编译为utf-8编码 ...
-- by ember_319 -
javascript的消息资源国际 ...
我的做法是在产品初次启动的时候去过滤JS文件,然后使用Spring标签国际化。不 ...
-- by longleg -
javascript的消息资源国际 ...
navigator.userLanguage 这个只能获取 浏览器 设置的 L ...
-- by wucc1986 -
Struts2学习笔记4 ――国 ...
我在做Struts2的国际化的时候将properties文件中的中文全部用nat ...
-- by fornever -
Rails生成Ext Tree
无明 写道nested tree有2个缺点比较明显: 1、单纯的nested t ...
-- by rainlife






评论排行榜