SlideShare a Scribd company logo
The hitchhiker’s guide to
Java class reloading
@antonarhipov
whoami
Anton Arhipov
@antonarhipov
whoami
Anton Arhipov
@antonarhipov
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
https://zeroturnaround.com/rebellabs/java-ee-productivity-report-2011/
10%
20%
30%
0.5 1 2 3 4 5 6 7 8 10+ minutes
https://zeroturnaround.com/rebellabs/java-ee-productivity-report-2011/
10%
20%
30%
0.5 1 2 3 4 5 6 7 8 10+
The only effective developers??
minutes
https://zeroturnaround.com/rebellabs/java-ee-productivity-report-2011/
10%
20%
30%
0.5 1 2 3 4 5 6 7 8 10+
How about those?
minutes
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
HotSwap
HotSwap
Class loaders
HotSwap
Class loaders
Java agents & instrumentation
HotSwap
Class loaders
Java agents & instrumentation
hotswapEST. 2001
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
HOW ABOUT…
refactoring?
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JDB
instanceKlass constantPoolOop
constants()
pool_holder()
klassVTable
Embedded
klassITable
Embedded
Embedded
statics
M. Dmitriev. Safe class and data evolution in large and long-lived Java (тм) applications. Technical report, Mountain View. 2001
instanceKlass constantPoolOop
constants()
constantPoolCacheOop
cache()
pool_holder()
klassVTable
Embedded
klassITable
Embedded
Embedded
statics
M. Dmitriev. Safe class and data evolution in large and long-lived Java (тм) applications. Technical report, Mountain View. 2001
instanceKlass constantPoolOop
constants()
constantPoolCacheOop
cache()
pool_holder()
klassVTable
Embedded
klassITable
Embedded
Embedded
statics
objArrayOop
methodOop
methods()
M. Dmitriev. Safe class and data evolution in large and long-lived Java (тм) applications. Technical report, Mountain View. 2001
instanceKlass constantPoolOop
constants()
constantPoolCacheOop
cache()
pool_holder()
klassVTable
Embedded
klassITable
Embedded
Embedded
statics
nmethod
code()
method()
constants()
objArrayOop
methodOop
methods()
M. Dmitriev. Safe class and data evolution in large and long-lived Java (тм) applications. Technical report, Mountain View. 2001
What if…
hotswap++
Dynamic Code Evolution for Java
T. Würthinger, C. Wimmer, L. Stadler. 2010
Dynamic Code Evolution for Java
T. Würthinger, C. Wimmer, L. Stadler. 2010
Statements
Dynamic Code Evolution for Java
T. Würthinger, C. Wimmer, L. Stadler. 2010
Statements
Methods
Dynamic Code Evolution for Java
T. Würthinger, C. Wimmer, L. Stadler. 2010
Statements
Methods Fields
Dynamic Code Evolution for Java
T. Würthinger, C. Wimmer, L. Stadler. 2010
Statements
Methods Fields Hierarchy
Dynamic Code Evolution for Java
T. Würthinger, C. Wimmer, L. Stadler. 2010
Statements
Methods Fields Hierarchy
Dynamic Code Evolution for Java
T. Würthinger, C. Wimmer, L. Stadler. 2010
Statements
Methods Fields Hierarchy
+ + +
Binary-compatible
Dynamic Code Evolution for Java
T. Würthinger, C. Wimmer, L. Stadler. 2010
Statements
Methods Fields Hierarchy
+ + +
x x x
Binary-compatible
Binary-incompatible
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
Classloaders
Class<?> uc1 = User.class;
Class<?> uc2 = new DynamicClassLoader().load("com.zt.User");
out.println(uc1.getName()); // com.zt.User
out.println(uc2.getName()); // com.zt.User
out.println(uc1.getClassLoader()); // sun.misc.Launcher$AppClassLoader@18b4aac2
out.println(uc2.getClassLoader()); // com.zt.DynamicClassLoader@22b4bba7
User.age = 11;
out.println((int) ReflectUtil.getStaticFieldValue("age", uc1)); // 11
out.println((int) ReflectUtil.getStaticFieldValue("age", uc2)); // 10
public class User {
public static int age = 10;
}
Class<?> uc1 = User.class;
Class<?> uc2 = new DynamicClassLoader().load("com.zt.User");
out.println(uc1.getName()); // com.zt.User
out.println(uc2.getName()); // com.zt.User
out.println(uc1.getClassLoader()); // sun.misc.Launcher$AppClassLoader@18b4aac2
out.println(uc2.getClassLoader()); // com.zt.DynamicClassLoader@22b4bba7
User.age = 11;
out.println((int) ReflectUtil.getStaticFieldValue("age", uc1)); // 11
out.println((int) ReflectUtil.getStaticFieldValue("age", uc2)); // 10
public class User {
public static int age = 10;
}
Class<?> uc1 = User.class;
Class<?> uc2 = new DynamicClassLoader().load("com.zt.User");
out.println(uc1.getName()); // com.zt.User
out.println(uc2.getName()); // com.zt.User
out.println(uc1.getClassLoader()); // sun.misc.Launcher$AppClassLoader@18b4aac2
out.println(uc2.getClassLoader()); // com.zt.DynamicClassLoader@22b4bba7
User.age = 11;
out.println((int) ReflectUtil.getStaticFieldValue("age", uc1)); // 11
out.println((int) ReflectUtil.getStaticFieldValue("age", uc2)); // 10
public class User {
public static int age = 10;
}
Class<?> uc1 = User.class;
Class<?> uc2 = new DynamicClassLoader().load("com.zt.User");
out.println(uc1.getName()); // com.zt.User
out.println(uc2.getName()); // com.zt.User
out.println(uc1.getClassLoader()); // sun.misc.Launcher$AppClassLoader@18b4aac2
out.println(uc2.getClassLoader()); // com.zt.DynamicClassLoader@22b4bba7
User.age = 11;
out.println((int) ReflectUtil.getStaticFieldValue("age", uc1)); // 11
out.println((int) ReflectUtil.getStaticFieldValue("age", uc2)); // 10
public class User {
public static int age = 10;
}
Class<?> uc1 = User.class;
Class<?> uc2 = new DynamicClassLoader().load("com.zt.User");
out.println(uc1.getName()); // com.zt.User
out.println(uc2.getName()); // com.zt.User
out.println(uc1.getClassLoader()); // sun.misc.Launcher$AppClassLoader@18b4aac2
out.println(uc2.getClassLoader()); // com.zt.DynamicClassLoader@22b4bba7
User.age = 11;
out.println((int) ReflectUtil.getStaticFieldValue("age", uc1)); // 11
out.println((int) ReflectUtil.getStaticFieldValue("age", uc2)); // 10
public class User {
public static int age = 10;
}
while(true) {
Class<?> uc = new DynamicClassLoader().load("com.zt.User");
ReflectUtil.invokeStatic("getHobby", uc);
}
public class User {
public Hobby getHobby() {
return Basketball();
}
}
while(true) {
Class<?> uc = new DynamicClassLoader().load("com.zt.User");
ReflectUtil.invokeStatic("getHobby", uc);
}
public class User {
public Hobby getHobby() {
return Basketball();
}
}
public static class Context {
public HobbyService hobbyService = new HobbyService();
public void init() {
hobbyService.user = new User();
anyService.initialize()
}
}
public static class Context {
public HobbyService hobbyService = new HobbyService();
public AnyService anyService = new AnyService();
public void init() {
hobbyService.user = new User();
anyService.initialize()
}
}
public static class Context {
public HobbyService hobbyService = new HobbyService();
public AnyService anyService = new AnyService();
public void init() {
hobbyService.user = new User();
anyService.initialize()
}
}
while(true) {
Class<?> c = new DynamicClassLoader().load("com.zt.Context");
Object context = c.newInstance();
ReflectUtil.invokeMethod("init", context);
invokeService(context);
}
DynamicClassLoader
Context class
HobbyService class
User class
Context object
HobbyService object
User object
Reloadable
“region”
Live thread
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
DEMO
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
Java agents
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
public class Agent {
public static void premain(String args, Instrumentation inst)
throws Exception {
inst.addTransformer(new ClassFileTransformer {
// here be dragons
});
}
}
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
public class Agent {
public static void premain(String args, Instrumentation inst)
throws Exception {
inst.addTransformer(new ClassFileTransformer {
// here be dragons
});
}
}
META-INF/MANIFEST.MF
Premain-Class: Agent
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
public class Agent {
public static void premain(String args, Instrumentation inst)
throws Exception {
inst.addTransformer(new ClassFileTransformer {
// here be dragons
});
}
}
$> java –javaagent:agent.jar application.Main
META-INF/MANIFEST.MF
Premain-Class: Agent
ClassFileTransformer
ClassA
ClassA
ClassA0
+field1
+field2
+field3
+method1
+method2
+method3
+method1
+method2
+method3
+field1
+field2
+field3
+proxy_methods
A thousand years of productivity: the JRebel story
E. Kabanov, V. Vene, 2012
ClassFileTransformer
ClassA
ClassA
ClassA0
+field1
+field2
+field3
+method1
+method2
+method3
+method1
+method2
+method3
+field1
+field2
+field3
+proxy_methods
A thousand years of productivity: the JRebel story
E. Kabanov, V. Vene, 2012
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
HotSwap
HotSwap
Class loaders
HotSwap
Class loaders
Java agents & instrumentation
anton@zeroturnaround.com
@antonarhipov

More Related Content

PDF
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
PDF
Java Bytecode for Discriminating Developers - JavaZone 2011
PDF
Android programming -_pushing_the_limits
PDF
droidparts
PPTX
Unit testing without Robolectric, Droidcon Berlin 2016
PPTX
Junit 5 - Maior e melhor
PDF
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
Java Bytecode for Discriminating Developers - JavaZone 2011
Android programming -_pushing_the_limits
droidparts
Unit testing without Robolectric, Droidcon Berlin 2016
Junit 5 - Maior e melhor
JavaOne 2017 - The hitchhiker’s guide to Java class reloading

What's hot (6)

KEY
YQL Tutorial
PDF
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
PPTX
Demystifying dependency Injection: Dagger and Toothpick
PDF
Improving Android Performance at Mobiconf 2014
PDF
Construire une application JavaFX 8 avec gradle
PPTX
Legacy projects: how to win the race
YQL Tutorial
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Demystifying dependency Injection: Dagger and Toothpick
Improving Android Performance at Mobiconf 2014
Construire une application JavaFX 8 avec gradle
Legacy projects: how to win the race
Ad

Similar to JavaZone 2017 - The Hitchhiker’s guide to Java class reloading (20)

PDF
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
PDF
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
PDF
Riga Dev Day 2016 - Having fun with Javassist
PDF
DevoxxPL: JRebel Under The Covers
PPT
Java Reflection
PPTX
Why Doesn't Java Has Instant Turnaround - Con-FESS 2012
PPTX
Rapid java application development @ JUG.ru 25.02.2012
PPTX
The definitive guide to java agents
PPTX
Binary patching for fun and profit @ JUG.ru, 25.02.2012
PDF
java classes in pune
PDF
JavaOne 2015 - Having fun with Javassist
PPT
Reflection
PPT
Reflection
PPT
Reflection
PPT
Reflection
PPT
Reflection
PPT
Reflection
PPTX
PDF
Jvm internals
PDF
Marvel of Annotation Preprocessing in Java by Alexey Buzdin
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga Dev Day 2016 - Having fun with Javassist
DevoxxPL: JRebel Under The Covers
Java Reflection
Why Doesn't Java Has Instant Turnaround - Con-FESS 2012
Rapid java application development @ JUG.ru 25.02.2012
The definitive guide to java agents
Binary patching for fun and profit @ JUG.ru, 25.02.2012
java classes in pune
JavaOne 2015 - Having fun with Javassist
Reflection
Reflection
Reflection
Reflection
Reflection
Reflection
Jvm internals
Marvel of Annotation Preprocessing in Java by Alexey Buzdin
Ad

More from Anton Arhipov (20)

PDF
JavaZone 2022 - Building Kotlin DSL.pdf
PDF
Idiomatic kotlin
PDF
TechTrain 2019 - (Не)адекватное техническое интервью
PDF
Build pipelines with TeamCity
PDF
Build pipelines with TeamCity
PDF
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
PDF
GeeCON Prague 2018 - Kotlin DSL in under an hour
PDF
Build pipelines with TeamCity and Kotlin DSL
PDF
Build pipelines with TeamCity
PDF
JavaDay Kiev 2017 - Integration testing with TestContainers
PDF
GeeCON Prague 2017 - TestContainers
PDF
JavaOne 2017 - TestContainers: integration testing without the hassle
PDF
JUG.ua 20170225 - Java bytecode instrumentation
PDF
GeeCON 2017 - TestContainers. Integration testing without the hassle
PDF
JEEConf 2017 - Having fun with Javassist
PDF
Devclub 01/2017 - (Не)адекватное Java-интервью
PDF
Joker 2016 - Bytecode 101
PDF
JPoint 2016 - Etudes of DIY Java profiler
PDF
JPoint 2016 - Bytecode
PDF
Oredev 2015 - Taming Java Agents
JavaZone 2022 - Building Kotlin DSL.pdf
Idiomatic kotlin
TechTrain 2019 - (Не)адекватное техническое интервью
Build pipelines with TeamCity
Build pipelines with TeamCity
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
GeeCON Prague 2018 - Kotlin DSL in under an hour
Build pipelines with TeamCity and Kotlin DSL
Build pipelines with TeamCity
JavaDay Kiev 2017 - Integration testing with TestContainers
GeeCON Prague 2017 - TestContainers
JavaOne 2017 - TestContainers: integration testing without the hassle
JUG.ua 20170225 - Java bytecode instrumentation
GeeCON 2017 - TestContainers. Integration testing without the hassle
JEEConf 2017 - Having fun with Javassist
Devclub 01/2017 - (Не)адекватное Java-интервью
Joker 2016 - Bytecode 101
JPoint 2016 - Etudes of DIY Java profiler
JPoint 2016 - Bytecode
Oredev 2015 - Taming Java Agents

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Modernizing your data center with Dell and AMD
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPT
Teaching material agriculture food technology
PDF
cuic standard and advanced reporting.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
Big Data Technologies - Introduction.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Digital-Transformation-Roadmap-for-Companies.pptx
Modernizing your data center with Dell and AMD
NewMind AI Weekly Chronicles - August'25 Week I
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Teaching material agriculture food technology
cuic standard and advanced reporting.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
20250228 LYD VKU AI Blended-Learning.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Empathic Computing: Creating Shared Understanding
Approach and Philosophy of On baking technology
Encapsulation_ Review paper, used for researhc scholars
Mobile App Security Testing_ A Comprehensive Guide.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Dropbox Q2 2025 Financial Results & Investor Presentation

JavaZone 2017 - The Hitchhiker’s guide to Java class reloading