Esempio n. 1
0
  /**
   * Prepare the paths for compiling
   *
   * @param srcFiles
   * @return String describing error if something went wrong
   */
  public String compile(File srcFiles[]) {
    String paths[] = new String[srcFiles.length];
    for (int i = 0; i < paths.length; i++) {
      paths[i] = srcFiles[i].getAbsolutePath();
    }
    for (String path : paths) {
      Logger.getInstance()
          .log("de.~.vm.Javac.compile(File)", Logger.DEBUG, "Content of paths: " + path);
    }
    Logger.getInstance()
        .log("de.~.vm.Javac.compile(File)", Logger.DEBUG, "Extracted paths, starting compiler");
    Logger.getInstance()
        .log(
            "de.~.vm.Javac.compile(File)",
            Logger.DEBUG,
            "Path to Javac.javac: "
                + this.getClass()
                    .getProtectionDomain()
                    .getCodeSource()
                    .getLocation()
                    .getFile()
                    .toString());

    return compile(paths);
  }
Esempio n. 2
0
  /**
   * Final preparations for the system's compiler, if it could be fetched. If not, compiling is down
   * by the compiler from com.sun.tools.javac.Main
   *
   * @param srcFiles
   * @return null if success; or compilation errors
   */
  public String compile(String srcFiles[]) {
    Logger.getInstance().log("de.~.vm.Javac.compile(String)", Logger.DEBUG, "Begin of function");

    StringWriter err = new StringWriter();
    PrintWriter errPrinter = new PrintWriter(err);

    String args[] = buildJavacArgs(srcFiles);

    // Fetch the systems JavaCompiler. Fails, if Eclipse is startet
    // with the VM from JRE. Works with JDK.
    compiler = ToolProvider.getSystemJavaCompiler();

    if (compiler != null) {
      String algorithmAsSourceCode = readfile(args[args.length - 1]);

      // extract the name of the algorithm. The name is always at the last position of this array
      String algorithmName =
          args[args.length - 1].split("/")[args[args.length - 1].split("/").length - 1];

      String error = null;

      // Create a new AlvisFileObject containing the source code and let the compiler compile it
      try {

        error = compile(new AlvisFileObject(algorithmName, algorithmAsSourceCode));

      } catch (URISyntaxException e) {
        e.printStackTrace();
      }

      Logger.getInstance()
          .log(
              "de.~.vm.Javac.compile(String)",
              Logger.DEBUG,
              "Compile with ToolsProvider compiler was successful");

      errPrinter.close();
      Logger.getInstance()
          .log("de.~.vm.Javac.compile(String)", Logger.DEBUG, "Error? " + err.toString());

      return error;
    } else {
      Logger.getInstance()
          .log(
              "de.~.vm.Javac.compile(String)",
              Logger.DEBUG,
              "ToolsProvider did not provide a compiler, using fallback");
      // Grabbing the standard compiler did not work, compiling "by hand"
      // FIXME: 1. Find a way, so that
      // ToolProvider.getSystemJavaCompiler() cannot return null
      // 2. Delete the call to com.sun.tools.javac.Main
      int resultCode = com.sun.tools.javac.Main.compile(args, errPrinter);
      return (resultCode == 0) ? null : err.toString();
    }
  }
Esempio n. 3
0
  /**
   * Compiles the source code using the systems java compiler
   *
   * @param source
   * @return true -> compiling worked flawlessly
   */
  private static String compile(JavaFileObject... source) {
    final ArrayList<String> options = new ArrayList<String>();
    if (classpath != null) {
      options.add("-classpath");
      options.add(System.getProperty("java.class.path") + classpath);
    }
    if (outputdir != null) {
      options.add("-d");
      options.add(outputdir);
    }

    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();

    /**
     * Retrieving the standard file manager from compiler object, which is used to provide basic
     * building block for customizing how a compiler reads and writes to files.
     *
     * <p>The same file manager can be reopened for another compiler task. Thus we reduce the
     * overhead of scanning through file system and jar files each time
     */
    StandardJavaFileManager stdFileManager =
        compiler.getStandardFileManager(null, Locale.getDefault(), null);

    final JavaCompiler.CompilationTask task =
        compiler.getTask(null, stdFileManager, diagnostics, options, null, Arrays.asList(source));
    boolean result = task.call();
    String error = null;

    if (!result) {
      error = "Compilation failed, see log for details";
      for (@SuppressWarnings("rawtypes") Diagnostic diagnostic : diagnostics.getDiagnostics()) {
        Logger.getInstance()
            .log(
                "de.~.vm.Javac.compile(JavaFileObject...)",
                Logger.DEBUG,
                "Error on line %d in %s" + diagnostic.getLineNumber() + diagnostic);
      }
    }
    try {
      stdFileManager.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return error;
  }