Home > Backend Development > PHP Tutorial > Reflection call private method practice (php, java), _PHP tutorial

Reflection call private method practice (php, java), _PHP tutorial

WBOY
Release: 2016-07-12 09:02:29
Original
853 people have browsed it

Practice of reflective calling private methods (php, java),

There is a common problem in single testing, the private method in the side class cannot be called directly. During the processing, Xiaoyan changes the method permissions through reflection, conducts a single test, shares it, and directly uploads the code.

Simple test class

Generate a simple test class with only one private method.

Copy code The code is as follows:

Single test code

Copy code The code is as follows:
objMyClass = new MyClass();}/*** Use reflection to unit test the private and protect methods in the class * * @param $strMethodName string: reflection function name * @return ReflectionMethod obj: callback object*/protected static function getPrivateMethod ($strMethodName) {$objReflectClass = new ReflectionClass(self::CLASS_NAME);$method = $objReflectClass->getMethod($strMethodName);$method->setAccessible(true);return $method;}/*** @brief: Test the call of private function*/public function testPrivateFunc(){$testCase = 'just a test string';//Reflect this class$testFunc = self::getPrivateMethod('privateFunc');$res = $testFunc->invokeArgs($this ->objMyClass, array($testCase));$this->assertEquals($testCase, $res);$this->expectOutputRegex('/success/i');// Capture no parameter exception test try { $testFunc->invokeArgs($this->transfer2Pscase, array());} catch (Exception $expected) {$this->assertNotNull($expected);return true;}$this->fail(self ::FAIL);}}

Run results

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)

Key code analysis

Encapsulates a reflection call of the method of the class under test; at the same time, if the access permission of the processing method before the return method is true, you can access the private function method.

Copy code The code is as follows:
/*** Use reflection to unit test the private and protect methods in the class * * @param $strMethodName string: reflection function name * @return ReflectionMethod obj: callback object*/protected static function getPrivateMethod($strMethodName) {$objReflectClass = new ReflectionClass(self::CLASS_NAME);$method = $objReflectClass->getMethod($strMethodName);$method->setAccessible( true);return $method;}

Let me share with you how to use reflection to call another type of private method in java

We know that Java applications cannot access private methods of persistence classes, but Hibernate does not have this restriction. It can access methods at various levels, such as private, default, protected, and public. How does Hibernate implement this function? ?The answer is to use JAVA’s reflection mechanism, as follows:

<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> 

Copy after login

The output result is: ---452345234----q31234132

Articles you may be interested in:

  • Analysis of private attribute inheritance issues in PHP classes
  • Questions about private access control in PHP classes and objects
  • The difference between public, private and protected in php class and example analysis
  • A brief analysis of php object-oriented public private protected access modifier
  • Is the private modifier in Java invalid?

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084580.htmlTechArticlePractice of reflectively calling private methods (php, java), there is a common problem in single test, side class The private methods in cannot be called directly. Xiaoyan changes the method right through reflection during processing...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template