本站不再更新,歡迎光臨 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開發技術網
          主站蜘蛛池模板: 宜兰市| 比如县| 甘南县| 彭阳县| 兰考县| 铜梁县| 扎鲁特旗| 乌兰察布市| 中卫市| 建湖县| 邹城市| 华安县| 马尔康县| 积石山| 渝北区| 成武县| 南丹县| 苍溪县| 新野县| 尚志市| 龙门县| 梅河口市| 长武县| 墨脱县| 灵宝市| 巴林左旗| 开远市| 康平县| 天津市| 犍为县| 东至县| 贵德县| 察哈| 田东县| 长治县| 雷山县| 福安市| 施甸县| 延吉市| 大余县| 中超|