java类反射例子

 
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");
    }



}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值