SlideShare a Scribd company logo
1 of 42
Download to read offline
Making Java more dynamic:
runtime code generation
for the JVM
interface Framework {
<T> Class<? extends T> secure(Class<T> type);
}
class Service {
@Secured(user = "ADMIN")
void deleteEverything() {
// delete everything...
}
}
@interface Secured {
String user();
}
class UserHolder {
static String user = "ANONYMOUS";
}
does not know aboutdepends on
discoversatruntime
class Service {
@Secured(user = "ADMIN")
void deleteEverything() {
if(!"ADMIN".equals(UserHolder.user)) {
throw new IllegalStateException("Wrong user");
}
// delete everything...
}
}
redefine class
(build time, agent)
create subclass
(Liskov substitution)
class SecuredService extends Service {
@Override
void deleteEverything() {
if(!"ADMIN".equals(UserHolder.user)) {
throw new IllegalStateException("Wrong user");
}
super.deleteEverything();
}
}
class Service {
@Secured(user = "ADMIN")
void deleteEverything() {
// delete everything...
}
}
0xCAFEBABE
source code
byte code
JVM
javac scalac groovyc jrubyc
JIT compilerinterpreter
class loader
creates
reads
runs
class Method {
Object invoke(Object obj,
Object... args)
throws IllegalAccessException,
IllegalArgumentException,
InvocationTargetException;
}
class Class {
Method getDeclaredMethod(String name,
Class<?>... parameterTypes)
throws NoSuchMethodException,
SecurityException;
}
Isn’t reflection meant for this?
Reflection implies neither type-safety nor a notion of fail-fast.
Note: there are no performance gains when using code generation over reflection!
Thus, runtime code generation only makes sense for user type enhancement: While
the framework code is less type safe, this type-unsafety does not spoil the user‘s code.
Do-it-yourself as an alternative?
class Service {
void deleteEverything() {
if(!"ADMIN".equals(UserHolder.user)) {
throw new IllegalStateException("Wrong user");
}
// delete everything...
}
}
At best, this makes testing an issue.
Maybe still the easiest approach for simple cross-cutting concerns.
In general, declarative programming often results in readable and modular code.
The “black magic” prejudice.
var service = {
/* @Secured(user = "ADMIN") */
deleteEverything: function () {
// delete everything ...
}
}
function run(service) {
service.deleteEverything();
}
In dynamic languages (also those running on the JVM) this concept is applied a lot!
For framework implementors, type-safety is conceptually impossible.
But with type information available, we are at least able to fail fast when generating
code at runtime in case that types do not match.
No type, no problem.
(“duck typing”)
The performance myth.
int compute() {
return i * ConstantHolder.value;
}
There is no point in “byte code optimization”.
It’s not true that “reflection is slower than generated code”.
Method::invoke
NativeMethodAccessor
GeneratedMethodAccessor###
The JIT compiler knows its job pretty well. NEVER “optimize” byte code.
Never use JNI for something you could also express as byte code.
However, avoid reflective member lookup.
-Dsun.reflect.inflationThreshold=#
int foo() {
return 1 + 2;
}
ICONST_1
ICONST_2
IADD
operand stack 1
2
13
IRETURN
0x04
0x05
0x60
0xAC
Java source code Java byte code
MethodVisitor methodVisitor = ...
methodVisitor.visitInsn(Opcodes.ICONST_1);
methodVisitor.visitInsn(Opcodes.ICONST_2);
methodVisitor.visitInsn(Opcodes.IADD);
methodVisitor.visitInsn(Opcodes.IRETURN);
MethodNode methodNode = ...
InsnList insnList = methodNode.instructions;
insnList.add(new InsnNode(Opcodes.ICONST_1));
insnList.add(new InsnNode(Opcodes.ICONST_2));
insnList.add(new InsnNode(Opcodes.IADD));
insnList.add(new InsnNode(Opcodes.IRETURN));
ASM / BCEL Javassist cglib Byte Buddy
visitorAPItreeAPI
• Byte code-level API gives full freedom
• Requires knowledge of byte code
(stack metaphor, JVM type system)
• Requires a lot of manual work
(stack sizes / stack map frames)
• Byte code-level APIs are not type safe
(jeopardy of verifier errors, visitor call order)
• Byte code itself is little expressive
• Low overhead (visitor APIs)
• ASM is currently more popular than BCEL
(used by the OpenJDK, considered as public API)
• Versioning issues for ASM (especially v3 to v4)
ASM / BCEL Javassist cglib Byte Buddy
ASM / BCEL Javassist cglib Byte Buddy
int foo() {
return 1 + 2;
}
"int foo() {" +
" return 1 + 2;" +
"}"
• Strings are not typed (“SQL quandary”)
• Specifically: Security problems!
• Makes debugging difficult
(unlinked source code, exception stack traces)
• Bound to Java as a language
• The Javassist compiler lags behind javac
• Requires special Java source code instructions
for realizing cross-cutting concerns
class SecuredService extends Service {
@Override
void deleteEverything() {
methodInterceptor.intercept(this,
Service.class.getDeclaredMethod("deleteEverything"),
new Object[0],
new $MethodProxy());
}
class $MethodProxy implements MethodProxy {
// inner class semantics, can call super
}
}
ASM / BCEL Javassist cglib Byte Buddy
class SecuredService extends Service {
@Override
void deleteEverything() {
if(!"ADMIN".equals(UserHolder.user)) {
throw new IllegalStateException("Wrong user");
}
super.deleteEverything();
}
}
genericdelegation
interface MethodInterceptor {
Object intercept(Object object,
Method method,
Object[] arguments,
MethodProxy proxy)
throws Throwable
}
ASM / BCEL Javassist cglib Byte Buddy
• Discards all available type information
• JIT compiler struggles with two-way-boxing
(check out JIT-watch for evidence)
• Interface dependency of intercepted classes
• Delegation requires explicit class initialization
(breaks build-time usage / class serialization)
• Subclass instrumentation only
(breaks annotation APIs / class identity)
• “Feature complete” / little development
• Little intuitive user-API
ASM / BCEL Javassist cglib Byte Buddy
Class<?> dynamicType = new ByteBuddy()
.subclass(Object.class)
.method(named("toString"))
.intercept(value("Hello World!"))
.make()
.load(getClass().getClassLoader())
.getLoaded();
assertThat(dynamicType.newInstance().toString(),
is("Hello World!"));
ASM / BCEL Javassist cglib Byte Buddy
class MyInterceptor {
static String intercept() {
return "Hello World";
}
}
identifiesbestmatch
Class<?> dynamicType = new ByteBuddy()
.subclass(Object.class)
.method(named("toString"))
.intercept(to(MyInterceptor.class))
.make()
.load(getClass().getClassLoader())
.getLoaded();
ASM / BCEL Javassist cglib Byte Buddy
Annotations that are not visible to a class loader are ignored at runtime.
Thus, Byte Buddy’s classes can be used without Byte Buddy on the class path.
class MyInterceptor {
static String intercept(@Origin Method m) {
return "Hello World from " + m.getName();
}
}
Class<?> dynamicType = new ByteBuddy()
.subclass(Object.class)
.method(named("toString"))
.intercept(to(MyInterceptor.class))
.make()
.load(getClass().getClassLoader())
.getLoaded();
providesarguments
ASM / BCEL Javassist cglib Byte Buddy
@Origin Method|Class<?>|String Provides caller information
@SuperCall Runnable|Callable<?> Allows super method call
@DefaultCall Runnable|Callable<?> Allows default method call
@AllArguments T[] Provides boxed method arguments
@Argument(index) T Provides argument at the given index
@This T Provides caller instance
@Super T Provides super method proxy
ASM / BCEL Javassist cglib Byte Buddy
class Foo {
String bar() { return "bar"; }
}
Foo foo = new Foo();
new ByteBuddy()
.redefine(Foo.class)
.method(named("bar"))
.intercept(value("Hello World!"))
.make()
.load(Foo.class.getClassLoader(),
ClassReloadingStrategy.installedAgent());
assertThat(foo.bar(), is("Hello World!"));
The instrumentation API does not allow introduction of new methods.
This might change with JEP-159: Enhanced Class Redefiniton.
class Foo {
String bar() { return "bar"; }
}
assertThat(new Foo().bar(), is("Hello World!"));
public static void premain(String arguments,
Instrumentation instrumentation) {
new AgentBuilder.Default()
.type(named("Foo"))
.transform((builder, type, loader, module) ->
builder.method(named("bar"))
.intercept(value("Hello World!"));
)
.installOn(instrumentation);
}
Byte Buddy: Java agents
class TimerAdvice {
@OnMethodEnter
static long enter() {
return System.nanoTime();
}
@OnMethodExit
static void exit(@Enter long start, @Origin String method) {
long duration = System.nanoTime() - start;
System.out.println(method + " took " + duration);
}
}
class Foo {
String bar() {
long $start = System.nanoTime();
return "bar";
}
}
class Foo {
String bar() {
long $start = System.nanoTime();
String $result = "bar";
return $result;
}
}
class Foo {
String bar() {
long $start = System.nanoTime();
String $result = "bar";
long $duration = System.nanoTime() – $start;
System.out.println("Foo.bar()" + " took " + $duration);
return $result;
}
}
class Foo {
String bar() {
return "bar";
}
}
Byte Buddy: adding code to a method using advice
class Foo {
String bar() { return "bar"; }
}
System.out.println(new Foo().bar());
public static void agentmain(String arguments,
Instrumentation instrumentation) {
new AgentBuilder.Default()
.with(RedefinitionStrategy.RETRANSFORMATION)
.disableClassFormatChanges()
.type(named("Foo"))
.transform((builder, type, loader, module) ->
builder.visit(Advice.to(TimerAdvice.class)
.on(named("bar")));
)
.installOn(instrumentation);
}
Byte Buddy: Java runtime agents
// bar
// Foo.bar() took 1630
Build-time instrumentation: API
public class SimplePlugin implements Plugin {
@Override
public boolean matches(TypeDescription target) {
return target.getName().equals("Foo");
}
@Override
public DynamicType.Builder<?> apply(
DynamicType.Builder<?> builder,
TypeDescription typeDescription) {
return builder.method(named("bar"))
.intercept(value("Hello World!"));
}
}
Build plugins can be applied as Java agent.
Adapters allow build to agent transformation. (Agent APIs are more specific.)
Build-time instrumentation with Maven.
<plugin>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<configuration>
<transformations>
<transformation>
<plugin>pkg.SimplePlugin</plugin>
</transformation>
</transformations>
</configuration>
</plugin>
Build-time instrumentation with Maven.
<plugin>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<configuration>
<transformations>
<transformation>
<plugin>pkg.SimplePlugin</plugin>
<artifactId>foo</artifactId>
</transformation>
</transformations>
</configuration>
</plugin>
Build-time instrumentation with Gradle.
buildscript {
repositories { jCenter() }
dependencies {
classpath "net.bytebuddy:byte-buddy-gradle-plugin:+"
}
}
apply plugin: "net.bytebuddy.byte-buddy"
configurations {
simplePlugin
}
dependencies {
simplePlugin "bar:foo:1.0"
}
byteBuddy {
transformation {
plugin = "pkg.SimplePlugin"
classPath = configurations.simplePlugin
}
}
Reality check: Reinvent Java?
“Plain old Java applications” (POJAs)
Working with POJOs reduces complexity. Reducing infrastructure code as a goal
Many applications are built around a central infrastructure. A lot of code does not
solve domain problems but bridges between domain and infrastructure.
Java agents allow to add a decentralized infrastructure at runtime. In the source code,
the infrastructure is only declared.
Java virtual
machine
[stack, JIT]
Dalvik virtual
machine
[register, JIT]
Android
runtime
[register, AOT]
Android makes things more complicated.
Solution: Embed the Android SDK’s dex compiler (Apache 2.0 license).
Unfortunately, only subclass instrumentation possible.
baseline Byte Buddy cglib Javassist Java proxy
(1) 0 142 515 193 70
(2a) 0 1‘126 960 1’070 1’060
(2b) 0.002 0.002 0.003 0.011 0.008
(3a) 0 882
5’408
1‘632 683 n/a
(3b) 0.004 0.004
0.004
0.021 0.025 n/a
Results are measured in nanoseconds (rounded).
All benchmarks run with JMH, source code: https://github.com/raphw/byte-buddy
(1) Extending the Object class without any methods but with a default constructor
(2a) Implementing an interface with 18 methods, method stubs
(2b) Executing a method of this interface
(3a) Extending a class with 18 methods, super method invocation
(3b) Executing a method of this class
Performance: library comparison
I O
foo()
bar()
qux()
Performance: delegation vs. advice
foo()
bar()
qux()
qux()
intercept()
-XX:MaxInlineSize=35 (auto-reduced)
-XX:FreqInlineSize=325
-XX:InlineSmallCode=2000
-XX:MaxInlineLevel=9
-XX:InlineSynchronizedMethods=true
-XX:ReservedCodeCacheSize=48M
-XX:+PrintCodeCache
-XX:+PrintCodeCacheOnCompilation
foo()
[module A]
bar()
[module A]
qux()
[module A]
qux()
[module A]
intercept()
[module B]
Module moduleA = findModule("A"),
moduleB = findModule("B");
moduleA.addReads(moduleB);
Java 9: instrumentation meets modules
instrumentation.addReads(moduleA, moduleB);
Loading and leaking runtime classes
boot extension system wrapper
Foo
Bar
[wrapper]
Bar
[inject]
public class Foo {
/* package-private */ String bar() { return "foo"; }
}
public class Bar extends Foo {
@Override
/* package-private */ String bar() { return "bar"; }
}
Caching runtime classes
WeakHashMap<Class<?>, Class<?>> proxies;
Foo Foo$Proxy
via CL
WeakHashMap<Class<?>, SoftReference<Class<?>>>
Map<Class<?>, Class<?>>
WeakHashMap<Class<?>, WeakReference<Class<?>>>
Value references key strongly (parent class). Requires active life-cycle management.
When injecting into the source class loader, this is the only meaningful solution.
Proxy class is collected when there is no instance available.
Proxy class is collected when the heap is filled and there is no instance available.
The only proper solution would be ephemerons which are not available in the JVM.
Discussed in January 2016 / OpenJDK mailing list (core-libs-dev).
class Foo {
void bar(Object arg) { /* do something */ }
}
class Bar extends Foo {
@Override
void bar(Number arg) { /* do something */ }
/* bridge */
void bar(Object arg) { this.bar((Number) arg); }
}
class Foo<T> {
void bar(T arg) { /* do something */ }
}
class Bar<S extends Number> extends Foo<S> {
@Override
void bar(S arg) { /* do something */ }
}
Dealing with bridge methods
class Bar$Proxy extends Bar {
@Override void bar(Number arg) { super.bar(arg); }
@Override void bar(Object arg) { super.bar(arg); }
}
class Foo {
public void bar() { /* do something */ }
}
public class Bar extends Foo {
/* bridge */
public void bar() { super.bar(); }
}
class Foo {
public void bar() { /* do something */ }
}
public class Bar extends Foo { }
Dealing with bridge methods: visibility bridges
class Qux {
public final void bar() { /* do something */ }
}
public class Baz extends Qux { }
new Baz().bar(); // works
Baz.class.getMethod("bar").invoke(new Baz()); // access error
Generic runtime types and dealing with meta data
new ByteBuddy()
.subclass(parameterizedType(Bar.class,
String.class).build())
.method(named("foo"))
.intercept(value("bar"))
.attribute(ForInstrumentedMethod.INCLUDING_RECEIVER)
.make();
@interface Foo {}
interface Bar<T> {
@Foo
void foo(@Foo this, T arg);
}
Byte Buddy handles meta data and generic types transparently.
Not only important for “build-time” instrumentation but also for meta data APIs.
class $MethodProxy implements MethodProxy {
@Override
public Object invokeSuper(Object self,
Method method,
Object[] arguments) {
if (method.equals($bar)) {
((Foo) self).bar((Integer) arguments[0]);
return null;
} else if (method.equals($qux)) {
return ((Foo) self).qux((String) arguments[0]);
} else {
throw new IllegalStateException(); }
}
}
Super method proxies: cglib
interface Foo {
void bar(int arg);
String qux(String arg);
}
class $BarMethodProxy implements Runnable {
final Foo self; final int arg; // constructor omitted
@Override
public void run() {
self.bar(arg);
}
}
Super method proxies: cglib
interface Foo {
void bar(int arg);
String qux(String arg);
}
class $QuxMethodProxy implements Callable<String> {
final Foo self; final String arg; // constructor omitted
@Override
public String call() {
return self.bar(arg);
}
}
Problems caused by “optional” types
class SomeFramework {
void doFrameworkStuff() {
if (isFancyPluginAvailable()) {
init(new OptionalPluginType());
}
/* do work */
}
private void init(OptionalPluginType plugin) {
/* do work */
}
}
// causes NoClassDefFoundError
SomeFramework.class.getMethods();
Not requiring loaded types: an reflection API alternative
interface TypeDescription
interface MethodDescription
interface FieldDescription
interface AnnotationDescription
When a class file transformer is applied, a class is not yet loaded and it is therefore
not possible to access loaded types. To provide an API for matchers and transformers,
Byte Buddy offers an abstraction to the Java reflection API:
Any reference of such a description is resolved lazily. This also allows working with
optional types. The Byte Buddy agent builder offers an automatic fallback as using
loaded types is more performant.
http://rafael.codes
@rafaelcodes
http://documents4j.com
https://github.com/documents4j/documents4j
http://bytebuddy.net
https://github.com/raphw/byte-buddy

More Related Content

What's hot

Php and threads ZTS
Php and threads ZTSPhp and threads ZTS
Php and threads ZTSjulien pauli
 
Testing Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 worldTesting Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 worldYura Nosenko
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesCharles Nutter
 
JavaOne 2009 - TS-5276 - RESTful Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful  Protocol BuffersJavaOne 2009 - TS-5276 - RESTful  Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful Protocol BuffersMatt O'Keefe
 
Building Pluggable Web Applications using Django
Building Pluggable Web Applications using DjangoBuilding Pluggable Web Applications using Django
Building Pluggable Web Applications using DjangoLakshman Prasad
 
Gitlab CI : Integration et Déploiement Continue
Gitlab CI : Integration et Déploiement ContinueGitlab CI : Integration et Déploiement Continue
Gitlab CI : Integration et Déploiement ContinueVincent Composieux
 
The Evolution of Java
The Evolution of JavaThe Evolution of Java
The Evolution of JavaFu Cheng
 
Workshop Spring - Session 4 - Spring Batch
Workshop Spring -  Session 4 - Spring BatchWorkshop Spring -  Session 4 - Spring Batch
Workshop Spring - Session 4 - Spring BatchAntoine Rey
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and filesMarcello Thiry
 
Heap Dump Analysis - AEM: Real World Issues
Heap Dump Analysis - AEM: Real World IssuesHeap Dump Analysis - AEM: Real World Issues
Heap Dump Analysis - AEM: Real World IssuesKanika Gera
 

What's hot (20)

Php and threads ZTS
Php and threads ZTSPhp and threads ZTS
Php and threads ZTS
 
Completable future
Completable futureCompletable future
Completable future
 
Java 11 to 17 : What's new !?
Java 11 to 17 : What's new !?Java 11 to 17 : What's new !?
Java 11 to 17 : What's new !?
 
JAVA NIO
JAVA NIOJAVA NIO
JAVA NIO
 
This and Static Keyword
This and Static KeywordThis and Static Keyword
This and Static Keyword
 
Javascript event handler
Javascript event handlerJavascript event handler
Javascript event handler
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Testing Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 worldTesting Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 world
 
An Introduction to Python Concurrency
An Introduction to Python ConcurrencyAn Introduction to Python Concurrency
An Introduction to Python Concurrency
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for Dummies
 
Java Logging
Java LoggingJava Logging
Java Logging
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
JavaOne 2009 - TS-5276 - RESTful Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful  Protocol BuffersJavaOne 2009 - TS-5276 - RESTful  Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful Protocol Buffers
 
Building Pluggable Web Applications using Django
Building Pluggable Web Applications using DjangoBuilding Pluggable Web Applications using Django
Building Pluggable Web Applications using Django
 
Gitlab CI : Integration et Déploiement Continue
Gitlab CI : Integration et Déploiement ContinueGitlab CI : Integration et Déploiement Continue
Gitlab CI : Integration et Déploiement Continue
 
The Evolution of Java
The Evolution of JavaThe Evolution of Java
The Evolution of Java
 
Workshop Spring - Session 4 - Spring Batch
Workshop Spring -  Session 4 - Spring BatchWorkshop Spring -  Session 4 - Spring Batch
Workshop Spring - Session 4 - Spring Batch
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
 
Heap Dump Analysis - AEM: Real World Issues
Heap Dump Analysis - AEM: Real World IssuesHeap Dump Analysis - AEM: Real World Issues
Heap Dump Analysis - AEM: Real World Issues
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 

Similar to Making Java more dynamic: runtime code generation for the JVM

The definitive guide to java agents
The definitive guide to java agentsThe definitive guide to java agents
The definitive guide to java agentsRafael Winterhalter
 
Code generation for alternative languages
Code generation for alternative languagesCode generation for alternative languages
Code generation for alternative languagesRafael Winterhalter
 
Testing in android
Testing in androidTesting in android
Testing in androidjtrindade
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotationjavatwo2011
 
JJUG CCC 2011 Spring
JJUG CCC 2011 SpringJJUG CCC 2011 Spring
JJUG CCC 2011 SpringKiyotaka Oku
 
New features and enhancement
New features and enhancementNew features and enhancement
New features and enhancementRakesh Madugula
 
code for quiz in my sql
code for quiz  in my sql code for quiz  in my sql
code for quiz in my sql JOYITAKUNDU1
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?Doug Hawkins
 
Have Your Cake and Eat It Too: Meta-Programming Techniques for Java
Have Your Cake and Eat It Too: Meta-Programming Techniques for JavaHave Your Cake and Eat It Too: Meta-Programming Techniques for Java
Have Your Cake and Eat It Too: Meta-Programming Techniques for JavaHoward Lewis Ship
 
Why Spring <3 Kotlin
Why Spring <3 KotlinWhy Spring <3 Kotlin
Why Spring <3 KotlinVMware Tanzu
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVMRafael Winterhalter
 
Object Calisthenics em Go
Object Calisthenics em GoObject Calisthenics em Go
Object Calisthenics em GoElton Minetto
 

Similar to Making Java more dynamic: runtime code generation for the JVM (20)

The definitive guide to java agents
The definitive guide to java agentsThe definitive guide to java agents
The definitive guide to java agents
 
Java byte code in practice
Java byte code in practiceJava byte code in practice
Java byte code in practice
 
Code generation for alternative languages
Code generation for alternative languagesCode generation for alternative languages
Code generation for alternative languages
 
Griffon @ Svwjug
Griffon @ SvwjugGriffon @ Svwjug
Griffon @ Svwjug
 
Testing in android
Testing in androidTesting in android
Testing in android
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
 
ES6 Overview
ES6 OverviewES6 Overview
ES6 Overview
 
JDK Power Tools
JDK Power ToolsJDK Power Tools
JDK Power Tools
 
JJUG CCC 2011 Spring
JJUG CCC 2011 SpringJJUG CCC 2011 Spring
JJUG CCC 2011 Spring
 
New features and enhancement
New features and enhancementNew features and enhancement
New features and enhancement
 
code for quiz in my sql
code for quiz  in my sql code for quiz  in my sql
code for quiz in my sql
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
Have Your Cake and Eat It Too: Meta-Programming Techniques for Java
Have Your Cake and Eat It Too: Meta-Programming Techniques for JavaHave Your Cake and Eat It Too: Meta-Programming Techniques for Java
Have Your Cake and Eat It Too: Meta-Programming Techniques for Java
 
Why Spring <3 Kotlin
Why Spring <3 KotlinWhy Spring <3 Kotlin
Why Spring <3 Kotlin
 
Jersey Guice AOP
Jersey Guice AOPJersey Guice AOP
Jersey Guice AOP
 
Nantes Jug - Java 7
Nantes Jug - Java 7Nantes Jug - Java 7
Nantes Jug - Java 7
 
TDD per Webapps
TDD per WebappsTDD per Webapps
TDD per Webapps
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVM
 
Object Calisthenics em Go
Object Calisthenics em GoObject Calisthenics em Go
Object Calisthenics em Go
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 

More from Rafael Winterhalter

Java and OpenJDK: disecting the ecosystem
Java and OpenJDK: disecting the ecosystemJava and OpenJDK: disecting the ecosystem
Java and OpenJDK: disecting the ecosystemRafael Winterhalter
 
Event-Sourcing Microservices on the JVM
Event-Sourcing Microservices on the JVMEvent-Sourcing Microservices on the JVM
Event-Sourcing Microservices on the JVMRafael Winterhalter
 
Getting started with Java 9 modules
Getting started with Java 9 modulesGetting started with Java 9 modules
Getting started with Java 9 modulesRafael Winterhalter
 
Monitoring distributed (micro-)services
Monitoring distributed (micro-)servicesMonitoring distributed (micro-)services
Monitoring distributed (micro-)servicesRafael Winterhalter
 
An introduction to JVM performance
An introduction to JVM performanceAn introduction to JVM performance
An introduction to JVM performanceRafael Winterhalter
 
Understanding Java byte code and the class file format
Understanding Java byte code and the class file formatUnderstanding Java byte code and the class file format
Understanding Java byte code and the class file formatRafael Winterhalter
 

More from Rafael Winterhalter (12)

Java and OpenJDK: disecting the ecosystem
Java and OpenJDK: disecting the ecosystemJava and OpenJDK: disecting the ecosystem
Java and OpenJDK: disecting the ecosystem
 
Byte code field report
Byte code field reportByte code field report
Byte code field report
 
Event-Sourcing Microservices on the JVM
Event-Sourcing Microservices on the JVMEvent-Sourcing Microservices on the JVM
Event-Sourcing Microservices on the JVM
 
Java 10, Java 11 and beyond
Java 10, Java 11 and beyondJava 10, Java 11 and beyond
Java 10, Java 11 and beyond
 
Getting started with Java 9 modules
Getting started with Java 9 modulesGetting started with Java 9 modules
Getting started with Java 9 modules
 
Monitoring distributed (micro-)services
Monitoring distributed (micro-)servicesMonitoring distributed (micro-)services
Monitoring distributed (micro-)services
 
An Overview of Project Jigsaw
An Overview of Project JigsawAn Overview of Project Jigsaw
An Overview of Project Jigsaw
 
Migrating to JUnit 5
Migrating to JUnit 5Migrating to JUnit 5
Migrating to JUnit 5
 
The Java memory model made easy
The Java memory model made easyThe Java memory model made easy
The Java memory model made easy
 
An introduction to JVM performance
An introduction to JVM performanceAn introduction to JVM performance
An introduction to JVM performance
 
Unit testing concurrent code
Unit testing concurrent codeUnit testing concurrent code
Unit testing concurrent code
 
Understanding Java byte code and the class file format
Understanding Java byte code and the class file formatUnderstanding Java byte code and the class file format
Understanding Java byte code and the class file format
 

Recently uploaded

Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorTier1 app
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdfSteve Caron
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...OnePlan Solutions
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfmaor17
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesVictoriaMetrics
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdfAndrey Devyatkin
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shardsChristopher Curtin
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxRTS corp
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesKrzysztofKkol1
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
Advantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxAdvantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxRTS corp
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsJean Silva
 
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdfPros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdfkalichargn70th171
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
Mastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxMastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxAS Design & AST.
 

Recently uploaded (20)

Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryError
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdf
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 Updates
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
2024-04-09 - From Complexity to Clarity - AWS Summit AMS.pdf
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
Advantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptxAdvantages of Cargo Cloud Solutions.pptx
Advantages of Cargo Cloud Solutions.pptx
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero results
 
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdfPros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
Pros and Cons of Selenium In Automation Testing_ A Comprehensive Assessment.pdf
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
Mastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptxMastering Project Planning with Microsoft Project 2016.pptx
Mastering Project Planning with Microsoft Project 2016.pptx
 

Making Java more dynamic: runtime code generation for the JVM

  • 1. Making Java more dynamic: runtime code generation for the JVM
  • 2. interface Framework { <T> Class<? extends T> secure(Class<T> type); } class Service { @Secured(user = "ADMIN") void deleteEverything() { // delete everything... } } @interface Secured { String user(); } class UserHolder { static String user = "ANONYMOUS"; } does not know aboutdepends on discoversatruntime
  • 3. class Service { @Secured(user = "ADMIN") void deleteEverything() { if(!"ADMIN".equals(UserHolder.user)) { throw new IllegalStateException("Wrong user"); } // delete everything... } } redefine class (build time, agent) create subclass (Liskov substitution) class SecuredService extends Service { @Override void deleteEverything() { if(!"ADMIN".equals(UserHolder.user)) { throw new IllegalStateException("Wrong user"); } super.deleteEverything(); } } class Service { @Secured(user = "ADMIN") void deleteEverything() { // delete everything... } }
  • 4. 0xCAFEBABE source code byte code JVM javac scalac groovyc jrubyc JIT compilerinterpreter class loader creates reads runs
  • 5. class Method { Object invoke(Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException; } class Class { Method getDeclaredMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException; } Isn’t reflection meant for this? Reflection implies neither type-safety nor a notion of fail-fast. Note: there are no performance gains when using code generation over reflection! Thus, runtime code generation only makes sense for user type enhancement: While the framework code is less type safe, this type-unsafety does not spoil the user‘s code.
  • 6. Do-it-yourself as an alternative? class Service { void deleteEverything() { if(!"ADMIN".equals(UserHolder.user)) { throw new IllegalStateException("Wrong user"); } // delete everything... } } At best, this makes testing an issue. Maybe still the easiest approach for simple cross-cutting concerns. In general, declarative programming often results in readable and modular code.
  • 7. The “black magic” prejudice. var service = { /* @Secured(user = "ADMIN") */ deleteEverything: function () { // delete everything ... } } function run(service) { service.deleteEverything(); } In dynamic languages (also those running on the JVM) this concept is applied a lot! For framework implementors, type-safety is conceptually impossible. But with type information available, we are at least able to fail fast when generating code at runtime in case that types do not match. No type, no problem. (“duck typing”)
  • 8.
  • 9. The performance myth. int compute() { return i * ConstantHolder.value; } There is no point in “byte code optimization”. It’s not true that “reflection is slower than generated code”. Method::invoke NativeMethodAccessor GeneratedMethodAccessor### The JIT compiler knows its job pretty well. NEVER “optimize” byte code. Never use JNI for something you could also express as byte code. However, avoid reflective member lookup. -Dsun.reflect.inflationThreshold=#
  • 10. int foo() { return 1 + 2; } ICONST_1 ICONST_2 IADD operand stack 1 2 13 IRETURN 0x04 0x05 0x60 0xAC Java source code Java byte code
  • 11. MethodVisitor methodVisitor = ... methodVisitor.visitInsn(Opcodes.ICONST_1); methodVisitor.visitInsn(Opcodes.ICONST_2); methodVisitor.visitInsn(Opcodes.IADD); methodVisitor.visitInsn(Opcodes.IRETURN); MethodNode methodNode = ... InsnList insnList = methodNode.instructions; insnList.add(new InsnNode(Opcodes.ICONST_1)); insnList.add(new InsnNode(Opcodes.ICONST_2)); insnList.add(new InsnNode(Opcodes.IADD)); insnList.add(new InsnNode(Opcodes.IRETURN)); ASM / BCEL Javassist cglib Byte Buddy visitorAPItreeAPI
  • 12. • Byte code-level API gives full freedom • Requires knowledge of byte code (stack metaphor, JVM type system) • Requires a lot of manual work (stack sizes / stack map frames) • Byte code-level APIs are not type safe (jeopardy of verifier errors, visitor call order) • Byte code itself is little expressive • Low overhead (visitor APIs) • ASM is currently more popular than BCEL (used by the OpenJDK, considered as public API) • Versioning issues for ASM (especially v3 to v4) ASM / BCEL Javassist cglib Byte Buddy
  • 13. ASM / BCEL Javassist cglib Byte Buddy int foo() { return 1 + 2; } "int foo() {" + " return 1 + 2;" + "}" • Strings are not typed (“SQL quandary”) • Specifically: Security problems! • Makes debugging difficult (unlinked source code, exception stack traces) • Bound to Java as a language • The Javassist compiler lags behind javac • Requires special Java source code instructions for realizing cross-cutting concerns
  • 14. class SecuredService extends Service { @Override void deleteEverything() { methodInterceptor.intercept(this, Service.class.getDeclaredMethod("deleteEverything"), new Object[0], new $MethodProxy()); } class $MethodProxy implements MethodProxy { // inner class semantics, can call super } } ASM / BCEL Javassist cglib Byte Buddy class SecuredService extends Service { @Override void deleteEverything() { if(!"ADMIN".equals(UserHolder.user)) { throw new IllegalStateException("Wrong user"); } super.deleteEverything(); } } genericdelegation interface MethodInterceptor { Object intercept(Object object, Method method, Object[] arguments, MethodProxy proxy) throws Throwable }
  • 15. ASM / BCEL Javassist cglib Byte Buddy • Discards all available type information • JIT compiler struggles with two-way-boxing (check out JIT-watch for evidence) • Interface dependency of intercepted classes • Delegation requires explicit class initialization (breaks build-time usage / class serialization) • Subclass instrumentation only (breaks annotation APIs / class identity) • “Feature complete” / little development • Little intuitive user-API
  • 16. ASM / BCEL Javassist cglib Byte Buddy Class<?> dynamicType = new ByteBuddy() .subclass(Object.class) .method(named("toString")) .intercept(value("Hello World!")) .make() .load(getClass().getClassLoader()) .getLoaded(); assertThat(dynamicType.newInstance().toString(), is("Hello World!"));
  • 17. ASM / BCEL Javassist cglib Byte Buddy class MyInterceptor { static String intercept() { return "Hello World"; } } identifiesbestmatch Class<?> dynamicType = new ByteBuddy() .subclass(Object.class) .method(named("toString")) .intercept(to(MyInterceptor.class)) .make() .load(getClass().getClassLoader()) .getLoaded();
  • 18. ASM / BCEL Javassist cglib Byte Buddy Annotations that are not visible to a class loader are ignored at runtime. Thus, Byte Buddy’s classes can be used without Byte Buddy on the class path. class MyInterceptor { static String intercept(@Origin Method m) { return "Hello World from " + m.getName(); } } Class<?> dynamicType = new ByteBuddy() .subclass(Object.class) .method(named("toString")) .intercept(to(MyInterceptor.class)) .make() .load(getClass().getClassLoader()) .getLoaded(); providesarguments
  • 19. ASM / BCEL Javassist cglib Byte Buddy @Origin Method|Class<?>|String Provides caller information @SuperCall Runnable|Callable<?> Allows super method call @DefaultCall Runnable|Callable<?> Allows default method call @AllArguments T[] Provides boxed method arguments @Argument(index) T Provides argument at the given index @This T Provides caller instance @Super T Provides super method proxy
  • 20. ASM / BCEL Javassist cglib Byte Buddy class Foo { String bar() { return "bar"; } } Foo foo = new Foo(); new ByteBuddy() .redefine(Foo.class) .method(named("bar")) .intercept(value("Hello World!")) .make() .load(Foo.class.getClassLoader(), ClassReloadingStrategy.installedAgent()); assertThat(foo.bar(), is("Hello World!")); The instrumentation API does not allow introduction of new methods. This might change with JEP-159: Enhanced Class Redefiniton.
  • 21. class Foo { String bar() { return "bar"; } } assertThat(new Foo().bar(), is("Hello World!")); public static void premain(String arguments, Instrumentation instrumentation) { new AgentBuilder.Default() .type(named("Foo")) .transform((builder, type, loader, module) -> builder.method(named("bar")) .intercept(value("Hello World!")); ) .installOn(instrumentation); } Byte Buddy: Java agents
  • 22. class TimerAdvice { @OnMethodEnter static long enter() { return System.nanoTime(); } @OnMethodExit static void exit(@Enter long start, @Origin String method) { long duration = System.nanoTime() - start; System.out.println(method + " took " + duration); } } class Foo { String bar() { long $start = System.nanoTime(); return "bar"; } } class Foo { String bar() { long $start = System.nanoTime(); String $result = "bar"; return $result; } } class Foo { String bar() { long $start = System.nanoTime(); String $result = "bar"; long $duration = System.nanoTime() – $start; System.out.println("Foo.bar()" + " took " + $duration); return $result; } } class Foo { String bar() { return "bar"; } } Byte Buddy: adding code to a method using advice
  • 23. class Foo { String bar() { return "bar"; } } System.out.println(new Foo().bar()); public static void agentmain(String arguments, Instrumentation instrumentation) { new AgentBuilder.Default() .with(RedefinitionStrategy.RETRANSFORMATION) .disableClassFormatChanges() .type(named("Foo")) .transform((builder, type, loader, module) -> builder.visit(Advice.to(TimerAdvice.class) .on(named("bar"))); ) .installOn(instrumentation); } Byte Buddy: Java runtime agents // bar // Foo.bar() took 1630
  • 24. Build-time instrumentation: API public class SimplePlugin implements Plugin { @Override public boolean matches(TypeDescription target) { return target.getName().equals("Foo"); } @Override public DynamicType.Builder<?> apply( DynamicType.Builder<?> builder, TypeDescription typeDescription) { return builder.method(named("bar")) .intercept(value("Hello World!")); } } Build plugins can be applied as Java agent. Adapters allow build to agent transformation. (Agent APIs are more specific.)
  • 25. Build-time instrumentation with Maven. <plugin> <groupId>net.bytebuddy</groupId> <artifactId>byte-buddy-maven-plugin</artifactId> <executions> <execution> <goals> <goal>transform</goal> </goals> </execution> </executions> <configuration> <transformations> <transformation> <plugin>pkg.SimplePlugin</plugin> </transformation> </transformations> </configuration> </plugin>
  • 26. Build-time instrumentation with Maven. <plugin> <groupId>net.bytebuddy</groupId> <artifactId>byte-buddy-maven-plugin</artifactId> <executions> <execution> <goals> <goal>transform</goal> </goals> </execution> </executions> <configuration> <transformations> <transformation> <plugin>pkg.SimplePlugin</plugin> <artifactId>foo</artifactId> </transformation> </transformations> </configuration> </plugin>
  • 27. Build-time instrumentation with Gradle. buildscript { repositories { jCenter() } dependencies { classpath "net.bytebuddy:byte-buddy-gradle-plugin:+" } } apply plugin: "net.bytebuddy.byte-buddy" configurations { simplePlugin } dependencies { simplePlugin "bar:foo:1.0" } byteBuddy { transformation { plugin = "pkg.SimplePlugin" classPath = configurations.simplePlugin } }
  • 28. Reality check: Reinvent Java? “Plain old Java applications” (POJAs) Working with POJOs reduces complexity. Reducing infrastructure code as a goal Many applications are built around a central infrastructure. A lot of code does not solve domain problems but bridges between domain and infrastructure. Java agents allow to add a decentralized infrastructure at runtime. In the source code, the infrastructure is only declared.
  • 29. Java virtual machine [stack, JIT] Dalvik virtual machine [register, JIT] Android runtime [register, AOT] Android makes things more complicated. Solution: Embed the Android SDK’s dex compiler (Apache 2.0 license). Unfortunately, only subclass instrumentation possible.
  • 30. baseline Byte Buddy cglib Javassist Java proxy (1) 0 142 515 193 70 (2a) 0 1‘126 960 1’070 1’060 (2b) 0.002 0.002 0.003 0.011 0.008 (3a) 0 882 5’408 1‘632 683 n/a (3b) 0.004 0.004 0.004 0.021 0.025 n/a Results are measured in nanoseconds (rounded). All benchmarks run with JMH, source code: https://github.com/raphw/byte-buddy (1) Extending the Object class without any methods but with a default constructor (2a) Implementing an interface with 18 methods, method stubs (2b) Executing a method of this interface (3a) Extending a class with 18 methods, super method invocation (3b) Executing a method of this class Performance: library comparison
  • 31. I O foo() bar() qux() Performance: delegation vs. advice foo() bar() qux() qux() intercept() -XX:MaxInlineSize=35 (auto-reduced) -XX:FreqInlineSize=325 -XX:InlineSmallCode=2000 -XX:MaxInlineLevel=9 -XX:InlineSynchronizedMethods=true -XX:ReservedCodeCacheSize=48M -XX:+PrintCodeCache -XX:+PrintCodeCacheOnCompilation
  • 32. foo() [module A] bar() [module A] qux() [module A] qux() [module A] intercept() [module B] Module moduleA = findModule("A"), moduleB = findModule("B"); moduleA.addReads(moduleB); Java 9: instrumentation meets modules instrumentation.addReads(moduleA, moduleB);
  • 33. Loading and leaking runtime classes boot extension system wrapper Foo Bar [wrapper] Bar [inject] public class Foo { /* package-private */ String bar() { return "foo"; } } public class Bar extends Foo { @Override /* package-private */ String bar() { return "bar"; } }
  • 34. Caching runtime classes WeakHashMap<Class<?>, Class<?>> proxies; Foo Foo$Proxy via CL WeakHashMap<Class<?>, SoftReference<Class<?>>> Map<Class<?>, Class<?>> WeakHashMap<Class<?>, WeakReference<Class<?>>> Value references key strongly (parent class). Requires active life-cycle management. When injecting into the source class loader, this is the only meaningful solution. Proxy class is collected when there is no instance available. Proxy class is collected when the heap is filled and there is no instance available. The only proper solution would be ephemerons which are not available in the JVM. Discussed in January 2016 / OpenJDK mailing list (core-libs-dev).
  • 35. class Foo { void bar(Object arg) { /* do something */ } } class Bar extends Foo { @Override void bar(Number arg) { /* do something */ } /* bridge */ void bar(Object arg) { this.bar((Number) arg); } } class Foo<T> { void bar(T arg) { /* do something */ } } class Bar<S extends Number> extends Foo<S> { @Override void bar(S arg) { /* do something */ } } Dealing with bridge methods class Bar$Proxy extends Bar { @Override void bar(Number arg) { super.bar(arg); } @Override void bar(Object arg) { super.bar(arg); } }
  • 36. class Foo { public void bar() { /* do something */ } } public class Bar extends Foo { /* bridge */ public void bar() { super.bar(); } } class Foo { public void bar() { /* do something */ } } public class Bar extends Foo { } Dealing with bridge methods: visibility bridges class Qux { public final void bar() { /* do something */ } } public class Baz extends Qux { } new Baz().bar(); // works Baz.class.getMethod("bar").invoke(new Baz()); // access error
  • 37. Generic runtime types and dealing with meta data new ByteBuddy() .subclass(parameterizedType(Bar.class, String.class).build()) .method(named("foo")) .intercept(value("bar")) .attribute(ForInstrumentedMethod.INCLUDING_RECEIVER) .make(); @interface Foo {} interface Bar<T> { @Foo void foo(@Foo this, T arg); } Byte Buddy handles meta data and generic types transparently. Not only important for “build-time” instrumentation but also for meta data APIs.
  • 38. class $MethodProxy implements MethodProxy { @Override public Object invokeSuper(Object self, Method method, Object[] arguments) { if (method.equals($bar)) { ((Foo) self).bar((Integer) arguments[0]); return null; } else if (method.equals($qux)) { return ((Foo) self).qux((String) arguments[0]); } else { throw new IllegalStateException(); } } } Super method proxies: cglib interface Foo { void bar(int arg); String qux(String arg); }
  • 39. class $BarMethodProxy implements Runnable { final Foo self; final int arg; // constructor omitted @Override public void run() { self.bar(arg); } } Super method proxies: cglib interface Foo { void bar(int arg); String qux(String arg); } class $QuxMethodProxy implements Callable<String> { final Foo self; final String arg; // constructor omitted @Override public String call() { return self.bar(arg); } }
  • 40. Problems caused by “optional” types class SomeFramework { void doFrameworkStuff() { if (isFancyPluginAvailable()) { init(new OptionalPluginType()); } /* do work */ } private void init(OptionalPluginType plugin) { /* do work */ } } // causes NoClassDefFoundError SomeFramework.class.getMethods();
  • 41. Not requiring loaded types: an reflection API alternative interface TypeDescription interface MethodDescription interface FieldDescription interface AnnotationDescription When a class file transformer is applied, a class is not yet loaded and it is therefore not possible to access loaded types. To provide an API for matchers and transformers, Byte Buddy offers an abstraction to the Java reflection API: Any reference of such a description is resolved lazily. This also allows working with optional types. The Byte Buddy agent builder offers an automatic fallback as using loaded types is more performant.