예제 #1
0
 private static byte[] loadClassFile(File classFile) {
   try {
     DataInputStream in = new DataInputStream(new FileInputStream(classFile));
     byte[] ret = new byte[(int) classFile.length()];
     in.readFully(ret);
     in.close();
     return ret;
   } catch (IOException e) {
     log.info(e.getMessage());
   }
   return null;
 }
예제 #2
0
public class HotSwapUtil {
  private static JdeLogger log = JdeLogger.getLogger("HotSwapServer");

  @SuppressWarnings("all")
  public static void replace(Debugger debugger, File classFile, String className) {

    VirtualMachine vm = debugger.getVm();
    if (vm == null) return;
    if (!vm.canRedefineClasses()) return;

    byte[] classBytes = loadClassFile(classFile);
    if (classBytes == null) return;

    List classes = vm.classesByName(className);

    if (classes == null || classes.size() == 0) return;

    for (int i = 0; i < classes.size(); i++) {
      ReferenceType refType = (ReferenceType) classes.get(i);
      HashMap map = new HashMap();
      map.put(refType, classBytes);
      vm.redefineClasses(map);
    }
  }

  private static byte[] loadClassFile(File classFile) {
    try {
      DataInputStream in = new DataInputStream(new FileInputStream(classFile));
      byte[] ret = new byte[(int) classFile.length()];
      in.readFully(ret);
      in.close();
      return ret;
    } catch (IOException e) {
      log.info(e.getMessage());
    }
    return null;
  }
}