public class CharSequenceCompiler<T>
extends java.lang.Object
CharSequence
, returning a Java
Class
instance that may be instantiated. This class is a Facade
around JavaCompiler
for a narrower use case, but a bit easier to use.Constructor and Description |
---|
CharSequenceCompiler(ProxyClassLoader loader,
java.lang.Iterable<java.lang.String> options)
Construct a new instance which delegates to the named class loader.
|
Modifier and Type | Method and Description |
---|---|
java.util.Map<java.lang.String,java.lang.Class<T>> |
compile(java.util.Map<java.lang.String,java.lang.CharSequence> classes,
javax.tools.DiagnosticCollector<javax.tools.JavaFileObject> diagnosticsList)
Compile multiple Java source strings and return a Map containing the
resulting classes.
|
java.lang.Class<T> |
compile(java.lang.String qualifiedClassName,
java.lang.CharSequence javaSource,
javax.tools.DiagnosticCollector<javax.tools.JavaFileObject> diagnosticsList,
java.lang.Class<?>... types)
Compile Java source in
javaSource and return the resulting
class.Thread safety: this method is thread safe if the javaSource
and diagnosticsList are isolated to this thread. |
java.lang.ClassLoader |
getClassLoader() |
java.lang.Class<T> |
loadClass(java.lang.String qualifiedClassName)
Load a class that was generated by this instance or accessible from its
parent class loader.
|
public CharSequenceCompiler(ProxyClassLoader loader, java.lang.Iterable<java.lang.String> options)
loader
- the application ClassLoader. The compiler will look through to
this // class loader for dependent classesoptions
- The compiler options (such as "-target" "1.5"). See the usage
for javacjava.lang.IllegalStateException
- if the Java compiler cannot be loaded.public java.lang.Class<T> compile(java.lang.String qualifiedClassName, java.lang.CharSequence javaSource, javax.tools.DiagnosticCollector<javax.tools.JavaFileObject> diagnosticsList, java.lang.Class<?>... types) throws CharSequenceCompilerException, java.lang.ClassCastException
javaSource
and return the resulting
class.javaSource
and diagnosticsList
are isolated to this thread.qualifiedClassName
- The fully qualified class name.javaSource
- Complete java source, including a package statement and a class,
interface, or annotation declaration.diagnosticsList
- Any diagnostics generated by compiling the source are added to
this collector.types
- zero or more Class objects representing classes or interfaces
that the resulting class must be assignable (castable) to.CharSequenceCompilerException
- if the source cannot be compiled - for example, if it contains
syntax or semantic errors or if dependent classes cannot be
found.java.lang.ClassCastException
- if the generated class is not assignable to all the optional
types
.public java.util.Map<java.lang.String,java.lang.Class<T>> compile(java.util.Map<java.lang.String,java.lang.CharSequence> classes, javax.tools.DiagnosticCollector<javax.tools.JavaFileObject> diagnosticsList) throws CharSequenceCompilerException
classes
and
diagnosticsList
are isolated to this thread.classes
- A Map whose keys are qualified class names and whose values are
the Java source strings containing the definition of the class.
A map value may be null, indicating that compiled class is
expected, although no source exists for it (it may be a
non-public class contained in one of the other strings.)diagnosticsList
- Any diagnostics generated by compiling the source are added to
this list.classes
; the
values are the corresponding Class objects.CharSequenceCompilerException
- if the source cannot be compiledpublic java.lang.Class<T> loadClass(java.lang.String qualifiedClassName) throws java.lang.ClassNotFoundException
compile()
,
for example if the primary class contained nested classes or additional
non-public classes.qualifiedClassName
- the name of the compiled class you wish to loadqualifiedClassName
java.lang.ClassNotFoundException
- if no such class is found.public java.lang.ClassLoader getClassLoader()