SAP ABAP CGLIB(Code Generation Library)的模擬實現
What is CGLIB?
A Byte Code Generation Library which is high level API to generate and transform Java byte code. It is used in various scenarios such as AOP, testing, data access frameworks to generate dynamic proxy objects and intercept field access.
See one example in unit test.
In line 17, a new dynamic proxy class is generated as mock. In line 19, we tell the proxy, “if get(0) is called on this mock class, then return mocked data “hello, world”. As a result, in line 23 “result: hello, world” will be printed out.
In debugger you can find that the variable in line 17 is mocked by CGLIB:
Its byte code is generated dynamically and stored in variable byte[] b in line 217.
Let’s see another example of injecting pre-exit and post-exit ( which ABAPers are very familiar with ) into a given method via dynamic proxy generated by CGLIB:
(1) I have a class MyMEthodExitDemo which has a normal method myFun. (2) A new dynamic proxy class is generated in method createProxy which has a method with equal name as original class plus custom enhancement covered by class JerryEnhancement.
(3) The pre-exit and post-exit are defined in class JerryEnhancement which implements interface MethodInterceptor defined in CGLIB library. The original method is generated in line 14, with pre-exit before it ( line 13 ) and post-exit after it ( line 15 ).
Execute result:
How does this example work under the hood?
I implement a demo in ABAP with the same logic.
(1) I have a very simple ABAP class with only one public method which will print “Hello World”:
(2) The CGLIB utility class in line 5 simulates the logic in Java, which will construct a proxy class based on existing global class ZCL_JAVA_CGLIB. The generated proxy class will be DYNAMICALLY injected with two enhancement implemented by zcl_jerry_preexit and zcl_jerry_postexit.
There are two interfaces defined for pre-exit and post-exit logics which contain only one EXECUTE method without any parameters:
And zcl_jerry_preexit and zcl_jerry_postexit implement these two interfaces accordingly:
once method greet in line 16 is called, the enhanced version of method greet is called:
By this way, the method greet is enhanced in a non-invasive approach – no modification on original class, no new class are persisted in repository.
The method get_proxy of zcl_abap_cglib_tool does the magic, I just follow the idea of CGLIB implementation in Java:
The pre-exit and post-exit passed by consumer are dynamically injected into proxy class here:
Till now you should be familiar with CGLIB idea, and it is quite easy to understand why it is NOT possible to create a proxy class based on a final class by CGLIB, simply because a final class could not be subclassed.
The report to consume it:
REPORT zcglib_proxy_generate.DATA: lo_proxy TYPE REF TO object.CALL METHOD zcl_abap_cglib_tool=>get_proxy
EXPORTING
iv_class_name = 'ZCL_JAVA_CGLIB'
io_pre_exit = NEW zcl_jerry_preexit( )
io_post_exit = NEW zcl_jerry_postexit( )
RECEIVING
ro_proxy = lo_proxy.CHECK lo_proxy IS NOT INITIAL.DATA(lo_class) = CAST zcl_java_cglib( lo_proxy ).lo_class->greet( ).
Update on 2017-02-02 CGLIB use scenario
The CGLIB is widely used in many Java framework, for example Mockito as a unit test framework. See my another blog Simulate Mockito in ABAP for detail.
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2713820/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Java的Covariance設計原理和SAP ABAP的模擬實現Java
- SAP ABAP Gateway Client 的 ABAP 實現,重用 HTTP ConnectionGatewayclientHTTP
- Implement CGLIB in ABAPCGLib
- 在SAP ABAP裡使用註解@Inject模擬Java SpringJavaSpring
- SAP ABAP和Java的動態代理實現Java
- SAP ABAP OData 服務的 $count 操作實現
- promise的模擬實現Promise
- library cache pin等待事件的模擬事件
- SAP ABAP:獲取匯率的完整實現之一
- js模擬實現hashCode()方法程式碼例項JS
- javascript模擬new的實現JavaScript
- JavaScript 模擬new的實現JavaScript
- 等待模擬-library cache 軟解析
- javascript模擬實現replaceAll()JavaScript
- svpwm的matlab模擬實現Matlab
- strlen函式的模擬實現函式
- qsort的模擬實現和練習
- JavaScript深入之new的模擬實現JavaScript
- JavaScript深入之bind的模擬實現JavaScript
- JavaScript 深入之new的模擬實現JavaScript
- JavaScript 深入之bind的模擬實現JavaScript
- 【Java】——模擬登入實現Java
- JavaScript模擬實現replaceAll方法JavaScript
- SAP ABAP 效能優化實踐優化
- js實現的模擬滾動條效果JS
- 模擬STL連結串列類的實現
- 淺談 SAP ABAP 系統裡的 ALV 輸出方式實現
- 等待模擬-library cache shared pool 硬解析
- 特效實現用查表法實現對水波的模擬(轉)特效
- 模擬實現apply/call/bindAPP
- bind,call,apply模擬實現APP
- JavaScript中模擬實現jsonpJavaScriptJSON
- 模擬實現簡易版shell
- js模擬實現列舉效果JS
- 簡單的string類的模擬實現
- 實戰模擬│單點登入 SSO 的實現
- 三維模擬模擬如何實現精益工廠佈局?
- 用ABAP模擬JavaScript的柯里化語言特性(Curry)JavaScript