In Java, a typical workflow involves restarting the application (almost) with every class change. For some applications it is not a problem at all, for some – it is a disaster.
From HotSwap to agent-based reloading. In this session, we are going to take a look at the options available for Java class reloading. There is plenty of tools that you can use for this task: rely on standard JVM HotSwap, redesign your application to rely on dynamic class loaders, to comprehend the Zen of OSGi, or to integrate a reloading agent. Every option has its own drawbacks and benefits and we’re going to take a deep dive on the subject.
Finally, there are also the conceptual challenges in reloading Java classes. What to do with the state? What should happen with the static initialisers? What if super class changes? Join this session to gain a better understanding of class reloading technologies and become more productive Java developer.
31. Dynamic Code Evolution for Java
T. Würthinger, C. Wimmer, L. Stadler. 2010
Statements
Methods
32. Dynamic Code Evolution for Java
T. Würthinger, C. Wimmer, L. Stadler. 2010
Statements
Methods Fields
33. Dynamic Code Evolution for Java
T. Würthinger, C. Wimmer, L. Stadler. 2010
Statements
Methods Fields Hierarchy
34. Dynamic Code Evolution for Java
T. Würthinger, C. Wimmer, L. Stadler. 2010
Statements
Methods Fields Hierarchy
35. Dynamic Code Evolution for Java
T. Würthinger, C. Wimmer, L. Stadler. 2010
Statements
Methods Fields Hierarchy
+ + +
Binary-compatible
36. Dynamic Code Evolution for Java
T. Würthinger, C. Wimmer, L. Stadler. 2010
Statements
Methods Fields Hierarchy
+ + +
x x x
Binary-compatible
Binary-incompatible
39. 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;
}
40. 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;
}
41. 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;
}
42. 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;
}
43. 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;
}
44. while(true) {
Class<?> uc = new DynamicClassLoader().load("com.zt.User");
ReflectUtil.invokeStatic("getHobby", uc);
}
public class User {
public Hobby getHobby() {
return Basketball();
}
}
45. while(true) {
Class<?> uc = new DynamicClassLoader().load("com.zt.User");
ReflectUtil.invokeStatic("getHobby", uc);
}
public class User {
public Hobby getHobby() {
return Basketball();
}
}
46. public static class Context {
public HobbyService hobbyService = new HobbyService();
public void init() {
hobbyService.user = new User();
anyService.initialize()
}
}
47. public static class Context {
public HobbyService hobbyService = new HobbyService();
public AnyService anyService = new AnyService();
public void init() {
hobbyService.user = new User();
anyService.initialize()
}
}
48. 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);
}