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

          評論:
          # re: java類反射例子 2008-02-27 13:18 | 有貓相伴的日子
          Object obj=cls.newInstance(); 根據class對象生成實例  回復  更多評論
            
          # re: java類反射例子[未登錄] 2013-03-08 17:47 | a
          a  回復  更多評論
            
          本站不再更新,歡迎光臨 java開發技術網
          主站蜘蛛池模板: 永年县| 宽甸| 泗水县| 山东省| 集贤县| 浦县| 林口县| 商城县| 松潘县| 扎兰屯市| 民县| 定兴县| 高淳县| 海盐县| 望江县| 广灵县| 和田县| 稷山县| 邢台市| 读书| 汉沽区| 蒲城县| 龙口市| 余姚市| 长治县| 山阳县| 瑞安市| 巴青县| 灵璧县| 三亚市| 东阳市| 洪雅县| 门头沟区| 南城县| 忻城县| 库尔勒市| 镇江市| 宣城市| 济宁市| 勃利县| 鄂托克前旗|