欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

反射調(diào)用private方法實(shí)踐(php、java)

 更新時(shí)間:2015年12月21日 15:29:47   投稿:mrr  
這篇文章主要介紹了反射調(diào)用private方法實(shí)踐(php、java)的相關(guān)資料,需要的朋友可以參考下

單測中有個(gè)普遍性的問題,被側(cè)類中的private方法無法直接調(diào)用。小拽在處理過程中通過反射改變方法權(quán)限,進(jìn)行單測,分享一下,直接上代碼。

簡單被測試類

生成一個(gè)簡單的被測試類,只有個(gè)private方法。

復(fù)制代碼 代碼如下:

<?php/** * 崔小渙單測的基本模板。 * * @author cuihuan * @date 2015/11/12 22:15:31 * @version $Revision:1.0$ **/class MyClass {/** * 私有方法 * * @param $params * @return bool */private function privateFunc($params){if(!isset($params)){return false;}echo "test success";return $params;}}

單測代碼

復(fù)制代碼 代碼如下:

<?php/*************************************************************************** * * $Id: MyClassTest T,v 1.0 PsCaseTest cuihuan Exp$ * **************************************************************************//** * 崔小渙單測的基本模板。 * * @author cuihuan * @date 2015/11/12 22:09:31 * @version $Revision:1.0$ **/require_once ('./MyClass.php');class MyClassTest extends PHPUnit_Framework_TestCase {const CLASS_NAME = 'MyClass';const FAIL  = 'fail';protected $objMyClass;/** * @brief setup: Sets up the fixture, for example, opens a network connection. * * 可以看做phpunit的構(gòu)造函數(shù) */public function setup() {date_default_timezone_set('PRC');$this->objMyClass = new MyClass();}/** * 利用反射,對(duì)類中的private 和 protect 方法進(jìn)行單元測試 * * @param $strMethodName string :反射函數(shù)名 * @return ReflectionMethod obj  :回調(diào)對(duì)象 */protected static function getPrivateMethod($strMethodName) {$objReflectClass = new ReflectionClass(self::CLASS_NAME);$method = $objReflectClass->getMethod($strMethodName);$method->setAccessible(true);return $method;}/** * @brief :測試private函數(shù)的調(diào)用 */public function testPrivateFunc(){$testCase = 'just a test string';// 反射該類$testFunc = self::getPrivateMethod('privateFunc');$res = $testFunc->invokeArgs($this->objMyClass, array($testCase));$this->assertEquals($testCase, $res);$this->expectOutputRegex('/success/i');// 捕獲沒有參數(shù)異常測試try { $testFunc->invokeArgs($this->transfer2Pscase, array());} catch (Exception $expected) {$this->assertNotNull($expected);return true;}$this->fail(self::FAIL);}}

運(yùn)行結(jié)果

cuihuan:test cuixiaohuan$ phpunit MyClassTest.php PHPUnit 4.8.6 by Sebastian Bergmann and contributors.Time: 103 ms, Memory: 11.75MbOK (1 test, 3 assertions)

關(guān)鍵代碼分析

封裝了一個(gè),被測類方法的反射調(diào)用;同時(shí),返回方法之前處理方法的接入權(quán)限為true,便可以訪問private的函數(shù)方法。

復(fù)制代碼 代碼如下:

/** * 利用反射,對(duì)類中的private 和 protect 方法進(jìn)行單元測試 * * @param $strMethodName string :反射函數(shù)名 * @return ReflectionMethod obj  :回調(diào)對(duì)象 */protected static function getPrivateMethod($strMethodName) {$objReflectClass = new ReflectionClass(self::CLASS_NAME);$method = $objReflectClass->getMethod($strMethodName);$method->setAccessible(true);return $method;}

下面給大家分享java中利用反射調(diào)用另一類的private方法

我們知道,Java應(yīng)用程序不能訪問持久化類的private方法,但Hibernate沒有這個(gè)限制,它能夠訪問各種級(jí)別的方法,如private, default, protected, public. Hibernate是如何實(shí)現(xiàn)該功能的呢?答案是利用JAVA的反射機(jī)制,如下: 

<span style="font-size:14px;">import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 
public class ReflectDemo { 
 public static void main(String[] args) throws Exception { 
  Method method = PackageClazz.class.getDeclaredMethod("privilegedMethod", new Class[]{String.class,String.class});  
  method.setAccessible(true); 
  method.invoke(new PackageClazz(), "452345234","q31234132"); 
 } 
} 
class PackageClazz { 
 private void privilegedMethod(String invokerName,String adb) { 
  System.out.println("---"+invokerName+"----"+adb); 
 } 
}</span> 

輸出結(jié)果為:---452345234----q31234132

相關(guān)文章

最新評(píng)論