Class ClassBodyEvaluator

    • Constructor Detail

      • ClassBodyEvaluator

        public ClassBodyEvaluator()
    • Method Detail

      • setClassName

        public void setClassName​(java.lang.String className)
        Description copied from interface: IClassBodyEvaluator
        Set 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[] optionalDefaultImports)
        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 in the following example.

        Example:

             sc.setDefaultImports(new String[] {
                 "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​(java.lang.Class optionalExtendedType)
        Description copied from interface: IClassBodyEvaluator
        Set a particular superclass that the generated class will extend. If null is passed, the generated class will extend Object.

        The common reason to set a base class for an evaluator is that the generated class can directly access the base superclass's (non-private) members.

        Specified by:
        setExtendedClass in interface IClassBodyEvaluator
      • setExtendedType

        @Deprecated
        public void setExtendedType​(java.lang.Class optionalExtendedClass)
        Deprecated.
        Specified by:
        setExtendedType in interface IClassBodyEvaluator
      • setImplementedInterfaces

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

        @Deprecated
        public void setImplementedTypes​(java.lang.Class[] implementedInterfaces)
        Deprecated.
        Specified by:
        setImplementedTypes in interface IClassBodyEvaluator
      • cook

        protected void cook​(java.lang.String optionalFileName,
                            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
      • parseImportDeclarations

        protected static java.lang.String[] parseImportDeclarations​(java.io.Reader r)
                                                             throws java.io.IOException
        Heuristically parse 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 optionalExtendedType and implements the given implementedTypes
        Throws:
        CompileException
        java.io.IOException