|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.nhncorp.neptune.client.procedure.CharSequenceCompiler<T>
public class CharSequenceCompiler<T>
Compile a String or other 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.
To compile a String containing source for a Java class which implements MyInterface:
ClassLoader classLoader = MyClass.class.getClassLoader(); // optional; null is also OK
List<Diagnostic> diagnostics = new ArrayList<Diagnostic>(); // optional; null is also OK
JavaStringCompiler<Object> compiler = new JavaStringCompiler<MyInterface>(classLoader,
null);
try {
Class<MyInterface> newClass = compiler.compile("com.mypackage.NewClass",
stringContaininSourceForNewClass, diagnostics, MyInterface);
MyInterface instance = newClass.newInstance();
instance.someOperation(someArgs);
} catch (JavaStringCompilerException e) {
handle(e);
} catch (IllegalAccessException e) {
handle(e);
}
The source can be in a String, StringBuffer, or your own class which
implements CharSequence. If you implement your own, it must be
thread safe (preferably, immutable.)
| Constructor Summary | |
|---|---|
CharSequenceCompiler(java.lang.ClassLoader loader,
java.lang.Iterable<java.lang.String> options)
Construct a new instance which delegates to the named class loader. |
|
| Method Summary | |
|---|---|
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. |
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. |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public CharSequenceCompiler(java.lang.ClassLoader 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 javac
java.lang.IllegalStateException - if the Java compiler cannot be loaded.| Method Detail |
|---|
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
Thread safety: this method is thread safe if the 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
Thread safety: this method is thread safe if the 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.
CharSequenceCompilerException - if the source cannot be compiled
public 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 load
java.lang.ClassNotFoundException - if no such class is found.public java.lang.ClassLoader getClassLoader()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||