Top Description Inners Fields Constructors Methods
org.h2.util

public Class SourceCompiler

extends Object
Class Inheritance
Imports
java.io.BufferedReader, .ByteArrayOutputStream, .IOException, .InputStream, .OutputStream, .PrintStream, .StringReader, .StringWriter, java.lang.reflect.Array, .Method, .Modifier, java.net.URI, java.nio.charset.StandardCharsets, java.nio.file.Files, .Path, .Paths, java.security.SecureClassLoader, java.util.ArrayList, .HashMap, .Map, java.util.function.Predicate, javax.script.Bindings, .Compilable, .CompiledScript, .ScriptContext, .ScriptEngine, .ScriptEngineManager, .ScriptException, javax.tools.FileObject, .ForwardingJavaFileManager, .JavaCompiler, .JavaFileManager, .JavaFileObject, .JavaFileObject.Kind, .SimpleJavaFileObject, .StandardJavaFileManager, .ToolProvider, org.h2.api.ErrorCode, org.h2.engine.SysProperties, org.h2.message.DbException

This class allows to convert source code to a class. It uses one class loader per class.

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
SourceCompiler.ClassFileManager

An in-memory class file manager.

private static class
SourceCompiler.GroovyCompiler

Access the Groovy compiler using reflection, so that we do not gain a compile-time dependency unnecessarily.

pack-priv static class
SourceCompiler.JavaClassObject

An in-memory java class object.

pack-priv static class
SourceCompiler.StringJavaFileObject

An in-memory java source file object.

Field Summary

Modifier and TypeField and Description
private static final String
pack-priv final HashMap<String, Class<?>>
compiled

The class name to byte code map.

pack-priv final Map<String, CompiledScript>
compiledScripts

The class name to compiled scripts map.

pack-priv static final JavaCompiler
JAVA_COMPILER

The "com.sun.tools.javac.Main" (if available).

private static final Class<?>
pack-priv final HashMap<String, String>
sources

The class name to source code map.

pack-priv boolean
useJavaSystemCompiler

Whether to use the ToolProvider.getSystemJavaCompiler().

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static void
copyInThread(final InputStream in, final OutputStream out)

private static int
exec(String... args)

public Class<?>

Returns:

the class
getClass
(String
the class name
packageAndClassName
)

Get the class object for the given name.

public CompiledScript

Returns:

the compiled script
getCompiledScript
(String
the package and class name
packageAndClassName
)

Get the compiled script.

pack-priv static String

Returns:

the full source code
getCompleteSourceCode
(String
the package name
packageName
,
String
the class name
className
,
String
the (possibly shortened) source code
source
)

Get the complete source code (including package name, imports, and so on).

public Method

Returns:

the method name
getMethod
(String
the class name
className
)

Get the first public static method of the given class.

private static void
handleSyntaxError(String output, int exitStatus)

private static boolean
private static boolean
public static boolean

Returns:

true if getCompiledScript(String) can be called.
isJavaxScriptSource
(String
the source to test.
source
)

Whether the passed source can be compiled using javax.script.ScriptEngineManager.

private static boolean
pack-priv byte[]

Returns:

the class file
javacCompile
(String
the package name
packageName
,
String
the class name
className
,
String
the source code
source
)

Compile the given class.

private static void
javacProcess(Path javaFile)

private static synchronized void
javacSun(Path javaFile)

pack-priv Class<?>

Returns:

the class
javaxToolsJavac
(String
the package name
packageName
,
String
the class name
className
,
String
the source code
source
)

Compile using the standard java compiler.

public void
setJavaSystemCompiler(boolean
true to enable
enabled
)

Enable or disable the usage of the Java system compiler.

public void
setSource(String
the class name
className
,
String
the source code
source
)

Set the source code for the specified class.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

COMPILE_DIRback to summary
private static final String COMPILE_DIR
compiledback to summary
pack-priv final HashMap<String, Class<?>> compiled

The class name to byte code map.

compiledScriptsback to summary
pack-priv final Map<String, CompiledScript> compiledScripts

The class name to compiled scripts map.

JAVA_COMPILERback to summary
pack-priv static final JavaCompiler JAVA_COMPILER

The "com.sun.tools.javac.Main" (if available).

JAVAC_SUNback to summary
private static final Class<?> JAVAC_SUN
sourcesback to summary
pack-priv final HashMap<String, String> sources

The class name to source code map.

useJavaSystemCompilerback to summary
pack-priv boolean useJavaSystemCompiler

Whether to use the ToolProvider.getSystemJavaCompiler().

Constructor Detail

SourceCompilerback to summary
public SourceCompiler()

Method Detail

copyInThreadback to summary
private static void copyInThread(final InputStream in, final OutputStream out)
execback to summary
private static int exec(String... args)
getClassback to summary
public Class<?> getClass(String packageAndClassName) throws ClassNotFoundException

Get the class object for the given name.

Parameters
packageAndClassName:String

the class name

Returns:Class<?>

the class

Exceptions
ClassNotFoundException:
on failure
getCompiledScriptback to summary
public CompiledScript getCompiledScript(String packageAndClassName) throws ScriptException

Get the compiled script.

Parameters
packageAndClassName:String

the package and class name

Returns:CompiledScript

the compiled script

Exceptions
ScriptException:
on failure
getCompleteSourceCodeback to summary
pack-priv static String getCompleteSourceCode(String packageName, String className, String source)

Get the complete source code (including package name, imports, and so on).

Parameters
packageName:String

the package name

className:String

the class name

source:String

the (possibly shortened) source code

Returns:String

the full source code

getMethodback to summary
public Method getMethod(String className) throws ClassNotFoundException

Get the first public static method of the given class.

Parameters
className:String

the class name

Returns:Method

the method name

Exceptions
ClassNotFoundException:
on failure
handleSyntaxErrorback to summary
private static void handleSyntaxError(String output, int exitStatus)
isGroovySourceback to summary
private static boolean isGroovySource(String source)
isJavascriptSourceback to summary
private static boolean isJavascriptSource(String source)
isJavaxScriptSourceback to summary
public static boolean isJavaxScriptSource(String source)

Whether the passed source can be compiled using javax.script.ScriptEngineManager.

Parameters
source:String

the source to test.

Returns:boolean

true if getCompiledScript(String) can be called.

isRubySourceback to summary
private static boolean isRubySource(String source)
javacCompileback to summary
pack-priv 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:String

the package name

className:String

the class name

source:String

the source code

Returns:byte[]

the class file

javacProcessback to summary
private static void javacProcess(Path javaFile)
javacSunback to summary
private static synchronized void javacSun(Path javaFile)
javaxToolsJavacback to summary
pack-priv Class<?> javaxToolsJavac(String packageName, String className, String source)

Compile using the standard java compiler.

Parameters
packageName:String

the package name

className:String

the class name

source:String

the source code

Returns:Class<?>

the class

setJavaSystemCompilerback to summary
public void setJavaSystemCompiler(boolean enabled)

Enable or disable the usage of the Java system compiler.

Parameters
enabled:boolean

true to enable

setSourceback to summary
public void setSource(String className, String source)

Set the source code for the specified class. This will reset all compiled classes.

Parameters
className:String

the class name

source:String

the source code

org.h2.util back to summary

pack-priv Class SourceCompiler.ClassFileManager

extends ForwardingJavaFileManager<StandardJavaFileManager>
Class Inheritance

An in-memory class file manager.

Field Summary

Modifier and TypeField and Description
private SecureClassLoader
pack-priv Map<String, SourceCompiler.JavaClassObject>
classObjectsByName

We use map because there can be nested, anonymous etc classes.

Inherited from javax.tools.ForwardingJavaFileManager:
fileManager

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public ClassLoader
getClassLoader(JavaFileManager.Location
a location
location
)

Overrides javax.tools.ForwardingJavaFileManager.getClassLoader.

Implements javax.tools.JavaFileManager.getClassLoader.

Returns a class loader for loading plug-ins from the given package-oriented location.

public JavaFileObject
getJavaFileForOutput(JavaFileManager.Location
a package-oriented location
location
,
String
the name of a class
className
,
JavaFileObject.Kind
the kind of file, must be one of SOURCE or CLASS
kind
,
FileObject
a file object to be used as hint for placement; might be null
sibling
)

Overrides javax.tools.ForwardingJavaFileManager.getJavaFileForOutput.

Implements javax.tools.JavaFileManager.getJavaFileForOutput.

Returns a file object for output representing the specified class of the specified kind in the given package-oriented location.

Inherited from javax.tools.ForwardingJavaFileManager:
closecontainsflushgetFileForInputgetFileForOutputgetFileForOutputForOriginatingFilesgetJavaFileForInputgetJavaFileForOutputForOriginatingFilesgetLocationForModulegetLocationForModulegetServiceLoaderhandleOptionhasLocationinferBinaryNameinferModuleNameisSameFileisSupportedOptionlistlistLocationsForModules

Field Detail

classLoaderback to summary
private SecureClassLoader classLoader
classObjectsByNameback to summary
pack-priv Map<String, SourceCompiler.JavaClassObject> classObjectsByName

We use map because there can be nested, anonymous etc classes.

Constructor Detail

ClassFileManagerback to summary
public ClassFileManager(StandardJavaFileManager standardManager)

Method Detail

getClassLoaderback to summary
public ClassLoader getClassLoader(JavaFileManager.Location location)

Overrides javax.tools.ForwardingJavaFileManager.getClassLoader.

Implements javax.tools.JavaFileManager.getClassLoader.

Doc from javax.tools.JavaFileManager.getClassLoader.

Returns a class loader for loading plug-ins from the given package-oriented location. For example, to load annotation processors, a compiler will request a class loader for the ANNOTATION_PROCESSOR_PATH location.

Parameters
location:JavaFileManager.Location

a location

Returns:ClassLoader

a class loader for the given location; or null if loading plug-ins from the given location is disabled or if the location is not known

Annotations
@Override
getJavaFileForOutputback to summary
public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException

Overrides javax.tools.ForwardingJavaFileManager.getJavaFileForOutput.

Implements javax.tools.JavaFileManager.getJavaFileForOutput.

Doc from javax.tools.JavaFileManager.getJavaFileForOutput.

Returns a file object for output representing the specified class of the specified kind in the given package-oriented location.

Optionally, this file manager might consider the sibling as a hint for where to place the output. The exact semantics of this hint is unspecified. The JDK compiler, javac, for example, will place class files in the same directories as originating source files unless a class file output directory is provided. To facilitate this behavior, javac might provide the originating source file as sibling when calling this method.

Parameters
location:JavaFileManager.Location

a package-oriented location

className:String

the name of a class

kind:JavaFileObject.Kind

the kind of file, must be one of SOURCE or CLASS

sibling:FileObject

a file object to be used as hint for placement; might be null

Returns:JavaFileObject

a file object for output

Annotations
@Override
Exceptions
IOException:
if an I/O error occurred, or if close has been called and this file manager cannot be reopened
org.h2.util back to summary

private final Class SourceCompiler.GroovyCompiler

extends Object
Class Inheritance

Access the Groovy compiler using reflection, so that we do not gain a compile-time dependency unnecessarily.

Field Summary

Modifier and TypeField and Description
private static final Throwable
private static final Object

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Class<?>
parseClass(String source, String packageAndClassName)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INIT_FAIL_EXCEPTIONback to summary
private static final Throwable INIT_FAIL_EXCEPTION
LOADERback to summary
private static final Object LOADER

Constructor Detail

GroovyCompilerback to summary
private GroovyCompiler()

Method Detail

parseClassback to summary
public static Class<?> parseClass(String source, String packageAndClassName)
org.h2.util back to summary

pack-priv Class SourceCompiler.JavaClassObject

extends SimpleJavaFileObject
Class Inheritance

An in-memory java class object.

Field Summary

Modifier and TypeField and Description
private final ByteArrayOutputStream
Inherited from javax.tools.SimpleJavaFileObject:
kinduri

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public byte[]
public OutputStream
openOutputStream()

Overrides javax.tools.SimpleJavaFileObject.openOutputStream.

Implements javax.tools.FileObject.openOutputStream.

Returns an OutputStream for this file object.

Inherited from javax.tools.SimpleJavaFileObject:
deleteforSourcegetAccessLevelgetCharContentgetKindgetLastModifiedgetNamegetNestingKindisNameCompatibleopenInputStreamopenReaderopenWritertoStringtoUri

Field Detail

outback to summary
private final ByteArrayOutputStream out

Constructor Detail

JavaClassObjectback to summary
public JavaClassObject(String name, JavaFileObject.Kind kind)

Method Detail

getBytesback to summary
public byte[] getBytes()
openOutputStreamback to summary
public OutputStream openOutputStream() throws IOException

Overrides javax.tools.SimpleJavaFileObject.openOutputStream.

Implements javax.tools.FileObject.openOutputStream.

Doc from javax.tools.SimpleJavaFileObject.openOutputStream.

Returns an OutputStream for this file object.

Returns:OutputStream

Doc from javax.tools.FileObject.openOutputStream.

an OutputStream

Annotations
@Override
Exceptions
IOException:

Doc from javax.tools.FileObject.openOutputStream.

if an I/O error occurred

org.h2.util back to summary

pack-priv Class SourceCompiler.StringJavaFileObject

extends SimpleJavaFileObject
Class Inheritance

An in-memory java source file object.

Field Summary

Modifier and TypeField and Description
private final String
Inherited from javax.tools.SimpleJavaFileObject:
kinduri

Constructor Summary

AccessConstructor and Description
public
StringJavaFileObject(String className, String sourceCode)

Method Summary

Modifier and TypeMethod and Description
public CharSequence
getCharContent(boolean
ignore encoding errors if true
ignoreEncodingErrors
)

Overrides javax.tools.SimpleJavaFileObject.getCharContent.

Implements javax.tools.FileObject.getCharContent.

Returns the character content of this file object, if available.

Inherited from javax.tools.SimpleJavaFileObject:
deleteforSourcegetAccessLevelgetKindgetLastModifiedgetNamegetNestingKindisNameCompatibleopenInputStreamopenOutputStreamopenReaderopenWritertoStringtoUri

Field Detail

sourceCodeback to summary
private final String sourceCode

Constructor Detail

StringJavaFileObjectback to summary
public StringJavaFileObject(String className, String sourceCode)

Method Detail

getCharContentback to summary
public CharSequence getCharContent(boolean ignoreEncodingErrors)

Overrides javax.tools.SimpleJavaFileObject.getCharContent.

Implements javax.tools.FileObject.getCharContent.

Doc from javax.tools.SimpleJavaFileObject.getCharContent.

Returns the character content of this file object, if available. Any byte that cannot be decoded will be replaced by the default translation character. In addition, a diagnostic may be reported unless ignoreEncodingErrors is true.

Parameters
ignoreEncodingErrors:boolean

Doc from javax.tools.FileObject.getCharContent.

ignore encoding errors if true

Returns:CharSequence

Doc from javax.tools.FileObject.getCharContent.

a CharSequence if available; null otherwise

Annotations
@Override