Some method to operate private member by java reflect

Sometimes, we need operate private member from outer without modifying source , we canuse java reflect to do it , The document will introduce how to do it.

 

Get  private inner class instance and exec itsfunction

Samplecode like below:

public class StorageServerPmTraffic {
	//…
     static class MySettingListener implements MTValueSettingFailureListener
    {
        static MTValueSettingFailureInfo failedInfo;

        public void notifyValueSettingFailureResult(MTValueSettingFailureInfo result) 
        {		
//…
	   }
    }
    //…
}

we shall execute notifyValueSettingFailureResult , at first , we should get a instance of outer class (StorageServerPmTraffic)and get its inner class (MySettingListener)

//new outer class instance
StorageServerPmTraffic sspt = new StorageServerPmTraffic(); 
//get inner class handle
Class<?> cInner = sspt.getClass().getDeclaredClasses()[0];

cInnerrepresent inner class MySettingListener.  And then , get inner class  construct func and set its accessible to true:

Constructor<?> c = cInner.getDeclaredConstructors()[0]; 
//let me have access right
c.setAccessible(true);
fetchfunction we want to execute , we care “notifyValueSettingFailureResult” :

// get func (notifyValueSettingFailureResult) handle , arg1 is func name you want to get , arg2 is the func’s argument type
method = cInner.getDeclaredMethod(methodName, MTValueSettingFailureInfo.class); 
//execute MySettingListener.notifyValueSettingFailureResult
method.invoke(c.newInstance(new Object[]{}) , info);
  

invokewill execute inner class func ,  arg 1 isinstance of  inner class, arg 2 is innerclass func’s arg ( MySettingListener.notifyValueSettingFailureResult)

 

 

Get privatefield value and set it

Inthis condition , we try to get private member variable , and reset its value , itis another way to get class handle  , codelike below:

package ericsson.ipworks.storage.server.cba.pm;

//…
public class StorageServerPmTraffic {

    private static Gauge EnumDnSchedNumbergauge;
private static Gauge EnumDnRangeNumbergauge;
//…
}

We shall get EnumDnSchedNumbergauge , first , we use absolute class path to getclass handle 

cls = Class.forName("ericsson.ipworks.storage.server.cba.pm.StorageServerPmTraffic");

and then , get Specified field handle , set its accessible to true:

Field field = cls.getDeclaredField("EnumDnSchedNumbergauge");
field.setAccessible(true);

getfield value , it is a object type:

Object   Schedgauge = field.get(cls);

We can enforce to cast it to Gauge type like below:

Gauge result = (Gauge)Schedgauge;

Ishall set the result to EnumDnRangeNumbergauge , get its field handle:

Field fieldRange = cls.getDeclaredField("EnumDnRangeNumbergauge");
fieldRange.setAccessible(true);

setfieldRange to result , arg1 is StorageServerPmTraffic instance you want to set

fieldRange.set(sspt, result);






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值