<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    本站不再更新,歡迎光臨 java開發技術網
    隨筆-230  評論-230  文章-8  trackbacks-0
    package util;

    import java.lang.reflect.Field;
    import java.lang.reflect.Method;

    import test.web.example.PP;
    /**
     * 
     * 
    @author peidw
     *
     
    */

    public class Reflection {
        
        
    /**
         * 取得參數對象中的公共屬性
         * 
    @param obj
         * 
    @param fieldname
         * 
    @return
         * 
    @throws Exception
         
    */
        
    public Object getProperty(Object obj,String fieldname)throws Exception{
            Object result
    =null;
            Class objClass
    =obj.getClass();
            Field field
    =objClass.getField(fieldname);
            result
    =field.get(obj);
            
    return result;
        }
        
    /**
         * 獲得某類的靜態屬性
         * 
    @param className
         * 
    @param fieldName
         * 
    @return
         * 
    @throws Exception
         
    */
        
    public Object getStaticProperty(String className,String fieldName)throws Exception{
            Class cls
    =Class.forName(className);
            Field field
    =cls.getField(fieldName);
            Object provalue
    =field.get(cls);
            
    return provalue;
        }
        
    /**
         * 獲取參數對象的屬性值
         * 
    @param obj
         * 
    @param propertyName
         * 
    @return
         * 
    @throws Exception
         
    */
        
    public Object getPrivatePropertyValue(Object obj,String propertyName)throws Exception{
            Class cls
    =obj.getClass();
            Field field
    =cls.getDeclaredField(propertyName);
            field.setAccessible(
    true);
            Object retvalue
    =field.get(obj);
            
    return retvalue;
        }
        
        
    /**
         * 執行某對象的方法
         * 
    @param owner
         * 
    @param methodName
         * 
    @param args
         * 
    @return
         * 
    @throws Exception
         
    */
        
    public Object invokeMethod(Object owner,String methodName,Object[] args)throws Exception{
            Class cls
    =owner.getClass();
            Class[] argclass
    =new Class[args.length];
            
    for(int i=0,j=argclass.length;i<j;i++){
                argclass[i]
    =args[i].getClass();
            }
            Method method
    =cls.getMethod(methodName,argclass);
            
    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 cls
    =Class.forName(className);
            Class[] argclass
    =new Class[args.length];
            
    for(int i=0,j=argclass.length;i<j;i++){
                argclass[i]
    =args[i].getClass();
            }
            Method method
    =cls.getMethod(methodName,argclass);
            
    return method.invoke(null, args);
        }
        
    public Object newInstance(String className,Object[] args)throws Exception{
            Class clss
    =Class.forName(className);
        
            Class[] argclass
    =new Class[args.length];
            
    for(int i=0,j=argclass.length;i<j;i++){
                argclass[i]
    =args[i].getClass();
            }
            java.lang.reflect.Constructor cons
    =clss.getConstructor(argclass);
            
    return cons.newInstance();
        }
        
    public static void main(String[] args)throws Exception{
            Reflection rl
    =new Reflection();
            PP p
    =new PP();
            rl.getPrivatePropertyValue(p,
    "aname");
        }
    }
    測試類
    package test.web.example;

    import static org.junit.Assert.*;

    import org.junit.Before;
    import org.junit.Test;
    import org.junit.Assert.*;
    import util.Reflection;

    /**
     * 
     * 
    @author peidw
     *
     
    */
    public class ReflectionTest {
        
        
    private PP pp=null;
        
    private Reflection reflection=null;
        @Before
        
    public void setUp() throws Exception {
            reflection
    =new Reflection();
            pp
    =new PP();
            
        }

        @Test
        
    public void testGetProperty() throws Exception{
            System.out.println(pp);
            System.out.println(reflection);
            System.out.println(reflection.getProperty(pp, 
    "address"));
            
    //assertEquals(reflection.getProperty(pp, "address"), null);
            
        }

        @Test
        
    public void testGetStaticProperty()throws Exception{
            assertEquals(reflection.getStaticProperty(
    "test.web.example.PP""count"),"love you");
        }

        @Test
        
    public void testGetPrivatePropertyValue() throws Exception{
            assertEquals(reflection.getPrivatePropertyValue(pp, 
    "aname"),null);
        }

        @Test
        
    public void testInvokeMethod() throws Exception{
            assertEquals(reflection.invokeMethod(pp, 
    "setAddress"new Object[]{"合浦西場鎮"}),null);
            assertEquals(reflection.getProperty(pp, 
    "address"), "合浦西場鎮");
        }

        @Test
        
    public void testInvokeStaticMethod()throws Exception {
            assertEquals(reflection.invokeStaticMethod(
    "test.web.example.PP""changeCount",new Object[]{"80"}),true);
            assertEquals(reflection.getStaticProperty(
    "test.web.example.PP""count"),"80");
        }

        @Test
        
    public void testNewInstance() throws Exception{
            
            Object vpp
    =reflection.newInstance("test.web.example.PP",new Object[]{"peidw","廣州"});
            org.junit.Assert.assertNotNull(vpp);
            
    //assertEquals(reflection.getPrivatePropertyValue(vpp,"aname"),"peidw");
        }



    }
    posted on 2007-08-29 20:47 有貓相伴的日子 閱讀(10397) 評論(2)  編輯  收藏 所屬分類: jdk

    評論:
    # re: java類反射例子 2008-02-27 13:18 | 有貓相伴的日子
    Object obj=cls.newInstance(); 根據class對象生成實例  回復  更多評論
      
    # re: java類反射例子[未登錄] 2013-03-08 17:47 | a
    a  回復  更多評論
      
    本站不再更新,歡迎光臨 java開發技術網
    主站蜘蛛池模板: 白白色免费在线视频| 国产亚洲综合久久| 免费看成人AA片无码视频吃奶| 免费在线一级毛片| 成年大片免费视频播放一级| 亚洲精品无码你懂的网站| 乱淫片免费影院观看| 日本亚洲国产一区二区三区| 十八禁视频在线观看免费无码无遮挡骂过 | 久久精品人成免费| 亚洲精品福利网站| 丁香花在线观看免费观看| 亚洲人成网站在线在线观看 | 亚洲 自拍 另类小说综合图区| 免费看内射乌克兰女| 国产精品V亚洲精品V日韩精品| 本免费AV无码专区一区| 亚洲a一级免费视频| 一色屋成人免费精品网站 | 自拍偷自拍亚洲精品情侣| a级毛片免费播放| 亚洲午夜久久久精品影院| 日韩精品无码区免费专区| 相泽南亚洲一区二区在线播放| 亚洲七七久久精品中文国产| 成全在线观看免费观看大全 | 免费观看国产精品| 中国毛片免费观看| 亚洲午夜精品在线| 内射无码专区久久亚洲| 国产精品免费福利久久| 亚洲欧美日韩中文无线码 | 亚洲熟妇成人精品一区| 毛茸茸bbw亚洲人| 59pao成国产成视频永久免费| 亚洲AV无码一区二区一二区| 亚洲色成人中文字幕网站| 美女被免费喷白浆视频| 一级毛片免费不卡直观看| 亚洲精品电影在线| 亚洲AV无码专区日韩|