Package org.h2.util
Class SourceCompiler
java.lang.Object
org.h2.util.SourceCompiler
This class allows to convert source code to a class. It uses one class loader
per class.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static class
An in-memory class file manager.private static final class
Access the Groovy compiler using reflection, so that we do not gain a compile-time dependency unnecessarily.(package private) static class
An in-memory java class object.(package private) static class
An in-memory java source file object. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String
The class name to byte code map.(package private) final Map
<String, CompiledScript> The class name to compiled scripts map.(package private) static final JavaCompiler
The "com.sun.tools.javac.Main" (if available).private static final Class
<?> The class name to source code map.(package private) boolean
Whether to use the ToolProvider.getSystemJavaCompiler(). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
copyInThread
(InputStream in, OutputStream out) private static int
Class
<?> Get the class object for the given name.getCompiledScript
(String packageAndClassName) Get the compiled script.(package private) static String
getCompleteSourceCode
(String packageName, String className, String source) Get the complete source code (including package name, imports, and so on).Get the first public static method of the given class.private static void
handleSyntaxError
(String output, int exitStatus) private static boolean
isGroovySource
(String source) private static boolean
isJavascriptSource
(String source) static boolean
isJavaxScriptSource
(String source) Whether the passed source can be compiled usingScriptEngineManager
.private static boolean
isRubySource
(String source) (package private) byte[]
javacCompile
(String packageName, String className, String source) Compile the given class.private static void
javacProcess
(Path javaFile) private static void
(package private) Class
<?> javaxToolsJavac
(String packageName, String className, String source) Compile using the standard java compiler.void
setJavaSystemCompiler
(boolean enabled) Enable or disable the usage of the Java system compiler.void
Set the source code for the specified class.
-
Field Details
-
JAVA_COMPILER
The "com.sun.tools.javac.Main" (if available). -
JAVAC_SUN
-
COMPILE_DIR
-
sources
The class name to source code map. -
compiled
The class name to byte code map. -
compiledScripts
The class name to compiled scripts map. -
useJavaSystemCompiler
boolean useJavaSystemCompilerWhether to use the ToolProvider.getSystemJavaCompiler().
-
-
Constructor Details
-
SourceCompiler
public SourceCompiler()
-
-
Method Details
-
setSource
Set the source code for the specified class. This will reset all compiled classes.- Parameters:
className
- the class namesource
- 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
Get the class object for the given name.- Parameters:
packageAndClassName
- the class name- Returns:
- the class
- Throws:
ClassNotFoundException
- on failure
-
isGroovySource
-
isJavascriptSource
-
isRubySource
-
isJavaxScriptSource
Whether the passed source can be compiled usingScriptEngineManager
.- Parameters:
source
- the source to test.- Returns:
true
ifgetCompiledScript(String)
can be called.
-
getCompiledScript
Get the compiled script.- Parameters:
packageAndClassName
- the package and class name- Returns:
- the compiled script
- Throws:
ScriptException
- on failure
-
getMethod
Get the first public static method of the given class.- Parameters:
className
- the class name- Returns:
- the method name
- Throws:
ClassNotFoundException
- on failure
-
javacCompile
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 nameclassName
- the class namesource
- the source code- Returns:
- the class file
-
getCompleteSourceCode
Get the complete source code (including package name, imports, and so on).- Parameters:
packageName
- the package nameclassName
- the class namesource
- the (possibly shortened) source code- Returns:
- the full source code
-
javaxToolsJavac
Compile using the standard java compiler.- Parameters:
packageName
- the package nameclassName
- the class namesource
- the source code- Returns:
- the class
-
javacProcess
-
exec
-
copyInThread
-
javacSun
-
handleSyntaxError
-