Package org.elasticsearch.test.compiler
Class InMemoryJavaCompiler
java.lang.Object
org.elasticsearch.test.compiler.InMemoryJavaCompiler
An in-memory java source code compiler. InMemoryJavaCompiler can be used for compiling source
code, represented as a CharSequence, to byte code, represented as a byte[].
The compiler will not use the file system at all, instead using a ByteArrayOutputStream for storing the byte code.
Example:
Map<String, CharSequence> sources = Map.of(
"module-info",
"""
module foo {
exports p;
}
""",
"p.Foo",
"""
package p;
public class Foo implements java.util.function.Supplier<String> {
@Override public String get() {
return "Hello World!";
}
}
"""
);
Map<String, byte[]> result = compile(sources);
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]compile(String className, CharSequence sourceCode, String... options) Compiles the class with the given name and source code.compile(Map<String, CharSequence> sources, String... options) Compiles the classes with the given names and source code.
-
Constructor Details
-
InMemoryJavaCompiler
public InMemoryJavaCompiler()
-
-
Method Details
-
compile
Compiles the classes with the given names and source code.- Parameters:
sources- A map of class names to source codeoptions- Additional command line options (optional)- Returns:
- A Map containing the resulting byte code from the compilation, one entry per class name
- Throws:
RuntimeException- If the compilation did not succeed
-
compile
Compiles the class with the given name and source code.- Parameters:
className- The name of the classsourceCode- The source code for the class with nameclassNameoptions- Additional command line options (optional)- Returns:
- The resulting byte code from the compilation
- Throws:
RuntimeException- If the compilation did not succeed
-