Class CompilingClassLoader
java.lang.Object
java.lang.ClassLoader
com.google.common.truth.codegen.CompilingClassLoader
This is a Java ClassLoader that will attempt to load a class from a string of source code.
Example
String className = "com.foo.MyClass";
String classSource =
"package com.foo;\n" +
"public class MyClass implements Runnable {\n" +
" @Override public void run() {\n" +
" log(\"Hello world\");\n" +
" }\n" +
"}";
// Load class from source.
ClassLoader classLoader = new CompilingClassLoader(
parentClassLoader, className, classSource);
Class myClass = classLoader.loadClass(className);
// Use it.
Runnable instance = (Runnable)myClass.newInstance();
instance.run();
Only one chunk of source can be compiled per instance of CompilingClassLoader. If you need to
compile more, create multiple CompilingClassLoader instances.
Uses Java 1.6's in built compiler API.
If the class cannot be compiled, loadClass() will throw a ClassNotFoundException and log the
compile errors to System.err. If you don't want the messages logged, or want to explicitly handle
the messages you can provide your own DiagnosticListener through
{#setDiagnosticListener()}.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThrown when code cannot be compiled.private classProvides an in-memory representation of JavaFileManager abstraction, so we do not need to write any files to disk.private static class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map<String, ByteArrayOutputStream> private static final URI -
Constructor Summary
ConstructorsConstructorDescriptionCompilingClassLoader(ClassLoader parent, String className, String sourceCode, DiagnosticListener<JavaFileObject> diagnosticListener) -
Method Summary
Modifier and TypeMethodDescriptionprivate booleancompileSourceCodeToByteCode(String className, String sourceCode, DiagnosticListener<JavaFileObject> diagnosticListener) Class<?> Override ClassLoader's class resolving method.Methods inherited from class ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findResource, findResources, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
Field Details
-
byteCodeForClasses
-
EMPTY_URI
-
-
Constructor Details
-
CompilingClassLoader
public CompilingClassLoader(ClassLoader parent, String className, String sourceCode, DiagnosticListener<JavaFileObject> diagnosticListener) throws CompilingClassLoader.CompilerException - Parameters:
parent- Parent classloader to resolve dependencies from.className- Name of class to compile. eg. "com.foo.MyClass".sourceCode- Java source for class. e.g. "package com.foo; class MyClass { ... }".diagnosticListener- Notified of compiler errors (may be null).- Throws:
CompilingClassLoader.CompilerException
-
-
Method Details
-
findClass
Override ClassLoader's class resolving method. Don't call this directly, instead useClassLoader.loadClass(String).- Overrides:
findClassin classClassLoader- Throws:
ClassNotFoundException
-
compileSourceCodeToByteCode
private boolean compileSourceCodeToByteCode(String className, String sourceCode, DiagnosticListener<JavaFileObject> diagnosticListener) - Returns:
- Whether compilation was successful.
-