public class DynamicJavaCode
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
DynamicJavaCode.CompileInformation |
static class |
DynamicJavaCode.FullClassName |
static class |
DynamicJavaCode.MainMethod |
static class |
DynamicJavaCode.NonExecutableMethodException |
Constructor and Description |
---|
DynamicJavaCode() |
Modifier and Type | Method and Description |
---|---|
DynamicJavaCode.CompileInformation |
compileJava(java.util.Collection<CompilableClass> classes,
CompilerOptions options)
Compiles the Java code contained in the given classes.
|
JavaResult |
execute(DynamicJavaCode.MainMethod method,
java.util.List<?> args)
Executes the given main method.
|
DynamicJavaCode.MainMethod |
getMainMethod(DynamicJavaCode.FullClassName className,
java.lang.String methodName,
DynamicJavaCode.CompileInformation information,
java.util.Collection<java.lang.Class<?>> validComponentTypes)
Get the main method from the class with the given name, getting the method with the given name.
|
public JavaResult execute(DynamicJavaCode.MainMethod method, java.util.List<?> args) throws DynamicJavaCode.NonExecutableMethodException
method
- The main method which should be executed.args
- The arguments to the method.JavaResult.getSystemOut()
returns a String containing the output which was printed to System.out
- If an exception is thrown during the method, JavaResult.getCausedException()
will contain that exception.
If no exception is thrown, it will be null.
- If the main method returns a result, and no exception is thrown, then JavaResult.getReturnValue()
will contain
the returned object: JavaResult.ReturnValue.exists()
will be true, and JavaResult.ReturnValue.getValue()
will contain the returned object.
NOTE: This object may still be null, in case the method returns null.DynamicJavaCode.NonExecutableMethodException
- If the given main method is not public, or the arguments given are not accepted by it.public DynamicJavaCode.CompileInformation compileJava(java.util.Collection<CompilableClass> classes, CompilerOptions options)
classes
- The classes which chould be compiled.options
- The options according to which to compile the code.DynamicJavaCode.CompileInformation.wasCompilationSuccessful()
to find out whether the compilation was successful. The class can then later be loaded only if compilation was successful.public DynamicJavaCode.MainMethod getMainMethod(DynamicJavaCode.FullClassName className, java.lang.String methodName, DynamicJavaCode.CompileInformation information, java.util.Collection<java.lang.Class<?>> validComponentTypes) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException
className
- The full name of the class containing the main method.methodName
- The name of the main method.information
- The CompileInformation as returned by DynamicJavaCode#compileJava(String, String)
.validComponentTypes
- The collection of types, of which the elements of the argument to the main method may be.MainMethod#getLoadedClass()
.java.lang.ClassNotFoundException
- If any of the given classes could not be loaded.java.lang.NoSuchMethodException
- If either the given class has no method with the given name, or if the method with the given name
in the class does not satisfy the properties of a main method:
- It must be static, or its declaring class must have an empty, public constructor.
- It must take a single argument. The argument must be of an array type. The collection of the allowed component types of the array is supplied as an argument
to this method as well.
- It must be public.