Example #1
0
  public static String[] buildCompilerArguments(
      CompilerConfiguration config, String[] sourceFiles) {
    List args = new ArrayList();

    // ----------------------------------------------------------------------
    // Set output
    // ----------------------------------------------------------------------

    File destinationDir = new File(config.getOutputLocation());

    args.add("-d");

    args.add(destinationDir.getAbsolutePath());

    // ----------------------------------------------------------------------
    // Set the class and source paths
    // ----------------------------------------------------------------------

    List classpathEntries = config.getClasspathEntries();
    if (classpathEntries != null && !classpathEntries.isEmpty()) {
      args.add("-classpath");

      args.add(getPathString(classpathEntries));
    }

    List sourceLocations = config.getSourceLocations();
    if (sourceLocations != null && !sourceLocations.isEmpty()) {
      args.add("-sourcepath");

      args.add(getPathString(sourceLocations));
    }

    for (int i = 0; i < sourceFiles.length; i++) {
      args.add(sourceFiles[i]);
    }

    if (config.isOptimize()) {
      args.add("-O");
    }

    if (config.isDebug()) {
      args.add("-g");
    }

    if (config.isVerbose()) {
      args.add("-verbose");
    }

    if (config.isShowDeprecation()) {
      args.add("-deprecation");

      // This is required to actually display the deprecation messages
      config.setShowWarnings(true);
    }

    if (!config.isShowWarnings()) {
      args.add("-nowarn");
    }

    // TODO: this could be much improved
    if (StringUtils.isEmpty(config.getTargetVersion())) {
      // Required, or it defaults to the target of your JDK (eg 1.5)
      args.add("-target");
      args.add("1.1");
    } else {
      args.add("-target");
      args.add(config.getTargetVersion());
    }

    if (!suppressSource(config) && StringUtils.isEmpty(config.getSourceVersion())) {
      // If omitted, later JDKs complain about a 1.1 target
      args.add("-source");
      args.add("1.3");
    } else if (!suppressSource(config)) {
      args.add("-source");
      args.add(config.getSourceVersion());
    }

    if (!suppressEncoding(config) && !StringUtils.isEmpty(config.getSourceEncoding())) {
      args.add("-encoding");
      args.add(config.getSourceEncoding());
    }

    for (Iterator it = config.getCustomCompilerArguments().entrySet().iterator(); it.hasNext(); ) {
      Map.Entry entry = (Map.Entry) it.next();

      String key = (String) entry.getKey();

      if (StringUtils.isEmpty(key)) {
        continue;
      }

      args.add(key);

      String value = (String) entry.getValue();

      if (StringUtils.isEmpty(value)) {
        continue;
      }

      args.add(value);
    }

    return (String[]) args.toArray(new String[args.size()]);
  }