java反射的例子
- package cn.test;
- 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 TestReflection {
- /**
- * 得到某个对象的公共属性
- *
- * @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);
- }
- }
- package cn.test;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import cn.IpUtils.IpBean;
- public class TestObject {
- /**
- * 设置属性值
- * @param list
- * @param cla
- * @return
- */
- public ArrayList array2bean(ArrayList list, Class cla) {
- ArrayList result = new ArrayList();
- int filed_len = cla.getDeclaredFields().length;
- System.out.println(":"+cla.getDeclaredFields().length);
- for (int i = 0; i < list.size(); i++) {
- Object[] o = (Object[]) list.get(i);
- int length = filed_len < o.length ? filed_len : o.length;
- try {
- result.add(cla.newInstance());
- for (int j = 0; j < length; j++) {
- Method m = null;
- String mName =cla.getDeclaredFields()[j].getName();
- mName = mName.substring(0, 1).toUpperCase()+ mName.substring(1);
- mName = "set" + mName;
- m = cla.getMethod(mName, new Class[] { String.class });
- //调用设置的方法,给属性赋值
- m.invoke(result.get(i), new Object[] { o[j] == null ? "": o[j].toString() });
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return result;
- }
- /**
- * 获取get的取值
- * @param obj
- * @return
- * @throws Exception
- */
- public String getObjectToString(Object obj) throws Exception{
- Class cla=obj.getClass();
- Method[] ma=cla.getDeclaredMethods();//获取所有声明的方法数组
- Method method=null;
- String methodName=null;
- Object returnValue=null;
- for(int i=0;i<ma.length;i++){
- method=ma[i];
- methodName=method.getName();//方法名
- if(methodName.indexOf("get")==0){//以get开始的方法,排除set方法
- returnValue=method.invoke(obj, null);//调用方法,相当于执行get方法得到的结果,这里返回的是一个对象
- System.out.print(methodName+"::");
- System.out.println(returnValue==null?"":returnValue.toString());
- }
- }
- return "";
- }
- /**
- * 获取对象的属性值,含有get方法的
- * @param obj
- * @return
- * @throws NoSuchMethodException
- * @throws SecurityException
- * @throws InvocationTargetException
- * @throws IllegalAccessException
- * @throws IllegalArgumentException
- */
- public String getAttributeValue(Object obj) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
- Class cla=obj.getClass();
- Field[] fds=cla.getDeclaredFields();
- Field field=null;
- String fieldName=null;
- String methodName=null;
- Method method=null;
- Object returnValue=null;
- for(int i=0;i<fds.length;i++){
- field=fds[i];
- fieldName=field.getName();
- methodName="get"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1);
- method=cla.getDeclaredMethod(methodName, null);
- returnValue=method.invoke(obj, null);//调用方法,相当于执行get方法得到的结果,这里返回的是一个对象
- System.out.print(methodName+"::");
- System.out.println(returnValue==null?"":returnValue.toString());
- }
- return "";
- }
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- // IpBean.class
- TestObject to=new TestObject();
- IpBean ib=new IpBean();
- ib.setFrom("from1");
- ib.setPosition("position1");
- ib.setTo("to1");
- ib.setId(10);
- to.getObjectToString(ib);
- // to.getAttributeValue(ib);
- }
- }