Class SourceCompiler

java.lang.Object
org.jppf.utils.compilation.SourceCompiler
All Implemented Interfaces:
Closeable, AutoCloseable

public class SourceCompiler extends Object implements Closeable
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 Details

  • Method Details

    • compileToFile

      public void compileToFile(Map<String,CharSequence> sources, File classesDir) throws Exception
      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 class mypackage.MyClass is compiled, the resulting class file will be at classesDir/mypackage/MyClass.class.
      Throws:
      Exception - if any error occurs.
    • compileToMemory

      public Map<String,byte[]> compileToMemory(Map<String,CharSequence> sources) throws Exception
      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

      public void close() throws IOException
      Close this source compiler and its associated file manager.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if any I/O error occurs.
    • getDiagnostics

      public List<Diagnostic<JavaFileObject>> getDiagnostics()
      Get the errors and warnings that occured during the last compilation operation.
      Returns:
      a list of Diagnostic objects.
    • getClassloader

      public ClassLoader getClassloader()
      Get the class loader used to load the classes generated by this SourceCompiler.
      Returns:
      a ClassLoader instance.
    • isCompilerAvailable

      public static boolean isCompilerAvailable()
      Determines whether a compiler is available for this platform.
      Returns:
      true if a compiler is available, false otherwise.