Class ClassBodyEvaluator

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void cook​(java.lang.String fileName, java.io.Reader r)
      Reads, scans, parses and compiles Java tokens from the given Reader.
      protected void cook​(java.lang.String fileName, java.lang.String[] imports, java.io.Reader r)  
      java.lang.Object createInstance​(java.io.Reader reader)
      Scans, parses and compiles a class body from the tokens delivered by the the given Reader, then creates and returns an instance of that class.
      java.util.Map<java.lang.String,​byte[]> getBytecodes()  
      java.lang.Class<?> getClazz()  
      java.lang.String[] getDefaultImports()  
      protected java.io.Reader newFileName​(java.lang.String fileName, java.io.Reader reader)
      Sets the given file name, and the current line number to 1, and the current column number to 1, when the first char is read from the reader.
      protected static java.lang.String[] parseImportDeclarations​(java.io.Reader r)
      Heuristically parses IMPORT declarations at the beginning of the character stream produced by the given Reader.
      void setClassName​(java.lang.String className)
      Sets the name of the generated class.
      void setCompileErrorHandler​(ErrorHandler compileErrorHandler)
      Installs an ErrorHandler 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<?> extendedType)
      Sets a particular superclass that the generated class will extend.
      void setExtendedType​(java.lang.Class<?> extendedClass)
      Deprecated.
      void setImplementedInterfaces​(java.lang.Class<?>[] implementedTypes)
      Sets a particular set of interfaces that the generated class will implement.
      void setImplementedTypes​(java.lang.Class<?>[] implementedInterfaces)
      Deprecated.
      void setParentClassLoader​(java.lang.ClassLoader parentClassLoader)
      The "parent class loader" is used to load referenced classes.
      void setSourceVersion​(int version)
      Specifies the version of source code accepted, in analogy with JAVAC's -source command line option.
      void setTargetVersion​(int version)
      Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's -target command line option.
      void setWarningHandler​(WarningHandler warningHandler)
      By default, warnings are discarded, but an application my install a custom WarningHandler.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ClassBodyEvaluator

        public ClassBodyEvaluator()
    • Method Detail

      • setClassName

        public void setClassName​(java.lang.String className)
        Description copied from interface: IClassBodyEvaluator
        Sets the name of the generated class. Defaults to IClassBodyEvaluator.DEFAULT_CLASS_NAME. In most cases, there is no need to set this name, because the generated class is loaded into its own ClassLoader 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.

        Specified by:
        setClassName in interface IClassBodyEvaluator
      • setDefaultImports

        public void setDefaultImports​(java.lang.String... defaultImports)
        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:
        setDefaultImports in interface IClassBodyEvaluator
      • setExtendedClass

        public void setExtendedClass​(@Nullable
                                     java.lang.Class<?> extendedType)
        Description copied from interface: IClassBodyEvaluator
        Sets a particular superclass that the generated class will extend. If extendedClass is null, the generated class will extend Object.

        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:
        setExtendedClass in interface IClassBodyEvaluator
      • setImplementedInterfaces

        public void setImplementedInterfaces​(java.lang.Class<?>[] implementedTypes)
        Description copied from interface: IClassBodyEvaluator
        Sets a particular set of interfaces that the generated class will implement.
        Specified by:
        setImplementedInterfaces in interface IClassBodyEvaluator
      • setParentClassLoader

        public void setParentClassLoader​(@Nullable
                                         java.lang.ClassLoader parentClassLoader)
        Description copied from interface: IClassBodyEvaluator
        The "parent class loader" is used to load referenced classes. Useful values are:
        System.getSystemClassLoader() The running JVM's class path
        Thread.currentThread().getContextClassLoader() or null 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.

        Specified by:
        setParentClassLoader in interface IClassBodyEvaluator
      • setDebuggingInformation

        public void setDebuggingInformation​(boolean debugSource,
                                            boolean debugLines,
                                            boolean debugVars)
        Description copied from interface: IClassBodyEvaluator
        Determines what kind of debugging information is included in the generates classes. The default is typically "-g:none".
        Specified by:
        setDebuggingInformation in interface IClassBodyEvaluator
      • setSourceVersion

        public void setSourceVersion​(int version)
        Description copied from interface: ICookable
        Specifies the version of source code accepted, in analogy with JAVAC's -source command line option. May be ignored by an implementation (e.g. the janino implementation always accepts the language features as described on the home page). Allowed values, and the default value, depend on the implementation. -1 means to use a default version.
        Specified by:
        setSourceVersion in interface ICookable
      • setTargetVersion

        public void setTargetVersion​(int version)
        Description copied from interface: ICookable
        Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's -target command 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. -1 means to use a default version.
        Specified by:
        setTargetVersion in interface ICookable
      • cook

        public void cook​(@Nullable
                         java.lang.String fileName,
                         java.io.Reader r)
                  throws CompileException,
                         java.io.IOException
        Description copied from interface: ICookable
        Reads, scans, parses and compiles Java tokens from the given Reader.
        Specified by:
        cook in interface ICookable
        Specified by:
        cook in class Cookable
        Parameters:
        fileName - Used when reporting errors and warnings
        Throws:
        CompileException
        java.io.IOException
      • cook

        protected void cook​(@Nullable
                            java.lang.String fileName,
                            java.lang.String[] imports,
                            java.io.Reader r)
                     throws CompileException,
                            java.io.IOException
        Parameters:
        imports - E.g. "java.io.*" or "static java.util.Arrays.asList"
        r - The class body to cook, without leading IMPORT declarations
        Throws:
        CompileException
        java.io.IOException
      • getBytecodes

        public java.util.Map<java.lang.String,​byte[]> getBytecodes()
        Specified by:
        getBytecodes in interface ICookable
        Returns:
        The generated Java bytecode; maps class name to bytes
      • newFileName

        protected java.io.Reader newFileName​(@Nullable
                                             java.lang.String fileName,
                                             java.io.Reader reader)
        Sets the given file name, and the current line number to 1, and the current column number to 1, when the first char is read from the reader.
      • parseImportDeclarations

        protected static java.lang.String[] parseImportDeclarations​(java.io.Reader r)
                                                             throws java.io.IOException
        Heuristically parses IMPORT declarations at the beginning of the character stream produced by the given Reader. After this method returns, all characters up to and including that last IMPORT declaration have been read from the Reader.

        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 - A Reader that supports MARK, e.g. a BufferedReader
        Returns:
        The parsed imports, e.g. { "java.util.*", "static java.util.Map.Entry" }
        Throws:
        java.io.IOException
      • createInstance

        public java.lang.Object createInstance​(java.io.Reader reader)
                                        throws CompileException,
                                               java.io.IOException
        Description copied from interface: IClassBodyEvaluator
        Scans, parses and compiles a class body from the tokens delivered by the the given Reader, then creates and returns an instance of that class.
        Specified by:
        createInstance in interface IClassBodyEvaluator
        Parameters:
        reader - Source of class body tokens
        Returns:
        An object that extends the extendedType and implements the given implementedTypes
        Throws:
        CompileException
        java.io.IOException