Package org.h2.util

Class SourceCompiler

java.lang.Object
org.h2.util.SourceCompiler

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

    • JAVA_COMPILER

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

      private static final Class<?> JAVAC_SUN
    • COMPILE_DIR

      private static final String COMPILE_DIR
    • sources

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

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

      final Map<String,CompiledScript> compiledScripts
      The class name to compiled scripts map.
    • useJavaSystemCompiler

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

    • SourceCompiler

      public SourceCompiler()
  • Method Details

    • setSource

      public void setSource(String className, 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 Class<?> getClass(String packageAndClassName) throws ClassNotFoundException
      Get the class object for the given name.
      Parameters:
      packageAndClassName - the class name
      Returns:
      the class
      Throws:
      ClassNotFoundException - on failure
    • isGroovySource

      private static boolean isGroovySource(String source)
    • isJavascriptSource

      private static boolean isJavascriptSource(String source)
    • isRubySource

      private static boolean isRubySource(String source)
    • isJavaxScriptSource

      public static boolean isJavaxScriptSource(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 CompiledScript getCompiledScript(String packageAndClassName) throws ScriptException
      Get the compiled script.
      Parameters:
      packageAndClassName - the package and class name
      Returns:
      the compiled script
      Throws:
      ScriptException - on failure
    • getMethod

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

      byte[] javacCompile(String packageName, String className, 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 String getCompleteSourceCode(String packageName, String className, 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

      Class<?> javaxToolsJavac(String packageName, String className, 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(Path javaFile)
    • exec

      private static int exec(String... args)
    • copyInThread

      private static void copyInThread(InputStream in, OutputStream out)
    • javacSun

      private static void javacSun(Path javaFile)
    • handleSyntaxError

      private static void handleSyntaxError(String output, int exitStatus)