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

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

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

    本站不再更新,歡迎光臨 java開發(fā)技術(shù)網(wǎng)
    隨筆-230  評(píng)論-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 {
        
        
    /**
         * 取得參數(shù)對(duì)象中的公共屬性
         * 
    @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;
        }
        
    /**
         * 獲得某類的靜態(tài)屬性
         * 
    @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;
        }
        
    /**
         * 獲取參數(shù)對(duì)象的屬性值
         * 
    @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;
        }
        
        
    /**
         * 執(zhí)行某對(duì)象的方法
         * 
    @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);
        }
        
        
    /**
         * 執(zhí)行靜態(tài)類的方法
         * 
    @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");
        }
    }
    測(cè)試類
    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[]{"合浦西場(chǎng)鎮(zhèn)"}),null);
            assertEquals(reflection.getProperty(pp, 
    "address"), "合浦西場(chǎng)鎮(zhèn)");
        }

        @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) 評(píng)論(2)  編輯  收藏 所屬分類: jdk

    評(píng)論:
    # re: java類反射例子 2008-02-27 13:18 | 有貓相伴的日子
    Object obj=cls.newInstance(); 根據(jù)class對(duì)象生成實(shí)例  回復(fù)  更多評(píng)論
      
    # re: java類反射例子[未登錄] 2013-03-08 17:47 | a
    本站不再更新,歡迎光臨 java開發(fā)技術(shù)網(wǎng)
    主站蜘蛛池模板: 一个人免费观看视频www | 亚洲精品无码高潮喷水A片软| 亚洲一级片免费看| 免费av欧美国产在钱| 亚洲区小说区图片区QVOD| 男女啪啪永久免费观看网站| 69精品免费视频| 人妻在线日韩免费视频| 久久综合亚洲色hezyo| 亚洲国产成人九九综合| 亚洲精品国产成人99久久| 亚洲一区爱区精品无码| 亚洲成A人片在线观看中文| 成全影视免费观看大全二| 亚洲免费人成视频观看| 久久精品一区二区免费看| 成全视频在线观看免费| 久久久久久av无码免费看大片| 瑟瑟网站免费网站入口| avtt天堂网手机版亚洲| 亚洲精品免费在线视频| 亚洲日本中文字幕| 亚洲好看的理论片电影| 亚洲精品无码专区久久久 | 国内成人精品亚洲日本语音| 久久亚洲最大成人网4438| 亚洲成人网在线播放| 99人中文字幕亚洲区| 亚洲精品无码不卡| 日产亚洲一区二区三区| 亚洲高清无在码在线无弹窗| 色婷婷亚洲十月十月色天| 久久精品九九亚洲精品| 久久久亚洲AV波多野结衣| 在线电影你懂的亚洲| 亚洲欧洲国产精品久久| 亚洲精品国产情侣av在线| 亚洲伊人久久大香线焦| 亚洲熟女综合一区二区三区| 亚洲精品av无码喷奶水糖心| 亚洲av成本人无码网站|