Пример #1
0
  /**
   * Perform the 'hotswap' command.
   *
   * @param session JSwat session on which to operate.
   * @param args Tokenized string of command arguments.
   * @param out Output to write messages to.
   */
  public void perform(Session session, CommandArguments args, Log out) {
    if (!session.isActive()) {
      throw new CommandException(Bundle.getString("noActiveSession"));
    }

    if (!args.hasMoreTokens()) {
      throw new MissingArgumentsException();
    }

    // Name of class is required; name of .class file is optional.
    String cname = args.nextToken();
    String cfile = null;
    if (args.hasMoreTokens()) {
      cfile = args.nextToken();
    }

    // Find the ReferenceType for this class.
    VirtualMachine vm = session.getConnection().getVM();
    List classes = vm.classesByName(cname);
    if (classes.size() == 0) {
      throw new CommandException(Bundle.getString("hotswap.noSuchClass"));
    }
    ReferenceType clazz = (ReferenceType) classes.get(0);

    // Did the user give us a .class file?
    InputStream is = null;
    if (cfile == null) {
      // Try to find the .class file.
      PathManager pathman = (PathManager) session.getManager(PathManager.class);
      SourceSource src = pathman.mapClass(clazz);
      if (src == null) {
        throw new CommandException(Bundle.getString("hotswap.fileNotFound"));
      }
      is = src.getInputStream();
    } else {
      // A filename was given, just open it.
      try {
        is = new FileInputStream(cfile);
      } catch (FileNotFoundException fnfe) {
        throw new CommandException(Bundle.getString("hotswap.fileNotFound"));
      }
    }

    // Do the actual hotswap operation.
    try {
      Classes.hotswap(clazz, is, vm);
      out.writeln(Bundle.getString("hotswap.success"));
    } catch (UnsupportedOperationException uoe) {
      if (!vm.canRedefineClasses()) {
        throw new CommandException(Bundle.getString("hotswap.noHotSwap"));
      } else if (!vm.canAddMethod()) {
        throw new CommandException(Bundle.getString("hotswap.noAddMethod"));
      } else if (!vm.canUnrestrictedlyRedefineClasses()) {
        throw new CommandException(Bundle.getString("hotswap.noUnrestricted"));
      } else {
        throw new CommandException(Bundle.getString("hotswap.unsupported"));
      }
    } catch (IOException ioe) {
      throw new CommandException(Bundle.getString("hotswap.errorReadingFile") + ' ' + ioe);
    } catch (NoClassDefFoundError ncdfe) {
      throw new CommandException(Bundle.getString("hotswap.wrongClass"));
    } catch (VerifyError ve) {
      throw new CommandException(Bundle.getString("hotswap.verifyError") + ' ' + ve);
    } catch (UnsupportedClassVersionError ucve) {
      throw new CommandException(Bundle.getString("hotswap.versionError") + ' ' + ucve);
    } catch (ClassFormatError cfe) {
      throw new CommandException(Bundle.getString("hotswap.formatError") + ' ' + cfe);
    } catch (ClassCircularityError cce) {
      throw new CommandException(Bundle.getString("hotswap.circularity") + ' ' + cce);
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException ioe) {
        }
      }
    }
  } // perform