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的原创文章,请关注公众号"汪子熙":
相关推荐
资源名称:sap abap开发从入门到精通 内容简介: 《SAP ABAP开发从入门到精通》以应用实例的形式,讲解了SAPABAP开发的相关知识点,详细介绍了SAPABAP开发者必须掌握的包与变更传输系统(CTS)、数据类型、数据库...
SAP ABAP(Advanced Business Application Programming)是SAP公司推出的一种高级业务应用编程语言,用于开发在SAP R/3和SAP NetWeaver平台上的应用程序。这些电子书提供了全面的资源,帮助学习者深入理解并掌握SAP ...
《SAP ABAP开发从入门到精通》以应用实例的形式,讲解了SAP ABAP开发的相关知识点,详细介绍了SAP ABAP开发者必须掌握的包与变更传输系统(CTS)、数据类型、数据库、模块化程序、内表、调试、ABAP数据字典、锁对象...
SAP ABAP(Advanced Business Application Programming)是SAP系统中的编程语言,用于开发和定制企业应用程序。对于想要深入理解SAP编程的初学者,实战SAP程序开发——从实例学SAP ABAP编程这个资源提供了宝贵的实践...
文件名为"SAP ABAP CODE下载-1.4.4.txt"可能是一个文本文件,其中包含了这些源代码的清单或者链接,供开发者参考和下载。 在实际应用中,SAP ABAP的开发者可能会使用SE80交易码来访问和编辑代码,或者通过ABAP ...
### SAP ABAP 常用T-CODE 在SAP系统中,T-CODE(Transaction Code)是用户执行特定操作时使用的代码。这些代码能够直接引导用户到预定义的业务功能或工具,对于SAP ABAP开发人员而言,熟练掌握一系列关键的T-CODE尤...
《SAP ABAP实用程序开发攻略》这篇文档主要向初学者介绍了SAP ABAP语言以及其在SAP系统开发中的应用,涵盖从基础知识到数据类型和开发工具的详细解释。 首先,文档开篇提到SAP技术架构及发展历程,这为读者提供了一...
SAP ABAP开发学习——第9课:ALV2(视频教程) 1.SAP ABAP开发入门学习 2.含视频教程学习、PPT、作业 3.视频非常清晰
"SAP ABAP OO实现ALV框可编辑" 在SAP ABAP中,实现ALV框的可编辑功能是非常重要的。本文将详细介绍如何使用面向对象编程(Object-Oriented Programming,OOP)在SAP ABAP中实现ALV框的可编辑功能。 首先,我们需要...
ABAP(Advanced Business Application Programming)是SAP公司推出的一种编程语言,主要用于开发SAP系统中的业务应用程序。SAP系统在全球企业资源规划(ERP)领域有着广泛的应用,而ABAP则是其核心开发语言,使得...
《SAP ABAP开发技术详解(实例篇)(第二版) [1] 》除了对对SAP ABAP开发所需的常用技术(SAP报表、批量导入、对话程序、函数)做了详尽的介绍,包括基本报表、交互式报表、复杂报表、ALV(SAP List Viewer)报表、...
在本课程“SAP ABAP开发学习——第19课:实战讲解”中,我们将深入探讨SAP ABAP(Advanced Business Application Programming)编程语言在实际项目中的应用。ABAP是SAP系统的核心开发语言,用于构建企业级业务应用...
《SAP ABAP开发详解与高端应用》这本书深入探讨了SAP ABAP(Advanced Business Application Programming)编程语言,这是SAP系统中用于开发业务应用程序的关键技术。ABAP是SAP平台上的核心编程语言,用于创建自定义...
SAP ABAP 代码备份下载到本地程序代码
在SAP系统中,有时需要实现自定义的邮件发送功能以满足特定的业务...通过以上步骤,我们可以实现SAP ABAP程序启动Outlook并发送带有用户签名的邮件,同时邮件会记录在用户的Outlook发件箱中,满足了业务部门的需求。
SAP ABAP Query 是 SAP 系统中一种用于创建自定义报表的强大工具,尤其适合那些对 SQL 不太熟悉或者没有数据库直接访问权限的用户。它提供了丰富的功能,使得开发人员可以构建复杂的数据查询,而无需编写大量的 ABAP...
《SAP ABAP官方教程》是一份针对SAP ABAP编程语言的重要学习资源,适合对这个领域感兴趣的初学者和有经验的开发者。ABAP(Advanced Business Application Programming)是SAP系统中的主要编程语言,用于开发企业级...
从零开始,教授如何配置 Eclipse 开发 SAP ABAP 程序环境。2020 年最新版本操作笔记。对于初学者十分友好。 ABAP Development Tool的Java端实现就是Eclipse的一个扩展,使用JCO(Java Connector)连接ABAP后台的adt...
SAP ABAP 开发环境和开发工具介绍 SAP ABAP 开发环境和开发工具是 SAP 系统中最重要的组件之一,它提供了一个强大的开发平台,允许开发者创建、测试和部署 ABAP 程序。ABAP 是 SAP 系统中的主要编程语言,用于开发...