Search code examples
proxyinstrumentationcglib

Does cglib creates in memory bytecode or physical class for implementing proxy?


CGLIB is very popular implementation used to achieve proxy mechanism. I am curious to know that, to proxy a particular class CGLIB creates an in memory implementation of its sub class or it creates physical byte code and then instantiate that class?

If it creates in memory implementation then how much overhead it brings while doing so. I have tried to find this details in CGLIB documentation but it doesn't specify it or might have skipped by me.

May be the same question can be asked for javaassist library.


Solution

  • Well, first of all, cglib provides quite different classes that are fully independent of each other.

    However, as far as I know, as of today, cglib creates classes in memory by using ASM for reading the byte code of a class and manipulating it "on the fly" by an ASM ClassVisitor etc. That will say, cglib uses ASM's visitor API and not its more memory intensive tree API. The resulting class is then directly loaded. This is about how fast it can get with byte instrumentation.

    Basically, cglib can do everything ASM can do. ASM is quite well documented: http://download.forge.objectweb.org/asm/asm4-guide.pdf

    If you are interested, I wrote a summary on cglib here: http://mydailyjava.blogspot.no/2013/11/cglib-missing-manual.html