Class JCompilerCommandBuilder

java.lang.Object
org.matwoess.jsourceprofiler.common.JCompilerCommandBuilder

public class JCompilerCommandBuilder extends Object
Builder class for a javac command line with arguments. Used as a common abstraction to avoid hard-coding an array of javac command strings for ProcessBuilder usages.
  • Constructor Details

    • JCompilerCommandBuilder

      public JCompilerCommandBuilder()
  • Method Details

    • addSourceFile

      public JCompilerCommandBuilder addSourceFile(Path sourceFile)
      Register a source file that should be compiled.
      Parameters:
      sourceFile - target source file to compile
      Returns:
      the builder object itself for method chaining
    • setClassPath

      public JCompilerCommandBuilder setClassPath(Path classPath)
      Define the class path directory for the final javac command. Will be appended using the -cp option.
      Parameters:
      classPath - a directory that should be passed to the compiler as the class path option
      Returns:
      the builder object itself for method chaining
    • setDirectory

      public JCompilerCommandBuilder setDirectory(Path directory)
      Define the output directory for the javac command, specified by the -d option. Is used to place compiled .class files in a different directory than the instrumented files.
      Parameters:
      directory - the directory path where javac outputs are placed in
      Returns:
      the builder object itself for method chaining
    • addCompileArg

      public JCompilerCommandBuilder addCompileArg(String arg, String val)
      Specify additional compile options that will be added to the final command.

      The arg and value are always joined with " " together. The method can be called multiple times. Arguments will be added in-order.

      Parameters:
      arg - the java compile argument name (example: -target)
      val - the value associated with the argument
      Returns:
      the builder object itself for method chaining
    • build

      public String[] build()
      Returns aan array of strings that can be joined to a valid javac command line.

      The primary intended usage is to pass it to a ProcessBuilder. This is commonly done with the Util.runCommand(java.lang.String...) abstraction method.

      Returns:
      the string array to form a javac command, built by the setter methods