Package org.jppf.utils.compilation
Class SourceCompiler
java.lang.Object
org.jppf.utils.compilation.SourceCompiler
- All Implemented Interfaces:
Closeable,AutoCloseable
This class encapsulates a Java platform compiler and provides an API to compile
java sources provided as string to either the file system or an in-memory location.
- Author:
- Laurent Cohen
-
Constructor Summary
ConstructorsConstructorDescriptionInitialize aSourceCompiler.SourceCompiler(ClassLoader parentCL) Initialize a SourceCompiler with the specified parent class loader. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close this source compiler and its associated file manager.voidcompileToFile(Map<String, CharSequence> sources, File classesDir) Compile the specified sources to the file system.compileToMemory(Map<String, CharSequence> sources) Compile the specified sources to memory.Get the class loader used to load the classes generated by thisSourceCompiler.Get the errors and warnings that occured during the last compilation operation.static booleanDetermines whether a compiler is available for this platform.
-
Constructor Details
-
SourceCompiler
Initialize aSourceCompiler.- Throws:
UnsupportedOperationException- if no compiler is available for this platform.
-
SourceCompiler
Initialize a SourceCompiler with the specified parent class loader.- Parameters:
parentCL- the parent class loader used to build a class loader specific for this source compiler.- Throws:
UnsupportedOperationException- if no compiler is available for this platform.
-
-
Method Details
-
compileToFile
Compile the specified sources to the file system.- Parameters:
sources- a mapping of class names to their source code.classesDir- the root directory where the classes are stored. For instance, if a classmypackage.MyClassis compiled, the resulting class file will be atclassesDir/mypackage/MyClass.class.- Throws:
Exception- if any error occurs.
-
compileToMemory
Compile the specified sources to memory.- Parameters:
sources- a mapping of class names to their source code.- Returns:
- a mapping of the class names to their generated bytecode.
- Throws:
Exception- if any error occurs.
-
close
Close this source compiler and its associated file manager.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if any I/O error occurs.
-
getDiagnostics
Get the errors and warnings that occured during the last compilation operation.- Returns:
- a list of
Diagnosticobjects.
-
getClassloader
Get the class loader used to load the classes generated by thisSourceCompiler.- Returns:
- a
ClassLoaderinstance.
-
isCompilerAvailable
public static boolean isCompilerAvailable()Determines whether a compiler is available for this platform.- Returns:
trueif a compiler is available,falseotherwise.
-