Class CompilerUtil


  • public final class CompilerUtil
    extends java.lang.Object
    Utilities for compiling Java source files at runtime.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String TEMP_DIR_NAME
      Temporary directory for files.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private CompilerUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean compile​(javax.tools.DiagnosticCollector<javax.tools.JavaFileObject> diagnostics, javax.tools.JavaCompiler.CompilationTask task)
      Execute compilation task and report errors if it fails.
      static java.lang.Class<?> compileAndLoad​(java.lang.String className, javax.tools.DiagnosticCollector<javax.tools.JavaFileObject> diagnostics, javax.tools.JavaFileManager fileManager, javax.tools.JavaCompiler.CompilationTask task)
      Compile and load a class.
      static java.lang.Class<?> compileInMemory​(java.lang.String className, java.util.Map<java.lang.String,​java.lang.CharSequence> sources)
      Compile a Map of source files in-memory resulting in a Class which is named.
      static java.lang.Class<?> compileOnDisk​(java.lang.String className, java.util.Map<java.lang.String,​java.lang.CharSequence> sources)
      Compile a Map of source files on disk resulting in a Class which is named.
      static java.util.Collection<java.io.File> persist​(java.util.Map<java.lang.String,​java.lang.CharSequence> sources)
      Persist source files to disc.
      private static java.util.Collection<CharSequenceJavaFileObject> wrap​(java.util.Map<java.lang.String,​java.lang.CharSequence> sources)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • TEMP_DIR_NAME

        private static final java.lang.String TEMP_DIR_NAME
        Temporary directory for files.
    • Constructor Detail

      • CompilerUtil

        private CompilerUtil()
    • Method Detail

      • compileInMemory

        public static java.lang.Class<?> compileInMemory​(java.lang.String className,
                                                         java.util.Map<java.lang.String,​java.lang.CharSequence> sources)
                                                  throws java.lang.ClassNotFoundException
        Compile a Map of source files in-memory resulting in a Class which is named.
        Parameters:
        className - to return after compilation.
        sources - to be compiled.
        Returns:
        the named class that is the result of the compilation.
        Throws:
        java.lang.ClassNotFoundException - of the named class cannot be found.
      • compileOnDisk

        public static java.lang.Class<?> compileOnDisk​(java.lang.String className,
                                                       java.util.Map<java.lang.String,​java.lang.CharSequence> sources)
                                                throws java.lang.ClassNotFoundException,
                                                       java.io.IOException
        Compile a Map of source files on disk resulting in a Class which is named.
        Parameters:
        className - to return after compilation.
        sources - to be compiled.
        Returns:
        the named class that is the result of the compilation.
        Throws:
        java.lang.ClassNotFoundException - of the named class cannot be found.
        java.io.IOException - if an error occurs when writing to disk.
      • compileAndLoad

        public static java.lang.Class<?> compileAndLoad​(java.lang.String className,
                                                        javax.tools.DiagnosticCollector<javax.tools.JavaFileObject> diagnostics,
                                                        javax.tools.JavaFileManager fileManager,
                                                        javax.tools.JavaCompiler.CompilationTask task)
                                                 throws java.lang.ClassNotFoundException
        Compile and load a class.
        Parameters:
        className - name of the class to compile.
        diagnostics - attached to the compilation task.
        fileManager - to load compiled class from disk.
        task - compilation task.
        Returns:
        Class for the compiled class or null if compilation fails.
        Throws:
        java.lang.ClassNotFoundException - if compiled class was not loaded.
      • compile

        public static boolean compile​(javax.tools.DiagnosticCollector<javax.tools.JavaFileObject> diagnostics,
                                      javax.tools.JavaCompiler.CompilationTask task)
        Execute compilation task and report errors if it fails.
        Parameters:
        diagnostics - attached to the compilation task.
        task - compilation to be executed.
        Returns:
        true if compilation succeeds.
      • persist

        public static java.util.Collection<java.io.File> persist​(java.util.Map<java.lang.String,​java.lang.CharSequence> sources)
                                                          throws java.io.IOException
        Persist source files to disc.
        Parameters:
        sources - to persist.
        Returns:
        a collection of File objects pointing to the persisted sources.
        Throws:
        java.io.IOException - in case of I/O errors.
      • wrap

        private static java.util.Collection<CharSequenceJavaFileObject> wrap​(java.util.Map<java.lang.String,​java.lang.CharSequence> sources)