Qore jni Module  2.0.0
org.qore.jni.compiler.QoreJavaCompiler< T > Class Template Reference

Public Member Methods

 QoreJavaCompiler ()
 
 QoreJavaCompiler (Iterable< String > options)
 
 QoreJavaCompiler (String[] options)
 
 QoreJavaCompiler (QoreURLClassLoader loader, Iterable< String > options)
 
synchronized CompilerOutput< T > compile (final String qualifiedClassName, final String javaSource, final Class<?>... types) throws QoreJavaCompilerException, ClassCastException
 
synchronized CompilerOutput< T > compile (final String qualifiedClassName, final CharSequence javaSource, final Class<?>... types) throws QoreJavaCompilerException, ClassCastException
 
synchronized CompilerOutput< T > compile (final String qualifiedClassName, final String javaSource, final DiagnosticCollector< JavaFileObject > diagnosticsList, final Class<?>... types) throws QoreJavaCompilerException, ClassCastException
 
synchronized CompilerOutput< T > compile (final String qualifiedClassName, final CharSequence javaSource, final DiagnosticCollector< JavaFileObject > diagnosticsList, final Class<?>... types) throws QoreJavaCompilerException, ClassCastException
 
synchronized Map< String, CompilerOutput< T > > compile (final Map< String, CharSequence > classes, final DiagnosticCollector< JavaFileObject > diagnosticsList) throws QoreJavaCompilerException
 
Class< T > loadClass (final String qualifiedClassName) throws ClassNotFoundException
 
void addClassPath (String path)
 
ClassLoader getClassLoader ()
 

Detailed Description

Compile a String or other CharSequence, returning a Java Class instance that may be instantiated as well as the raw bytecode. 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
QoreJavaCompiler<Object> compiler = new QoreJavaCompiler<MyInterface>(classLoader,
      null);
try {
   Class<MyInterface> newClass = compiler.compile("com.mypackage.NewClass",
         stringContainingSourceForNewClass, diagnostics, MyInterface).cls;
   MyInterface instance = newClass.newInstance();
   instance.someOperation(someArgs);
} catch (QoreJavaCompilerException 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.)

Author
David J. Biesack, adapted for Qore by David Nichols

Constructor & Destructor Documentation

◆ QoreJavaCompiler() [1/4]

Construct a new instance which delegates to a new Qore classloader.

Exceptions
IllegalStateExceptionif the Java compiler cannot be loaded.

◆ QoreJavaCompiler() [2/4]

org.qore.jni.compiler.QoreJavaCompiler< T >.QoreJavaCompiler ( Iterable< String >  options)
inline

Construct a new instance which delegates to a new Qore classloader.

Parameters
optionsThe compiler options (such as "-target" "1.5"). See the usage for javac
Exceptions
IllegalStateExceptionif the Java compiler cannot be loaded.

◆ QoreJavaCompiler() [3/4]

org.qore.jni.compiler.QoreJavaCompiler< T >.QoreJavaCompiler ( String[]  options)
inline

Construct a new instance which delegates to a new Qore classloader.

Parameters
optionsThe compiler options (such as "-target" "1.5"). See the usage for javac
Exceptions
IllegalStateExceptionif the Java compiler cannot be loaded.

◆ QoreJavaCompiler() [4/4]

org.qore.jni.compiler.QoreJavaCompiler< T >.QoreJavaCompiler ( QoreURLClassLoader  loader,
Iterable< String >  options 
)
inline

Construct a new instance which delegates to the named class loader.

Parameters
loaderthe application ClassLoader. The compiler will look through to this // class loader for dependent classes
optionsThe compiler options (such as "-target" "1.5"). See the usage for javac
Exceptions
IllegalStateExceptionif the Java compiler cannot be loaded.

Member Function Documentation

◆ addClassPath()

void org.qore.jni.compiler.QoreJavaCompiler< T >.addClassPath ( String  path)
inline

Add a path to the classpath

◆ compile() [1/5]

synchronized Map<String, CompilerOutput<T> > org.qore.jni.compiler.QoreJavaCompiler< T >.compile ( final Map< String, CharSequence >  classes,
final DiagnosticCollector< JavaFileObject >  diagnosticsList 
) throws QoreJavaCompilerException
inline

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
classesA 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.)
diagnosticsListAny diagnostics generated by compiling the source are added to this list.
Returns
A mapping of qualified class names to their corresponding Class and file data. The map has keys for all output classes produced; not just the classes prsent in the input classes; values are the corresponding Class and file data objects.
Exceptions
QoreJavaCompilerExceptionif the source cannot be compiled

◆ compile() [2/5]

synchronized CompilerOutput<T> org.qore.jni.compiler.QoreJavaCompiler< T >.compile ( final String  qualifiedClassName,
final CharSequence  javaSource,
final Class<?>...  types 
) throws QoreJavaCompilerException, ClassCastException
inline

Compile Java source in javaSource</name> and return the resulting class.

Thread safety: this method is thread safe if the javaSource is isolated to this thread.

Parameters
qualifiedClassNameThe fully qualified class name.
javaSourceComplete java source, including a package statement and a class, interface, or annotation declaration.
typeszero or more Class objects representing classes or interfaces that the resulting class must be assignable (castable) to.
Returns
the Class and file data generated by compiling the source
Exceptions
QoreJavaCompilerExceptionif the source cannot be compiled - for example, if it contains syntax or semantic errors or if dependent classes cannot be found.
ClassCastExceptionif the generated class is not assignable to all the optional types.

◆ compile() [3/5]

synchronized CompilerOutput<T> org.qore.jni.compiler.QoreJavaCompiler< T >.compile ( final String  qualifiedClassName,
final CharSequence  javaSource,
final DiagnosticCollector< JavaFileObject >  diagnosticsList,
final Class<?>...  types 
) throws QoreJavaCompilerException, ClassCastException
inline

Compile Java source in javaSource</name> and return the resulting class.

Thread safety: this method is thread safe if the javaSource and diagnosticsList are isolated to this thread.

Parameters
qualifiedClassNameThe fully qualified class name.
javaSourceComplete java source, including a package statement and a class, interface, or annotation declaration.
diagnosticsListAny diagnostics generated by compiling the source are added to this collector.
typeszero or more Class objects representing classes or interfaces that the resulting class must be assignable (castable) to.
Returns
the Class and file data generated by compiling the source
Exceptions
QoreJavaCompilerExceptionif the source cannot be compiled - for example, if it contains syntax or semantic errors or if dependent classes cannot be found.
ClassCastExceptionif the generated class is not assignable to all the optional types.

◆ compile() [4/5]

synchronized CompilerOutput<T> org.qore.jni.compiler.QoreJavaCompiler< T >.compile ( final String  qualifiedClassName,
final String  javaSource,
final Class<?>...  types 
) throws QoreJavaCompilerException, ClassCastException
inline

Compile Java source in javaSource</name> and return the resulting class.

Thread safety: this method is thread safe if the javaSource is isolated to this thread.

Parameters
qualifiedClassNameThe fully qualified class name.
javaSourceComplete java source, including a package statement and a class, interface, or annotation declaration.
typeszero or more Class objects representing classes or interfaces that the resulting class must be assignable (castable) to.
Returns
the Class and file data generated by compiling the source
Exceptions
QoreJavaCompilerExceptionif the source cannot be compiled - for example, if it contains syntax or semantic errors or if dependent classes cannot be found.
ClassCastExceptionif the generated class is not assignable to all the optional types.

◆ compile() [5/5]

synchronized CompilerOutput<T> org.qore.jni.compiler.QoreJavaCompiler< T >.compile ( final String  qualifiedClassName,
final String  javaSource,
final DiagnosticCollector< JavaFileObject >  diagnosticsList,
final Class<?>...  types 
) throws QoreJavaCompilerException, ClassCastException
inline

Compile Java source in javaSource</name> and return the resulting class.

Thread safety: this method is thread safe if the javaSource and diagnosticsList are isolated to this thread.

Parameters
qualifiedClassNameThe fully qualified class name.
javaSourceComplete java source, including a package statement and a class, interface, or annotation declaration.
diagnosticsListAny diagnostics generated by compiling the source are added to this collector.
typeszero or more Class objects representing classes or interfaces that the resulting class must be assignable (castable) to.
Returns
the Class and file data generated by compiling the source
Exceptions
QoreJavaCompilerExceptionif the source cannot be compiled - for example, if it contains syntax or semantic errors or if dependent classes cannot be found.
ClassCastExceptionif the generated class is not assignable to all the optional types.

◆ getClassLoader()

ClassLoader org.qore.jni.compiler.QoreJavaCompiler< T >.getClassLoader ( )
inline
Returns
This compiler's class loader.

◆ loadClass()

Class<T> org.qore.jni.compiler.QoreJavaCompiler< T >.loadClass ( final String  qualifiedClassName) throws ClassNotFoundException
inline

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 by compile(), for example if the primary class contained nested classes or additional non-public classes.

Parameters
qualifiedClassNamethe name of the compiled class you wish to load
Returns
a Class instance named by qualifiedClassName
Exceptions
ClassNotFoundExceptionif no such class is found.

The documentation for this class was generated from the following file: