本站不再更新,歡迎光臨 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 有貓相伴的日子 閱讀(10400) 評論(2)  編輯  收藏 所屬分類: jdk

          評論:
          # re: java類反射例子 2008-02-27 13:18 | 有貓相伴的日子
          Object obj=cls.newInstance(); 根據class對象生成實例  回復  更多評論
            
          # re: java類反射例子[未登錄] 2013-03-08 17:47 | a
          a  回復  更多評論
            
          本站不再更新,歡迎光臨 java開發技術網
          主站蜘蛛池模板: 北宁市| 大庆市| 临泉县| 铁岭县| 南部县| 武汉市| 桃江县| 车险| 社旗县| 孟连| 微山县| 武隆县| 常德市| 定结县| 阿鲁科尔沁旗| 遂平县| 馆陶县| 南皮县| 桐城市| 逊克县| 界首市| 涟水县| 灌南县| 通渭县| 孙吴县| 沙雅县| 平南县| 夹江县| 万州区| 双鸭山市| 武清区| 射洪县| 西丰县| 双峰县| 喀什市| 容城县| 宜兴市| 东阳市| 平远县| 彭水| 灵宝市|