Package org.h2.util

Class SourceCompiler


  • public class SourceCompiler
    extends java.lang.Object
    This class allows to convert source code to a class. It uses one class loader per class.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String COMPILE_DIR  
      (package private) java.util.HashMap<java.lang.String,​java.lang.Class<?>> compiled
      The class name to byte code map.
      (package private) java.util.Map<java.lang.String,​javax.script.CompiledScript> compiledScripts
      The class name to compiled scripts map.
      (package private) static javax.tools.JavaCompiler JAVA_COMPILER
      The "com.sun.tools.javac.Main" (if available).
      private static java.lang.Class<?> JAVAC_SUN  
      (package private) java.util.HashMap<java.lang.String,​java.lang.String> sources
      The class name to source code map.
      (package private) boolean useJavaSystemCompiler
      Whether to use the ToolProvider.getSystemJavaCompiler().
    • Constructor Summary

      Constructors 
      Constructor Description
      SourceCompiler()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static void copyInThread​(java.io.InputStream in, java.io.OutputStream out)  
      private static int exec​(java.lang.String... args)  
      java.lang.Class<?> getClass​(java.lang.String packageAndClassName)
      Get the class object for the given name.
      javax.script.CompiledScript getCompiledScript​(java.lang.String packageAndClassName)
      Get the compiled script.
      (package private) static java.lang.String getCompleteSourceCode​(java.lang.String packageName, java.lang.String className, java.lang.String source)
      Get the complete source code (including package name, imports, and so on).
      java.lang.reflect.Method getMethod​(java.lang.String className)
      Get the first public static method of the given class.
      private static void handleSyntaxError​(java.lang.String output, int exitStatus)  
      private static boolean isGroovySource​(java.lang.String source)  
      private static boolean isJavascriptSource​(java.lang.String source)  
      static boolean isJavaxScriptSource​(java.lang.String source)
      Whether the passed source can be compiled using ScriptEngineManager.
      private static boolean isRubySource​(java.lang.String source)  
      (package private) byte[] javacCompile​(java.lang.String packageName, java.lang.String className, java.lang.String source)
      Compile the given class.
      private static void javacProcess​(java.nio.file.Path javaFile)  
      private static void javacSun​(java.nio.file.Path javaFile)  
      (package private) java.lang.Class<?> javaxToolsJavac​(java.lang.String packageName, java.lang.String className, java.lang.String source)
      Compile using the standard java compiler.
      void setJavaSystemCompiler​(boolean enabled)
      Enable or disable the usage of the Java system compiler.
      void setSource​(java.lang.String className, java.lang.String source)
      Set the source code for the specified class.
      • Methods inherited from class java.lang.Object

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

      • JAVA_COMPILER

        static final javax.tools.JavaCompiler JAVA_COMPILER
        The "com.sun.tools.javac.Main" (if available).
      • JAVAC_SUN

        private static final java.lang.Class<?> JAVAC_SUN
      • COMPILE_DIR

        private static final java.lang.String COMPILE_DIR
      • sources

        final java.util.HashMap<java.lang.String,​java.lang.String> sources
        The class name to source code map.
      • compiled

        final java.util.HashMap<java.lang.String,​java.lang.Class<?>> compiled
        The class name to byte code map.
      • compiledScripts

        final java.util.Map<java.lang.String,​javax.script.CompiledScript> compiledScripts
        The class name to compiled scripts map.
      • useJavaSystemCompiler

        boolean useJavaSystemCompiler
        Whether to use the ToolProvider.getSystemJavaCompiler().
    • Constructor Detail

      • SourceCompiler

        public SourceCompiler()
    • Method Detail

      • setSource

        public void setSource​(java.lang.String className,
                              java.lang.String source)
        Set the source code for the specified class. This will reset all compiled classes.
        Parameters:
        className - the class name
        source - the source code
      • setJavaSystemCompiler

        public void setJavaSystemCompiler​(boolean enabled)
        Enable or disable the usage of the Java system compiler.
        Parameters:
        enabled - true to enable
      • getClass

        public java.lang.Class<?> getClass​(java.lang.String packageAndClassName)
                                    throws java.lang.ClassNotFoundException
        Get the class object for the given name.
        Parameters:
        packageAndClassName - the class name
        Returns:
        the class
        Throws:
        java.lang.ClassNotFoundException - on failure
      • isGroovySource

        private static boolean isGroovySource​(java.lang.String source)
      • isJavascriptSource

        private static boolean isJavascriptSource​(java.lang.String source)
      • isRubySource

        private static boolean isRubySource​(java.lang.String source)
      • isJavaxScriptSource

        public static boolean isJavaxScriptSource​(java.lang.String source)
        Whether the passed source can be compiled using ScriptEngineManager.
        Parameters:
        source - the source to test.
        Returns:
        true if getCompiledScript(String) can be called.
      • getCompiledScript

        public javax.script.CompiledScript getCompiledScript​(java.lang.String packageAndClassName)
                                                      throws javax.script.ScriptException
        Get the compiled script.
        Parameters:
        packageAndClassName - the package and class name
        Returns:
        the compiled script
        Throws:
        javax.script.ScriptException - on failure
      • getMethod

        public java.lang.reflect.Method getMethod​(java.lang.String className)
                                           throws java.lang.ClassNotFoundException
        Get the first public static method of the given class.
        Parameters:
        className - the class name
        Returns:
        the method name
        Throws:
        java.lang.ClassNotFoundException - on failure
      • javacCompile

        byte[] javacCompile​(java.lang.String packageName,
                            java.lang.String className,
                            java.lang.String source)
        Compile the given class. This method tries to use the class "com.sun.tools.javac.Main" if available. If not, it tries to run "javac" in a separate process.
        Parameters:
        packageName - the package name
        className - the class name
        source - the source code
        Returns:
        the class file
      • getCompleteSourceCode

        static java.lang.String getCompleteSourceCode​(java.lang.String packageName,
                                                      java.lang.String className,
                                                      java.lang.String source)
        Get the complete source code (including package name, imports, and so on).
        Parameters:
        packageName - the package name
        className - the class name
        source - the (possibly shortened) source code
        Returns:
        the full source code
      • javaxToolsJavac

        java.lang.Class<?> javaxToolsJavac​(java.lang.String packageName,
                                           java.lang.String className,
                                           java.lang.String source)
        Compile using the standard java compiler.
        Parameters:
        packageName - the package name
        className - the class name
        source - the source code
        Returns:
        the class
      • javacProcess

        private static void javacProcess​(java.nio.file.Path javaFile)
      • exec

        private static int exec​(java.lang.String... args)
      • copyInThread

        private static void copyInThread​(java.io.InputStream in,
                                         java.io.OutputStream out)
      • javacSun

        private static void javacSun​(java.nio.file.Path javaFile)
      • handleSyntaxError

        private static void handleSyntaxError​(java.lang.String output,
                                              int exitStatus)