Exemplo n.º 1
0
  void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) {
    System.err.println("test: " + opts);
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    try {
      DocumentationTask t = javadoc.getTask(pw, fm, null, null, opts, files);
      boolean ok = t.call();
      pw.close();
      String out = sw.toString().replaceAll("[\r\n]+", "\n");
      if (!out.isEmpty()) System.err.println(out);
      if (ok && expectResult != Main.Result.OK) {
        error("Compilation succeeded unexpectedly");
      } else if (!ok && expectResult != Main.Result.ERROR) {
        error("Compilation failed unexpectedly");
      } else check(out, expectMessages);
    } catch (IllegalArgumentException e) {
      System.err.println(e);
      String expectOut = expectMessages.iterator().next().text;
      if (expectResult != Main.Result.CMDERR) error("unexpected exception caught");
      else if (!e.getMessage().equals(expectOut)) {
        error("unexpected exception message: " + e.getMessage() + " expected: " + expectOut);
      }
    }

    //        if (errors > 0)
    //            throw new Error("stop");
  }
Exemplo n.º 2
0
  public boolean run() throws Util.Exit {

    if (!javac_extras.contains("-XDsuppress-tool-removal-message")) {
      log.println(getMessage("javah.misc.Deprecation"));
    }

    Util util = new Util(log, diagnosticListener);

    if (noArgs || help) {
      showHelp();
      return help; // treat noArgs as an error for purposes of exit code
    }

    if (version || fullVersion) {
      showVersion(fullVersion);
      return true;
    }

    util.verbose = verbose;

    Gen g;

    if (llni) g = new LLNI(doubleAlign, util);
    else {
      g = new JNI(util);
    }

    if (ofile != null) {
      if (!(fileManager instanceof StandardJavaFileManager)) {
        diagnosticListener.report(createDiagnostic("err.cant.use.option.for.fm", "-o"));
        return false;
      }
      Iterable<? extends JavaFileObject> iter =
          ((StandardJavaFileManager) fileManager)
              .getJavaFileObjectsFromFiles(Collections.singleton(ofile));
      JavaFileObject fo = iter.iterator().next();
      g.setOutFile(fo);
    } else {
      if (odir != null) {
        if (!(fileManager instanceof StandardJavaFileManager)) {
          diagnosticListener.report(createDiagnostic("err.cant.use.option.for.fm", "-d"));
          return false;
        }

        if (!odir.exists()) if (!odir.mkdirs()) util.error("cant.create.dir", odir.toString());
        try {
          ((StandardJavaFileManager) fileManager)
              .setLocation(StandardLocation.CLASS_OUTPUT, Collections.singleton(odir));
        } catch (IOException e) {
          Object msg = e.getLocalizedMessage();
          if (msg == null) {
            msg = e;
          }
          diagnosticListener.report(createDiagnostic("err.ioerror", odir, msg));
          return false;
        }
      }
      g.setFileManager(fileManager);
    }

    /*
     * Force set to false will turn off smarts about checking file
     * content before writing.
     */
    g.setForce(force);

    if (fileManager instanceof JavahFileManager)
      ((JavahFileManager) fileManager).setSymbolFileEnabled(false);

    JavaCompiler c = ToolProvider.getSystemJavaCompiler();
    List<String> opts = new ArrayList<>();
    opts.add("-proc:only");
    opts.addAll(javac_extras);

    CompilationTask t;
    try {
      t = c.getTask(log, fileManager, diagnosticListener, opts, classes, null);
    } catch (IllegalArgumentException e) {
      util.error("bad.arg", e.getMessage());
      return false;
    }

    JavahProcessor p = new JavahProcessor(g);
    t.setProcessors(Collections.singleton(p));

    boolean ok = t.call();
    if (p.exit != null) throw new Util.Exit(p.exit);
    return ok;
  }