Class JavacCompiler

java.lang.Object
org.codehaus.plexus.compiler.AbstractCompiler
org.codehaus.plexus.compiler.javac.JavacCompiler
All Implemented Interfaces:
Compiler

@Named("javac") @Singleton public class JavacCompiler extends AbstractCompiler
  • Field Details

    • WARNING_PREFIXES

      private static final String[] WARNING_PREFIXES
    • NOTE_PREFIXES

      private static final String[] NOTE_PREFIXES
    • MISC_PREFIXES

      private static final String[] MISC_PREFIXES
    • LOCK

      private static final Object LOCK
    • JAVAC_CLASSNAME

      private static final String JAVAC_CLASSNAME
      See Also:
    • javacClass

      private volatile Class<?> javacClass
    • javacClasses

      private final Deque<Class<?>> javacClasses
    • inProcessCompiler

      @Inject private InProcessCompiler inProcessCompiler
    • STACK_TRACE_FIRST_LINE

      private static final Pattern STACK_TRACE_FIRST_LINE
    • STACK_TRACE_OTHER_LINE

      private static final Pattern STACK_TRACE_OTHER_LINE
  • Constructor Details

    • JavacCompiler

      public JavacCompiler()
  • Method Details

    • getCompilerId

      public String getCompilerId()
      Specified by:
      getCompilerId in class AbstractCompiler
    • performCompile

      public CompilerResult performCompile(CompilerConfiguration config) throws CompilerException
      Description copied from interface: Compiler
      Performs the compilation of the project. Clients must implement this method.
      Specified by:
      performCompile in interface Compiler
      Overrides:
      performCompile in class AbstractCompiler
      Parameters:
      config - the configuration description of the compilation to perform
      Returns:
      the result of the compilation returned by the language processor
      Throws:
      CompilerException
    • inProcessCompiler

      protected InProcessCompiler inProcessCompiler()
    • isJava16

      protected static boolean isJava16()
    • createCommandLine

      public String[] createCommandLine(CompilerConfiguration config) throws CompilerException
      Description copied from interface: Compiler
      Create the command line that would be executed using this configuration. If this particular compiler has no concept of a command line then returns null.
      Parameters:
      config - the CompilerConfiguration describing the compilation
      Returns:
      an array of Strings that make up the command line, or null if this compiler has no concept of command line
      Throws:
      CompilerException - if there was an error generating the command line
    • buildCompilerArguments

      public static String[] buildCompilerArguments(CompilerConfiguration config, String[] sourceFiles)
    • isPreJava14

      private static boolean isPreJava14(CompilerConfiguration config)
      Determine if the compiler is a version prior to 1.4. This is needed as 1.3 and earlier did not support -source or -encoding parameters
      Parameters:
      config - The compiler configuration to test.
      Returns:
      true if the compiler configuration represents a Java 1.4 compiler or later, false otherwise
    • isPreJava16

      private static boolean isPreJava16(CompilerConfiguration config)
      Determine if the compiler is a version prior to 1.6. This is needed for annotation processing parameters.
      Parameters:
      config - The compiler configuration to test.
      Returns:
      true if the compiler configuration represents a Java 1.6 compiler or later, false otherwise
    • isPreJava18

      private static boolean isPreJava18(CompilerConfiguration config)
    • isPreJava9

      private static boolean isPreJava9(CompilerConfiguration config)
    • suppressSource

      private static boolean suppressSource(CompilerConfiguration config)
    • suppressEncoding

      private static boolean suppressEncoding(CompilerConfiguration config)
    • compileOutOfProcess

      protected CompilerResult compileOutOfProcess(CompilerConfiguration config, String executable, String[] args) throws CompilerException
      Compile the java sources in a external process, calling an external executable, like javac.
      Parameters:
      config - compiler configuration
      executable - name of the executable to launch
      args - arguments for the executable launched
      Returns:
      a CompilerResult object encapsulating the result of the compilation and any compiler messages
      Throws:
      CompilerException
    • compileInProcess

      CompilerResult compileInProcess(String[] args, CompilerConfiguration config) throws CompilerException
      Compile the java sources in the current JVM, without calling an external executable, using com.sun.tools.javac.Main class
      Parameters:
      args - arguments for the compiler as they would be used in the command line javac
      config - compiler configuration
      Returns:
      a CompilerResult object encapsulating the result of the compilation and any compiler messages
      Throws:
      CompilerException
    • compileInProcessWithProperClassloader

      protected CompilerResult compileInProcessWithProperClassloader(Class<?> javacClass, String[] args) throws CompilerException
      Throws:
      CompilerException
    • compileInProcess0

      private static CompilerResult compileInProcess0(Class<?> javacClass, String[] args) throws CompilerException
      Helper method for compileInProcess()
      Throws:
      CompilerException
    • parseModernStream

      static List<CompilerMessage> parseModernStream(int exitCode, BufferedReader input) throws IOException
      Parse the output from the compiler into a list of CompilerMessage objects
      Parameters:
      exitCode - The exit code of javac.
      input - The output of the compiler
      Returns:
      List of CompilerMessage objects
      Throws:
      IOException
    • isMisc

      private static boolean isMisc(String line)
    • isNote

      private static boolean isNote(String line)
    • startsWithPrefix

      private static boolean startsWithPrefix(String line, String[] prefixes)
    • parseModernError

      static CompilerMessage parseModernError(int exitCode, String error)
      Construct a CompilerMessage object from a line of the compiler output
      Parameters:
      exitCode - The exit code from javac.
      error - output line from the compiler
      Returns:
      the CompilerMessage object
    • getWarnPrefix

      private static String getWarnPrefix(String msg)
    • createFileWithArguments

      private File createFileWithArguments(String[] args, String outputDirectory) throws IOException
      put args into a temp file to be referenced using the @ option in javac command line
      Parameters:
      args -
      Returns:
      the temporary file wth the arguments
      Throws:
      IOException
    • getJavacExecutable

      private static String getJavacExecutable() throws IOException
      Get the path of the javac tool executable: try to find it depending the OS or the java.home system property or the JAVA_HOME environment variable.
      Returns:
      the path of the Javadoc tool
      Throws:
      IOException - if not found
    • releaseJavaccClass

      private void releaseJavaccClass(Class<?> javaccClass, CompilerConfiguration compilerConfiguration)
    • getJavacClass

      private Class<?> getJavacClass(CompilerConfiguration compilerConfiguration) throws CompilerException
      Find the main class of JavaC. Return the same class for subsequent calls.
      Returns:
      the non-null class.
      Throws:
      CompilerException - if the class has not been found.
    • createJavacClass

      protected Class<?> createJavacClass() throws CompilerException
      Helper method for create Javac class
      Throws:
      CompilerException