為了能夠正確的做到動態(tài)更新,我們首先需要attach到target JVM上,具體方法參考http://www.aygfsteel.com/fjin/archive/2009/09/10/294443.html
連接上target JVM后,我們可以就可以利用vm提供的redefineClasses()將新的類文件注入到JVM中,替代原有的class信息。
1 public void reloadClasses(List toReloads){
2 Map toReloadMap = new HashMap();
3 for (Iterator iterator = toReloads.iterator(); iterator.hasNext();)
4 {
5 String toReload = (String) iterator.next();
6 InputStream is = VMDebugger.class.getClassLoader().getResourceAsStream(toReload.replace('.', '/') + ".class");
7 if (is == null) {
8 throw new RuntimeException("Class " + toReload + " is not found in current classpath");
9 }
10 List classes = vm.classesByName(toReload);
11 if (classes == null || classes.isEmpty()) {
12 throw new RuntimeException("Class: " + toReload + " is not found in target JVM");
13 } else {
14 ReferenceType ref = (ReferenceType) classes.get(0);
15 try{
16 toReloadMap.put(ref, toByteArray(is));
17 }catch(Exception e){
18 e.printStackTrace();
19 }
20 }
21 }
22 vm.redefineClasses(toReloadMap);
23 }
2 Map toReloadMap = new HashMap();
3 for (Iterator iterator = toReloads.iterator(); iterator.hasNext();)
4 {
5 String toReload = (String) iterator.next();
6 InputStream is = VMDebugger.class.getClassLoader().getResourceAsStream(toReload.replace('.', '/') + ".class");
7 if (is == null) {
8 throw new RuntimeException("Class " + toReload + " is not found in current classpath");
9 }
10 List classes = vm.classesByName(toReload);
11 if (classes == null || classes.isEmpty()) {
12 throw new RuntimeException("Class: " + toReload + " is not found in target JVM");
13 } else {
14 ReferenceType ref = (ReferenceType) classes.get(0);
15 try{
16 toReloadMap.put(ref, toByteArray(is));
17 }catch(Exception e){
18 e.printStackTrace();
19 }
20 }
21 }
22 vm.redefineClasses(toReloadMap);
23 }
上面的方法有如下幾個注意的地方:
1:確保要更新的類在當(dāng)前classpath下(也就是啟動VMDebugger的classpath)。
2:確保要更新的類在target JVM已被加載。
3:不同的VM實現(xiàn)上不一樣,有的可能不允許動態(tài)更新,可以使用
canRedefineClasses()
判斷一下。