java反射应用

package test;

import java.sql.Date;
import java.util.List;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.commons.beanutils.PropertyUtils;

public class TestC {

    public static void main(String[] args) {
        try {
            //根据指定的路径获得class对象
            Class<?> clazz =Class.forName("test.TestClass");
            //将class对象转成Object对象
            Object obj = clazz.newInstance();
            //这里用的是apache包的一个反射的帮助类,setSimpleProperty(对象,属性名,属性值)就可以赋值
            PropertyUtils.setSimpleProperty(obj, "age", "21");
            PropertyUtils.setSimpleProperty(obj, "name", "张三");
            //根据方法名称获得方法getMethod(传方法的名称)
            Method method = obj.getClass().getMethod("testReflect");
            //执行方法,无参时
            method.invoke(obj);
            
            //当有参数时getMethod(方法名称,参数的class对象)
            Method methodB = obj.getClass().getMethod("testReflect",String.class);
            //执行方法
            methodB.invoke(obj,new Object[]{"abc"});
            
            
            //当不知道要传哪些参数时,比如13465,张三,男,当前时间
            //因为下面要用getMethod(方法名,class对象数组),所以先在这里获取
            Class[] cls = getClass(Integer.parseInt("13465"),"张三","男",new Date(System.currentTimeMillis()));
            
//            传入的最好是引用类型的参数,不然还有再转一下,麻烦!,原因如下
//            Integer i = 0;
//            i.getClass();//可以点出相应的方法
//            int j = 0;
//            j.//没经过封装点不了
            
            Method methodC = obj.getClass().getMethod("testReflect",cls);
            //invoke(对象,要传入的值)
            methodC.invoke(obj,Integer.parseInt("13465"),"张三","男",new Date(System.currentTimeMillis()));
            
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        
    }
    
    public static Class[] getClass(Object... propertys){
        Class[] cls = new Class[propertys.length];
        for (int i = 0; i < propertys.length; i++) {
            cls[i]= propertys[i].getClass();
        }
        return cls;
    }
    
}

 

package test;

import java.sql.Date;

public class TestClass{
    
    private String name;
    private String age;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    
    public void testReflect(){
        System.out.println(getName()+getAge());
    }
    
    public void testReflect(String stu){
        System.out.println(stu);
    }
    
    public void testReflect(Integer i,String stu,String t,Date date){
        System.out.println(i+" "+stu+" "+t+" "+date);
    }
    
}

 

posted on 2013-10-18 18:24  定数  阅读(433)  评论(0)    收藏  举报

导航