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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
JavacCompiler.JavaVersion
Represents a particular Java version (through their according version prefixes)
-
Field Summary
Fields Modifier and Type Field Description private InProcessCompiler
inProcessCompiler
private static java.util.regex.Pattern
JAVA_MAJOR_AND_MINOR_VERSION_PATTERN
private static java.lang.String
JAVAC_CLASSNAME
private static java.util.regex.Pattern
JAVAC_OR_JVM_ERROR
private java.lang.Class<?>
javacClass
private java.util.Deque<java.lang.Class<?>>
javacClasses
private static java.lang.Object
LOCK
private static java.lang.String[]
MISC_PREFIXES
private static java.lang.String[]
NOTE_PREFIXES
private static java.util.regex.Pattern
STACK_TRACE_FIRST_LINE
private static java.util.regex.Pattern
STACK_TRACE_OTHER_LINE
private static java.util.Map<java.lang.String,java.lang.String>
VERSION_PER_EXECUTABLE
Cache of javac version per executable (never invalidated)private static java.lang.String[]
WARNING_PREFIXES
-
Fields inherited from class org.codehaus.plexus.compiler.AbstractCompiler
EOL, PS
-
-
Constructor Summary
Constructors Constructor Description JavacCompiler()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.lang.String[]
buildCompilerArguments(CompilerConfiguration config, java.lang.String[] sourceFiles, java.lang.String javacVersion)
(package private) CompilerResult
compileInProcess(java.lang.String[] args, CompilerConfiguration config)
Compile the java sources in the current JVM, without calling an external executable, usingcom.sun.tools.javac.Main
classprivate static CompilerResult
compileInProcess0(java.lang.Class<?> javacClass, java.lang.String[] args)
Helper method for compileInProcess()protected CompilerResult
compileInProcessWithProperClassloader(java.lang.Class<?> javacClass, java.lang.String[] args)
protected CompilerResult
compileOutOfProcess(CompilerConfiguration config, java.lang.String executable, java.lang.String[] args)
Compile the java sources in a external process, calling an external executable, like javac.java.lang.String[]
createCommandLine(CompilerConfiguration config)
Create the command line that would be executed using this configuration.private java.io.File
createFileWithArguments(java.lang.String[] args, java.lang.String outputDirectory)
put args into a temp file to be referenced using the @ option in javac command lineprotected java.lang.Class<?>
createJavacClass()
Helper method for create Javac class(package private) static java.lang.String
extractMajorAndMinorVersion(java.lang.String text)
java.lang.String
getCompilerId()
private java.lang.String
getInProcessJavacVersion()
private java.lang.Class<?>
getJavacClass(CompilerConfiguration compilerConfiguration)
Find the main class of JavaC.private static java.lang.String
getJavacExecutable()
Get the path of the javac tool executable: try to find it depending the OS or thejava.home
system property or theJAVA_HOME
environment variable.protected java.lang.String
getJavacExecutable(CompilerConfiguration config)
Get the path of the javac tool executable to use.private java.lang.String
getOutOfProcessJavacVersion(java.lang.String executable)
private static java.lang.String
getWarnPrefix(java.lang.String msg)
protected static boolean
hasJavaxToolProvider()
protected InProcessCompiler
inProcessCompiler()
private static boolean
isMisc(java.lang.String line)
private static boolean
isNote(java.lang.String line)
(package private) static CompilerMessage
parseModernError(int exitCode, java.lang.String error)
Construct a CompilerMessage object from a line of the compiler output(package private) static java.util.List<CompilerMessage>
parseModernStream(int exitCode, java.io.BufferedReader input)
Parse the output from the compiler into a list of CompilerMessage objectsCompilerResult
performCompile(CompilerConfiguration config)
Performs the compilation of the project.private void
releaseJavaccClass(java.lang.Class<?> javaccClass, CompilerConfiguration compilerConfiguration)
private static boolean
startsWithPrefix(java.lang.String line, java.lang.String[] prefixes)
-
Methods inherited from class org.codehaus.plexus.compiler.AbstractCompiler
canUpdateTarget, getCompilerOutputStyle, getInputFileEnding, getLog, getLogger, getOutputFile, getOutputFileEnding, getPathString, getSourceFiles, getSourceFilesForSourceRoot, logCompiling, makeClassName
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.codehaus.plexus.compiler.Compiler
supportsIncrementalCompilation
-
-
-
-
Field Detail
-
WARNING_PREFIXES
private static final java.lang.String[] WARNING_PREFIXES
-
NOTE_PREFIXES
private static final java.lang.String[] NOTE_PREFIXES
-
MISC_PREFIXES
private static final java.lang.String[] MISC_PREFIXES
-
LOCK
private static final java.lang.Object LOCK
-
JAVAC_CLASSNAME
private static final java.lang.String JAVAC_CLASSNAME
- See Also:
- Constant Field Values
-
javacClass
private volatile java.lang.Class<?> javacClass
-
javacClasses
private final java.util.Deque<java.lang.Class<?>> javacClasses
-
JAVA_MAJOR_AND_MINOR_VERSION_PATTERN
private static final java.util.regex.Pattern JAVA_MAJOR_AND_MINOR_VERSION_PATTERN
-
VERSION_PER_EXECUTABLE
private static final java.util.Map<java.lang.String,java.lang.String> VERSION_PER_EXECUTABLE
Cache of javac version per executable (never invalidated)
-
inProcessCompiler
@Inject private InProcessCompiler inProcessCompiler
-
STACK_TRACE_FIRST_LINE
private static final java.util.regex.Pattern STACK_TRACE_FIRST_LINE
-
STACK_TRACE_OTHER_LINE
private static final java.util.regex.Pattern STACK_TRACE_OTHER_LINE
-
JAVAC_OR_JVM_ERROR
private static final java.util.regex.Pattern JAVAC_OR_JVM_ERROR
-
-
Method Detail
-
getCompilerId
public java.lang.String getCompilerId()
- Specified by:
getCompilerId
in classAbstractCompiler
-
getInProcessJavacVersion
private java.lang.String getInProcessJavacVersion() throws CompilerException
- Throws:
CompilerException
-
getOutOfProcessJavacVersion
private java.lang.String getOutOfProcessJavacVersion(java.lang.String executable) throws CompilerException
- Throws:
CompilerException
-
extractMajorAndMinorVersion
static java.lang.String extractMajorAndMinorVersion(java.lang.String text)
-
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 interfaceCompiler
- Overrides:
performCompile
in classAbstractCompiler
- 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()
-
hasJavaxToolProvider
protected static boolean hasJavaxToolProvider()
- Returns:
true
if the current context class loader has access tojavax.tools.ToolProvider
-
createCommandLine
public java.lang.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 java.lang.String[] buildCompilerArguments(CompilerConfiguration config, java.lang.String[] sourceFiles, java.lang.String javacVersion)
-
compileOutOfProcess
protected CompilerResult compileOutOfProcess(CompilerConfiguration config, java.lang.String executable, java.lang.String[] args) throws CompilerException
Compile the java sources in a external process, calling an external executable, like javac.- Parameters:
config
- compiler configurationexecutable
- name of the executable to launchargs
- arguments for the executable launched- Returns:
- a CompilerResult object encapsulating the result of the compilation and any compiler messages
- Throws:
CompilerException
-
compileInProcess
CompilerResult compileInProcess(java.lang.String[] args, CompilerConfiguration config) throws CompilerException
Compile the java sources in the current JVM, without calling an external executable, usingcom.sun.tools.javac.Main
class- Parameters:
args
- arguments for the compiler as they would be used in the command line javacconfig
- compiler configuration- Returns:
- a CompilerResult object encapsulating the result of the compilation and any compiler messages
- Throws:
CompilerException
-
compileInProcessWithProperClassloader
protected CompilerResult compileInProcessWithProperClassloader(java.lang.Class<?> javacClass, java.lang.String[] args) throws CompilerException
- Throws:
CompilerException
-
compileInProcess0
private static CompilerResult compileInProcess0(java.lang.Class<?> javacClass, java.lang.String[] args) throws CompilerException
Helper method for compileInProcess()- Throws:
CompilerException
-
parseModernStream
static java.util.List<CompilerMessage> parseModernStream(int exitCode, java.io.BufferedReader input) throws java.io.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:
java.io.IOException
-
isMisc
private static boolean isMisc(java.lang.String line)
-
isNote
private static boolean isNote(java.lang.String line)
-
startsWithPrefix
private static boolean startsWithPrefix(java.lang.String line, java.lang.String[] prefixes)
-
parseModernError
static CompilerMessage parseModernError(int exitCode, java.lang.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 java.lang.String getWarnPrefix(java.lang.String msg)
-
createFileWithArguments
private java.io.File createFileWithArguments(java.lang.String[] args, java.lang.String outputDirectory) throws java.io.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:
java.io.IOException
-
getJavacExecutable
protected java.lang.String getJavacExecutable(CompilerConfiguration config)
Get the path of the javac tool executable to use. Either given through explicit configuration or viagetJavacExecutable()
.- Parameters:
config
- the configuration- Returns:
- the path of the javac tool
-
getJavacExecutable
private static java.lang.String getJavacExecutable() throws java.io.IOException
Get the path of the javac tool executable: try to find it depending the OS or thejava.home
system property or theJAVA_HOME
environment variable.- Returns:
- the path of the javac tool
- Throws:
java.io.IOException
- if not found
-
releaseJavaccClass
private void releaseJavaccClass(java.lang.Class<?> javaccClass, CompilerConfiguration compilerConfiguration)
-
getJavacClass
private java.lang.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 java.lang.Class<?> createJavacClass() throws CompilerException
Helper method for create Javac class- Throws:
CompilerException
-
-