Interface IClassBodyEvaluator
-
- All Superinterfaces:
ICookable
- All Known Implementing Classes:
ClassBodyEvaluator
,ClassBodyEvaluator
public interface IClassBodyEvaluator extends ICookable
Parses a class body and returns it as aClass
object ready for use with java.lang.reflect.Example:
import java.util.*; static private int a = 1; private int b = 2; public void func(int c, int d) { return func2(c, d); } private static void func2(int e, int f) { return e * f; }
To set up an
IClassBodyEvaluator
object, proceed as follows:- Create an
IClassBodyEvaluator
-implementing class. - Configure the
IClassBodyEvaluator
by calling any of the following methods: setDefaultImports(String[])
setClassName(String)
setExtendedClass(Class)
setImplementedInterfaces(Class[])
setParentClassLoader(ClassLoader)
-
Call any of the
ICookable.cook(String, java.io.Reader)
methods to scan, parse, compile and load the class body into the JVM.
To compile a class body and immediately instantiate an object, the
createInstance(Reader)
methods can be used.The generated class may optionally extend/implement a given type; the returned instance can safely be type-cast to that
baseType
.Example:
public interface Foo { int bar(int a, int b); } ...
IClassBodyEvaluator
cbe =CompilerFactoryFactory
.getDefaultCompilerFactory
().newClassBodyEvaluator
(); Foo f = (Foo) ClassBodyEvaluator.createFastClassBodyEvaluator( new Scanner(null, new StringReader("public int bar(int a, int b) { return a + b; }")), Foo.class, // Base type to extend/implement (ClassLoader) null // Use current thread's context class loader ); System.out.println("1 + 2 = " + f.bar(1, 2));
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DEFAULT_CLASS_NAME
Default name for the generated class.
-
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description java.lang.Object
createInstance(java.io.Reader reader)
Scans, parses and compiles a class body from the tokens delivered by the the givenReader
, then creates and returns an instance of that class.java.lang.Class<?>
getClazz()
java.lang.String[]
getDefaultImports()
void
setClassName(java.lang.String className)
Sets the name of the generated class.void
setCompileErrorHandler(ErrorHandler compileErrorHandler)
Installs anErrorHandler
which is invoked during compilation on each error.void
setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)
Determines what kind of debugging information is included in the generates classes.void
setDefaultImports(java.lang.String... defaultImports)
"Default imports" add to the system import "java.lang", i.e. the evaluator may refer to classes imported by default imports without having to explicitly declare IMPORT statements.void
setExtendedClass(java.lang.Class<?> extendedClass)
Sets a particular superclass that the generated class will extend.void
setExtendedType(java.lang.Class<?> extendedClass)
Deprecated.UsesetExtendedClass(Class)
insteadvoid
setImplementedInterfaces(java.lang.Class<?>[] implementedInterfaces)
Sets a particular set of interfaces that the generated class will implement.void
setImplementedTypes(java.lang.Class<?>[] implementedInterfaces)
Deprecated.UsesetImplementedInterfaces(Class[])
insteadvoid
setParentClassLoader(java.lang.ClassLoader parentClassLoader)
The "parent class loader" is used to load referenced classes.void
setWarningHandler(WarningHandler warningHandler)
By default, warnings are discarded, but an application my install a customWarningHandler
.
-
-
-
Field Detail
-
DEFAULT_CLASS_NAME
static final java.lang.String DEFAULT_CLASS_NAME
Default name for the generated class.- See Also:
- Constant Field Values
-
-
Method Detail
-
setParentClassLoader
void setParentClassLoader(@Nullable java.lang.ClassLoader parentClassLoader)
The "parent class loader" is used to load referenced classes. Useful values are:System.getSystemClassLoader()
The running JVM's class path Thread.currentThread().getContextClassLoader()
ornull
The class loader effective for the invoking thread ClassLoaders.BOOTCLASSPATH_CLASS_LOADER
The running JVM's boot class path The parent class loader defaults to the current thread's context class loader.
-
setDebuggingInformation
void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars)
Determines what kind of debugging information is included in the generates classes. The default is typically "-g:none
".
-
setCompileErrorHandler
void setCompileErrorHandler(@Nullable ErrorHandler compileErrorHandler)
Installs anErrorHandler
which is invoked during compilation on each error. (By default, the compilation throws aCompileException
on the first error and terminates.)If the given
ErrorHandler
throws aCompileException
, then the compilation terminates and the exception is propagated.If the given
ErrorHandler
does not throw aCompileException
but completes normally, then the compilation may or may not continue, depending on the error. Iff the compilation completes normally but errors were reported, then it will throw aCompileException
indicating the number of errors.In other words: The
ErrorHandler
may throw aCompileException
or not, but the compilation will definitely throw aCompileException
if one or more compile errors have occurred.- Parameters:
compileErrorHandler
-null
to restore the default behavior (throwing aCompileException
)
-
setWarningHandler
void setWarningHandler(@Nullable WarningHandler warningHandler)
By default, warnings are discarded, but an application my install a customWarningHandler
.- Parameters:
warningHandler
-null
to indicate that no warnings be issued
-
setDefaultImports
void setDefaultImports(java.lang.String... defaultImports)
"Default imports" add to the system import "java.lang", i.e. the evaluator may refer to classes imported by default imports without having to explicitly declare IMPORT statements.Notice that JDK 5 "static imports" are also supported, as shown here:
sc.setDefaultImports( "java.util.Map", // Single type import "java.io.*", // Type-import-on-demand "static java.util.Collections.EMPTY_MAP", // Single static import "static java.util.Collections.*", // Static-import-on-demand );
-
getDefaultImports
java.lang.String[] getDefaultImports()
- Returns:
- The default imports that were previously configured with
setDefaultImports(String...)
-
setClassName
void setClassName(java.lang.String className)
Sets the name of the generated class. Defaults toDEFAULT_CLASS_NAME
. In most cases, there is no need to set this name, because the generated class is loaded into its ownClassLoader
where its name cannot collide with classes generated by other evaluators.One reason to use this function is to have a class name in a non-default package, which can be relevant when types and members with DEFAULT accessibility are accessed.
-
setExtendedClass
void setExtendedClass(@Nullable java.lang.Class<?> extendedClass)
Sets a particular superclass that the generated class will extend. If extendedClass isnull
, the generated class will extendObject
.The usual reason to set a base class for an evaluator is that the generated class can directly access the superclass's (non-private) members.
-
setExtendedType
@Deprecated void setExtendedType(@Nullable java.lang.Class<?> extendedClass)
Deprecated.UsesetExtendedClass(Class)
instead
-
setImplementedInterfaces
void setImplementedInterfaces(java.lang.Class<?>[] implementedInterfaces)
Sets a particular set of interfaces that the generated class will implement.
-
setImplementedTypes
@Deprecated void setImplementedTypes(java.lang.Class<?>[] implementedInterfaces)
Deprecated.UsesetImplementedInterfaces(Class[])
instead
-
getClazz
java.lang.Class<?> getClazz()
- Returns:
- The generated
Class
- Throws:
java.lang.IllegalStateException
- ThisIClassBodyEvaluator
is not yet cooked
-
createInstance
java.lang.Object createInstance(java.io.Reader reader) throws CompileException, java.io.IOException
Scans, parses and compiles a class body from the tokens delivered by the the givenReader
, then creates and returns an instance of that class.- Parameters:
reader
- Source of class body tokens- Returns:
- An object that extends the
extendedType
and implements the givenimplementedTypes
- Throws:
CompileException
java.io.IOException
-
-