SourceCompiler

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

Methods
static String getCompleteSourceCode(String packageName, String className, String source)
Get the complete source code (including package name, imports, and so on).
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
static boolean isJavaxScriptSource(String source)
Whether the passed source can be compiled using
static boolean isJavaxScriptSource(String source)
Whether the passed source can be compiled using {@link javax.script.ScriptEngineManager}.
Parameters:
source - the source to test.
Returns:
true if {@link #getCompiledScript(String)} can be called.
Class getClass(String packageAndClassName)
Get the class object for the given name.
Class getClass(String packageAndClassName) throws ClassNotFoundException
Get the class object for the given name.
Parameters:
packageAndClassName - the class name
Returns:
the class
CompiledScript getCompiledScript(String packageAndClassName)
Get the compiled script.
CompiledScript getCompiledScript(String packageAndClassName) throws ScriptException
Get the compiled script.
Parameters:
packageAndClassName - the package and class name
Returns:
the compiled script
Method getMethod(String className)
Get the first public static method of the given class.
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
byte[] javacCompile(String packageName, String className, String source)
Compile the given class.
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
Class javaxToolsJavac(String packageName, String className, String source)
Compile using the standard java compiler.
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
void setJavaSystemCompiler(boolean enabled)
Enable or disable the usage of the Java system compiler.
void setJavaSystemCompiler(boolean enabled)
Enable or disable the usage of the Java system compiler.
Parameters:
enabled - true to enable
void setSource(String className, String source)
Set the source code for the specified class.
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

Fields
static JavaCompiler JAVA_COMPILER
static HashMap compiled
static Map compiledScripts
static HashMap sources
static boolean useJavaSystemCompiler

JAVA_COMPILER

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

compiled

The class name to byte code map.

compiledScripts

The class name to compiled scripts map.

sources

The class name to source code map.

useJavaSystemCompiler

Whether to use the ToolProvider.getSystemJavaCompiler().