jasext.jhplotserver
Class CharSequenceCompiler<T>
- java.lang.Object
-
- jasext.jhplotserver.CharSequenceCompiler<T>
-
public class CharSequenceCompiler<T> extends java.lang.ObjectCompile a String or otherCharSequence, returning a JavaClassinstance that may be instantiated. This class is a Facade aroundJavaCompilerfor 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 implementsCharSequence. If you implement your own, it must be thread safe (preferably, immutable.)
-
-
Constructor Summary
Constructors Constructor and Description 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
All Methods Instance Methods Concrete Methods 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.java.lang.ClassLoadergetClassLoader()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.
-
-
-
Constructor Detail
-
CharSequenceCompiler
public CharSequenceCompiler(java.lang.ClassLoader loader, java.lang.Iterable<java.lang.String> options)Construct a new instance which delegates to the named class loader.- Parameters:
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- Throws:
java.lang.IllegalStateException- if the Java compiler cannot be loaded.
-
-
Method Detail
-
compile
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
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.
- Parameters:
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.- Returns:
- a Class which is generated by compiling the source
- Throws:
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.
-
compile
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
Compile multiple Java source strings and return a Map containing the resulting classes.Thread safety: this method is thread safe if the classes and diagnosticsList are isolated to this thread.
- Parameters:
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.- Returns:
- A mapping of qualified class names to their corresponding classes. The map has the same keys as the input classes; the values are the corresponding Class objects.
- Throws:
CharSequenceCompilerException- if the source cannot be compiled
-
loadClass
public java.lang.Class<T> loadClass(java.lang.String qualifiedClassName) throws java.lang.ClassNotFoundException
Load a class that was generated by this instance or accessible from its parent class loader. Use this method if you need access to additional classes compiled bycompile(), for example if the primary class contained nested classes or additional non-public classes.- Parameters:
qualifiedClassName- the name of the compiled class you wish to load- Returns:
- a Class instance named by qualifiedClassName
- Throws:
java.lang.ClassNotFoundException- if no such class is found.
-
getClassLoader
public java.lang.ClassLoader getClassLoader()
- Returns:
- This compiler's class loader.
-
-
DataMelt 3.0 © DataMelt by jWork.ORG