views:

266

answers:

3

Just out of curiosity, are there any (stable) open source projects for runtime java code generation other than cglib? And why should I use them?

+2  A: 

Javassist.

If you need to make proxies, take a look at commons-proxy - it uses both CGLIB and Javassit.

Bozho
+1  A: 

I prefer raw ASM, which I believe is used by cglib anyway. It's low level, but the documentation is brilliant, and once you get used to it you'll be flying.

To answer your second question, you should use code generation when your reflection and dynamic proxies are beginning to feel a bit cobbled together and you need a rock solid solution. In the past I've even added a code generation step into the build process in Eclipse, effectively giving me compile time reporting of anything and everything.

CurtainDog
+1  A: 

I think it's more sense to use Javassist instead of cglib. E.g. javasist perfectly works with signed jars unlike cglib. Besides, such grand as Hibernate project decided to stop using cglib in favor of Javassist.

FoxyBOA