public class CharSequenceCompiler<T> extends Object implements MemoryCompiler
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.)| 构造器和说明 |
|---|
CharSequenceCompiler(ClassLoader loader,
Iterable<String> options)
Construct a new instance which delegates to the named class loader.
|
| 限定符和类型 | 方法和说明 |
|---|---|
Map<String,Class<T>> |
compile(Map<String,CharSequence> classes,
DiagnosticCollector<JavaFileObject> diagnosticsList)
Compile multiple Java source strings and return a Map containing the
resulting classes.
|
Class<T> |
compile(String qualifiedClassName,
CharSequence javaSource,
DiagnosticCollector<JavaFileObject> diagnosticsList,
Class<?>... types)
Compile Java source in javaSource and return the resulting
class.
|
Class<?> |
compile(String qualifiedClassName,
String javaSource) |
ClassLoader |
getClassLoader() |
Class<T> |
loadClass(String qualifiedClassName)
Load a class that was generated by this instance or accessible from its
parent class loader.
|
public CharSequenceCompiler(ClassLoader loader, Iterable<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 javacIllegalStateException - if the Java compiler cannot be loaded.public Class<?> compile(String qualifiedClassName, String javaSource) throws Exception
compile 在接口中 MemoryCompilerqualifiedClassName - example: com.rgt.slg.gameserver.script.MemoryCompilerjavaSource - 源码Exceptionpublic Class<T> compile(String qualifiedClassName, CharSequence javaSource, DiagnosticCollector<JavaFileObject> diagnosticsList, Class<?>... types) throws net.jueb.util4j.hotSwap.memoryScript.impl.CharSequenceCompilerException, ClassCastException
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.net.jueb.util4j.hotSwap.memoryScript.impl.CharSequenceCompilerException - if the source cannot be compiled - for example, if it contains
syntax or semantic errors or if dependent classes cannot be
found.ClassCastException - if the generated class is not assignable to all the optional
types.public Map<String,Class<T>> compile(Map<String,CharSequence> classes, DiagnosticCollector<JavaFileObject> diagnosticsList) throws net.jueb.util4j.hotSwap.memoryScript.impl.CharSequenceCompilerException
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.net.jueb.util4j.hotSwap.memoryScript.impl.CharSequenceCompilerException - if the source cannot be compiledpublic Class<T> loadClass(String qualifiedClassName) throws 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 loadClassNotFoundException - if no such class is found.public ClassLoader getClassLoader()
Copyright © 2020. All rights reserved.