Ejemplo n.º 1
0
  @Override
  public void transformCode(final StringBuilder messages, StringWriter result, final File file)
      throws Throwable {
    delombok.setVerbose(false);
    delombok.setForceProcess(true);
    delombok.setCharset("UTF-8");

    delombok.setDiagnosticsListener(
        new DiagnosticListener<JavaFileObject>() {
          @Override
          public void report(Diagnostic<? extends JavaFileObject> d) {
            String msg = d.getMessage(Locale.ENGLISH);
            Matcher m =
                Pattern.compile(
                        "^"
                            + Pattern.quote(file.getAbsolutePath())
                            + "\\s*:\\s*\\d+\\s*:\\s*(?:warning:\\s*)?(.*)$",
                        Pattern.DOTALL)
                    .matcher(msg);
            if (m.matches()) msg = m.group(1);
            messages.append(
                String.format(
                    "%d:%d %s %s\n", d.getLineNumber(), d.getColumnNumber(), d.getKind(), msg));
          }
        });

    delombok.addFile(file.getAbsoluteFile().getParentFile(), file.getName());
    delombok.setSourcepath(file.getAbsoluteFile().getParent());
    delombok.setWriter(result);
    delombok.delombok();
  }
Ejemplo n.º 2
0
  public static void main(String[] rawArgs) {
    CmdReader<CmdArgs> reader = CmdReader.of(CmdArgs.class);
    CmdArgs args;
    try {
      args = reader.make(rawArgs);
    } catch (InvalidCommandLineException e) {
      System.err.println("ERROR: " + e.getMessage());
      System.err.println(reader.generateCommandLineHelp("delombok"));
      System.exit(1);
      return;
    }

    if (args.help || args.input.isEmpty()) {
      if (!args.help) System.err.println("ERROR: no files or directories to delombok specified.");
      System.err.println(reader.generateCommandLineHelp("delombok"));
      System.exit(args.help ? 0 : 1);
      return;
    }

    Delombok delombok = new Delombok();

    if (args.quiet)
      delombok.setFeedback(
          new PrintStream(
              new OutputStream() {
                @Override
                public void write(int b) throws IOException {
                  // dummy - do nothing.
                }
              }));

    if (args.encoding != null) {
      try {
        delombok.setCharset(args.encoding);
      } catch (UnsupportedCharsetException e) {
        System.err.println("ERROR: Not a known charset: " + args.encoding);
        System.exit(1);
        return;
      }
    }

    if (args.verbose) delombok.setVerbose(true);
    if (args.nocopy) delombok.setNoCopy(true);
    if (args.print) {
      delombok.setOutputToStandardOut();
    } else {
      delombok.setOutput(new File(args.target));
    }

    if (args.classpath != null) delombok.setClasspath(args.classpath);
    if (args.sourcepath != null) delombok.setSourcepath(args.sourcepath);

    for (String in : args.input) {
      try {
        File f = new File(in);
        if (f.isFile()) {
          delombok.addFile(f.getParentFile(), f.getName());
        } else if (f.isDirectory()) {
          delombok.addDirectory(f);
        } else if (!f.exists()) {
          if (!args.quiet) System.err.println("WARNING: does not exist - skipping: " + f);
        } else {
          if (!args.quiet)
            System.err.println("WARNING: not a standard file or directory - skipping: " + f);
        }

        delombok.delombok();
      } catch (Exception e) {
        if (!args.quiet) {
          String msg = e.getMessage();
          if (msg != null && msg.startsWith("DELOMBOK: "))
            System.err.println(msg.substring("DELOMBOK: ".length()));
          else {
            e.printStackTrace();
          }
          System.exit(1);
          return;
        }
      }
    }
  }