Class ClassBodyEvaluator
- All Implemented Interfaces:
IClassBodyEvaluator, ICookable
ClassBodyEvaluator object, proceed as described for IClassBodyEvaluator. Alternatively,
a number of "convenience constructors" exist that execute the described steps instantly.
Notice that this implementation of IClassBodyEvaluator is prone to "Java injection", i.e. an
application could get more than one class body compiled by passing a bogus input document.
Also notice that the parsing of leading IMPORT declarations is heuristic and has certain limitations; see
parseImportDeclarations(Reader).
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Stringprivate String[]private Class<?> private Class<?>[]private static final Patternprivate Class<?> private final SimpleCompilerFields inherited from interface IClassBodyEvaluator
DEFAULT_CLASS_NAME -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidReads, scans, parses and compiles Java tokens from the givenReader.protected voidcreateInstance(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.Class<?> getClazz()String[]protected ReadernewFileName(String fileName, Reader reader) Sets the given file name, and the current line number to 1, and the current column number to 1, when the firstcharis read from the reader.protected static String[]Heuristically parses IMPORT declarations at the beginning of the character stream produced by the givenReader.voidsetClassName(String className) Sets the name of the generated class.voidsetCompileErrorHandler(ErrorHandler compileErrorHandler) Installs anErrorHandlerwhich is invoked during compilation on each error.voidsetDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars) Determines what kind of debugging information is included in the generates classes.voidsetDefaultImports(String... defaultImports) "Default imports" add to the system import "java.lang", i.e.voidsetExtendedClass(Class<?> extendedType) Sets a particular superclass that the generated class will extend.voidsetExtendedType(Class<?> extendedClass) Deprecated.voidsetImplementedInterfaces(Class<?>[] implementedTypes) Sets a particular set of interfaces that the generated class will implement.voidsetImplementedTypes(Class<?>[] implementedInterfaces) Deprecated.UsesetImplementedInterfaces(Class[])insteadvoidsetParentClassLoader(ClassLoader parentClassLoader) The "parent class loader" is used to load referenced classes.voidsetSourceVersion(int version) Specifies the version of source code accepted, in analogy with JAVAC's-sourcecommand line option.voidsetTargetVersion(int version) Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-targetcommand line option.voidsetWarningHandler(WarningHandler warningHandler) By default, warnings are discarded, but an application my install a customWarningHandler.Methods inherited from class Cookable
cook, cook, cook, cook, cook, cook, cook, cookFile, cookFile, cookFile, cookFile
-
Field Details
-
sc
-
defaultImports
-
className
-
extendedType
-
implementedTypes
-
result
-
IMPORT_STATEMENT_PATTERN
-
-
Constructor Details
-
ClassBodyEvaluator
public ClassBodyEvaluator()
-
-
Method Details
-
setClassName
Description copied from interface:IClassBodyEvaluatorSets the name of the generated class. Defaults toIClassBodyEvaluator.DEFAULT_CLASS_NAME. In most cases, there is no need to set this name, because the generated class is loaded into its ownClassLoaderwhere 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.
- Specified by:
setClassNamein interfaceIClassBodyEvaluator
-
setDefaultImports
Description copied from interface:IClassBodyEvaluator"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 );- Specified by:
setDefaultImportsin interfaceIClassBodyEvaluator
-
getDefaultImports
- Specified by:
getDefaultImportsin interfaceIClassBodyEvaluator- Returns:
- The default imports that were previously configured with
IClassBodyEvaluator.setDefaultImports(String...)
-
setExtendedClass
Description copied from interface:IClassBodyEvaluatorSets 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.
- Specified by:
setExtendedClassin interfaceIClassBodyEvaluator
-
setExtendedType
Deprecated.UsesetExtendedClass(Class)instead- Specified by:
setExtendedTypein interfaceIClassBodyEvaluator
-
setImplementedInterfaces
Description copied from interface:IClassBodyEvaluatorSets a particular set of interfaces that the generated class will implement.- Specified by:
setImplementedInterfacesin interfaceIClassBodyEvaluator
-
setImplementedTypes
Deprecated.UsesetImplementedInterfaces(Class[])instead- Specified by:
setImplementedTypesin interfaceIClassBodyEvaluator
-
setParentClassLoader
Description copied from interface:IClassBodyEvaluatorThe "parent class loader" is used to load referenced classes. Useful values are:System.getSystemClassLoader()The running JVM's class path Thread.currentThread().getContextClassLoader()ornullThe class loader effective for the invoking thread ClassLoaders.BOOTCLASSPATH_CLASS_LOADERThe running JVM's boot class path The parent class loader defaults to the current thread's context class loader.
- Specified by:
setParentClassLoaderin interfaceIClassBodyEvaluator
-
setDebuggingInformation
public void setDebuggingInformation(boolean debugSource, boolean debugLines, boolean debugVars) Description copied from interface:IClassBodyEvaluatorDetermines what kind of debugging information is included in the generates classes. The default is typically "-g:none".- Specified by:
setDebuggingInformationin interfaceIClassBodyEvaluator
-
setSourceVersion
public void setSourceVersion(int version) Description copied from interface:ICookableSpecifies the version of source code accepted, in analogy with JAVAC's-sourcecommand line option. May be ignored by an implementation (e.g. thejaninoimplementation always accepts the language features as described on the home page). Allowed values, and the default value, depend on the implementation.-1means to use a default version.- Specified by:
setSourceVersionin interfaceICookable
-
setTargetVersion
public void setTargetVersion(int version) Description copied from interface:ICookableGenerates class files that target a specified release of the virtual machine, in analogy with JAVAC's-targetcommand line option. Allowed values depend on the implementation. The default value also depends on the implementation. The only invariant is that the generated class files are suitable for the currently executing JVM.-1means to use a default version.- Specified by:
setTargetVersionin interfaceICookable
-
setCompileErrorHandler
Description copied from interface:IClassBodyEvaluatorInstalls anErrorHandlerwhich is invoked during compilation on each error. (By default, the compilation throws aCompileExceptionon the first error and terminates.)If the given
ErrorHandlerthrows aCompileException, then the compilation terminates and the exception is propagated.If the given
ErrorHandlerdoes not throw aCompileExceptionbut 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 aCompileExceptionindicating the number of errors.In other words: The
ErrorHandlermay throw aCompileExceptionor not, but the compilation will definitely throw aCompileExceptionif one or more compile errors have occurred.- Specified by:
setCompileErrorHandlerin interfaceIClassBodyEvaluator- Parameters:
compileErrorHandler-nullto restore the default behavior (throwing aCompileException)
-
setWarningHandler
Description copied from interface:IClassBodyEvaluatorBy default, warnings are discarded, but an application my install a customWarningHandler.- Specified by:
setWarningHandlerin interfaceIClassBodyEvaluator- Parameters:
warningHandler-nullto indicate that no warnings be issued
-
cook
Description copied from interface:ICookableReads, scans, parses and compiles Java tokens from the givenReader.- Specified by:
cookin interfaceICookable- Specified by:
cookin classCookable- Parameters:
fileName- Used when reporting errors and warnings- Throws:
CompileExceptionIOException
-
cook
protected void cook(@Nullable String fileName, String[] imports, Reader r) throws CompileException, IOException - Parameters:
imports- E.g. "java.io.*" or "static java.util.Arrays.asList"r- The class body to cook, without leading IMPORT declarations- Throws:
CompileExceptionIOException
-
getClazz
- Specified by:
getClazzin interfaceIClassBodyEvaluator- Returns:
- The
Classcreated by the preceding call toCookable.cook(Reader)
-
getBytecodes
- Specified by:
getBytecodesin interfaceICookable- Returns:
- The generated Java bytecode; maps class name to bytes
-
newFileName
-
parseImportDeclarations
Heuristically parses IMPORT declarations at the beginning of the character stream produced by the givenReader. After this method returns, all characters up to and including that last IMPORT declaration have been read from theReader.This method does not handle comments and string literals correctly, i.e. if a pattern that looks like an IMPORT declaration appears within a comment or a string literal, it will be taken as an IMPORT declaration.
- Parameters:
r- AReaderthat supports MARK, e.g. aBufferedReader- Returns:
- The parsed imports, e.g.
{ "java.util.*", "static java.util.Map.Entry" } - Throws:
IOException
-
createInstance
Description copied from interface:IClassBodyEvaluatorScans, parses and compiles a class body from the tokens delivered by the the givenReader, then creates and returns an instance of that class.- Specified by:
createInstancein interfaceIClassBodyEvaluator- Parameters:
reader- Source of class body tokens- Returns:
- An object that extends the
extendedTypeand implements the givenimplementedTypes - Throws:
CompileExceptionIOException
-
setExtendedClass(Class)instead