Byte Buddy : bytecode gen made easy !

Nicolas Comet
n.comet@lectra.com
javaone logo

About me

avatar
  • Developer since 7 years
logolectra130
  • Software R&D Engineer @Lectra, worldwide leader in integrated softwares, machines and cloud services for soft material industries
bordeauxjug
  • Speaker & Member of Bordeaux JUG

Lectra

lectra hires

How it all began

"Making Java more dynamic" @Devoxx France 2015

rafaelwinterhalter
rafael winterhalter
150

Cross Cutting Concerns

  • Non business/domain related
  • Orthogonal preoccupations
what

Examples

  • Logging
  • Caching
  • Monitoring
  • Data validation
  • Real time constraints
  • Persistence
  • Transactions
  • …​

Adding behavior

  • Transparent
  • Non intrusive
  • Strongly typed
  • Regardless of type
  • Lightweight

(Some) solutions

  • Reflection
  • Aspect Oriented Programming (AOP)
  • Bytecode generation

Reflection

java.lang.reflect

Reading Type metamodel at runtime

Calling constructors, methods, access attributes (sometimes unsafely)

Mostly about Introspection

It has a cost (JIT is useless)

AOP concepts

concerns

aop

AOP

  • Aspect Description of a cross cutting concern
  • Join point A point during the execution of code (method execution, attribute access)
  • Advice Action taken by an aspect at a particular join point
  • Pointcut A regular expression that matches join points.
ajc

Java Bytecode

bytecode

Bytecode generation

diagramme

Frameworks

frameworks

The famous case

  • n ∈ ℕ
def

The famous case

  • Call tree
fibotree150

Caching

  • Memoization
fibomemoized

Some code !

demo

Perf comparison

Calling fibonacci(42) (average results)

Version Time

Raw Fibonacci

1123.658 ms

AspectJ (compile time)

0.013 ms

Byte Buddy (runtime)

0.689 ms

Spring AOP

2123 ms (first time, then instant)

Under the hood

  • AspectJ

    • compile time weaving (ajc)
    • post-compile weaving (on classes and jars)
    • load time weaving (agent)
    • intercept everything

  • Spring AOP

    • proxy-based

      • Interface → Java dynamic proxy
      • else CGLIB bytecode generated proxy

    • good AspectJ integration if you need more

Pros & Cons

  • AspectJ

    • Setup
    • DSL to learn
    • Performance
    • Non intrusive
    • Span

  • Spring AOP

    • Not really AOP
    • Component’s public methods only
    • / Framework
    • Spring integration
    • Migration to AspectJ

  • Byte Buddy

    • / No compile time
    • Library
    • Java DSL API
    • Performance
    • Agent writing help

Byte Buddy

Open Source (license Apache), used by Mockito, Hibernate, Google Bazle, and others

Stars
  • Light
  • Easy to use (compared to CGLIB, BCEL, ASM)
  • Become a library writer

ASM Visitor

asm visitor

Demo

demo

Slides :

Sources :

Conclusion

  • Adding behavior

    • AOP → Implementing multiple cross cutting concerns
    • Byte Buddy → Writing libraries/frameworks or agents

  • Discovering at runtime

    • Reflection → Custom serialization, nasty things (setting private fields…​)

/