Implement CGLIB in ABAP

i042416發表於2019-12-14

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.

Implement CGLIB in ABAP

In debugger you can find that the variable in line 17 is mocked by CGLIB:

Implement CGLIB in ABAP

Its byte code is generated dynamically and stored in variable byte[] b in line 217.

Implement CGLIB in ABAP


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:
I have a class MyMEthodExitDemo which has a normal method myFun.
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.

Implement CGLIB in ABAP

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 can CGLIB be implemented in ABAP?

See my implementation  here

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

Implement CGLIB in ABAP


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2668579/,如需轉載,請註明出處,否則將追究法律責任。

相關文章