コード例 #1
0
  public static void main(String[] args) {
    String filename;
    if (args.length == 0 || !args[0].endsWith(".js")) {
      filename = "../jspointers/test/nano/prototype1.js";
    } else {
      filename = args[0];
    }
    File file = new File(filename);
    if (!file.exists()) {
      System.err.println(filename + " does not exist");
      return;
    }
    boolean onlyWarnings = false;
    for (int i = 1; i < args.length; i++) {
      if (args[i].equals("-onlywarn")) {
        onlyWarnings = true;
      }
    }

    Master master = new Master(file);
    System.out.printf("Using file %s as test case\n", filename);
    int lineNr = CommandLineUtil.promptInt("Enter line number of a property expression to rename");
    List<APropertyExp> candidates = new ArrayList<APropertyExp>();
    for (APropertyExp exp : master.getAllNodesOfType(APropertyExp.class)) {
      if (exp.getName().getLine() == lineNr
          && exp.getRoot() == master.getUserFiles().get(0).getAst()) {
        candidates.add(exp);
      }
    }
    APropertyExp expToRename;
    if (candidates.size() == 0) {
      System.err.println("I can't find any property expressions on that line");
      return;
    } else if (candidates.size() == 1) {
      expToRename = candidates.get(0);
      System.out.printf("Renaming expression %s\n", AstUtil.toSourceString(expToRename));
    } else {
      for (int i = 0; i < candidates.size(); i++) {
        System.out.printf("%d. %s\n", i + 1, AstUtil.toSourceString(candidates.get(i)));
      }
      int num =
          CommandLineUtil.promptInt("Which of the above expressions would you like to rename?");
      expToRename = candidates.get(num - 1);
    }
    String newName = CommandLineUtil.promptString("Enter a new name for the property");
    Refactoring rename = new RenameProperty(master, new PropertyExpAccess(expToRename), newName);
    rename.execute();
    for (Diagnostic warning : rename.getDiagnostics())
      System.err.println("Warning at line " + warning.getStartLine() + ": " + warning.getMessage());
    System.out.println();
    if (!onlyWarnings) {
      System.out.println("----------------------------------");
      System.out.print(AstUtil.toSourceString(master.getUserFiles().get(0).getAst()));
    }
  }