News for hackers. Stuff that matters.
Re:Byte Buddy – runtime code generation for the JVM by twic on Wednesday March 04, 2015 @09:00AM (#9144182)
Looks good! What are the alternatives, and how does it compare to them? cglib? Javassist? I guess BCEL is much lower-level.
Re:Byte Buddy – runtime code generation for the JVM by raphw on Wednesday March 04, 2015 @09:29AM (#9144375)
The two biggest competitors to Byte Buddy today are javassist and cglib. Both of them work quite differently:

javassist takes strings containing Java code and compiles these strings at runtime. This makes it simple to use as every Java developer knows Java. Unfortunately, the javassist compiler lags a lot behind the actual javac compiler. For example, it does not support generics or even auto-boxing, not to speak of lambda expressions. Also, Strings are not type-safe what makes javassist code quite error prone. (It is the equivalent of concenating SQL strings together, with all the risks like foreign code injection.)

cglib allows you to register callbacks for an overridden class, similar to the JDK proxies. However, this requires that all class loaders know about cglib if they want to know these cglib-proxies as cglib-specific types are hard-coded into any generated class. Also, cglib only allows subclassing but not the rewrital of existing classes. Furthermore, the callbacks require any arguments to be boxed and registeres these boxed values into arrays. It turns out that cglib is not quite Just-In-Time-compiler friendly.

Byte Buddy works with annotations or its type-safe DSL and realizes its instrumentation mostly based on delegating to user code. This way, you can implement interceptors in any JVM language and you avoid dependencies on Byte Buddy within the generated classes (annotations are fortunately ignored by JVM class loaders if they cannot be resolved at load-time). This makes for example for better exception stack traces than when using "string code". Also, Byte Buddy avoids boxing by creating smarter objects for the interceptors and only creates those interceptors that are requested by annotated arguments, using dependency injection.

ASM or BCEL are rather byte code parser than code generation libraries. You need to have a clear concept of how byte code works in order to use them. Also, those libraries allow you to create "unverified code", i.e. you have to do a lot manually. Byte Buddy is itself built on top of ASM, you can even plug ASM code into Byte Buddy if you need to.

Re:Byte Buddy – runtime code generation for the JVM by int3 on Wednesday March 04, 2015 @12:41PM (#9145905)
Another approach would be to translate the compiled output of javac into ASM method calls: https://github.com/int3/java2asm

It's fairly unpolished at the moment, though.

Re:Byte Buddy – runtime code generation for the JVM by gdubya on Wednesday March 04, 2015 @09:22AM (#9144319)
He compares ASM/BCEL, Javassist, and cglib to ByteBuddy in his "Making Java more dynamic: runtime code generation for the JVM" presentation: http://www.slideshare.net/RafaelWinterhalter/runtime-code-ge...

See also: Understanding Java byte code, presented at JavaZone 2014: http://2014.javazone.no/presentation.html?id=38ae33c2

Re:Byte Buddy – runtime code generation for the JVM by raphw on Wednesday March 04, 2015 @10:05AM (#9144578)
Also, have a look at this presentation from JavaOne last year of you are interested in the gory details: https://www.parleys.com/talk/runtime-code-generation-jvm
Re:Byte Buddy – runtime code generation for the JVM by rbolkey on Wednesday March 04, 2015 @11:38AM (#9145432)
Apache Tapestry has a library called Plastic that serves the same purpose and is also built on top of ASM. Plastic helps Tapestry handle a lot of the boilerplate and ceremony with common Java patterns (decorators, chains, handler pipelines, etc), and can be used as an independent library.

Here's one blog post on it: http://tapestryjava.blogspot.com/2012/02/plastic-advanced-ex...