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

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

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.

The code is as follows:
<?php/** * Basic template for Cui Xiaohuan’s single test. * * @author cuihuan * @date 2015/11/12 22:15:31 * @version $Revision:1.0$ **/class MyClass {/** * Private method* * @param $params * @return bool */ private function privateFunc($params){if(!isset($params)){return false;}echo "test success";return $params;}}

Unit test 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’);//Catch 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.

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:

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

 

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?

http://www.bkjia.com/PHPjc/1084580.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1084580.htmlTechArticlePractice of reflectively calling private methods (php, java). There is a common problem in single testing. The private method in the side class cannot be called directly. Xiaoyan changes method rights through reflection during processing…

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/reflection-call-private-method-practice-php-java-_php-tutorial-2/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索