Class SimpleCompiler

    • Field Detail

      • LOGGER

        private static final java.util.logging.Logger LOGGER
      • parentClassLoader

        private java.lang.ClassLoader parentClassLoader
      • debugSource

        private boolean debugSource
      • debugLines

        private boolean debugLines
      • debugVars

        private boolean debugVars
      • sourceVersion

        private int sourceVersion
      • targetVersion

        private int targetVersion
      • classFiles

        @Nullable
        private ClassFile[] classFiles
        Null before cooking, non-null after cooking.
      • getBytecodesCache

        @Nullable
        private java.util.Map<java.lang.String,​byte[]> getBytecodesCache
      • getClassLoaderCache

        @Nullable
        private java.lang.ClassLoader getClassLoaderCache
    • Method Detail

      • main

        public static void main​(java.lang.String[] args)
                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • setParentClassLoader

        public void setParentClassLoader​(@Nullable
                                         java.lang.ClassLoader parentClassLoader)
        Description copied from interface: ISimpleCompiler
        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 ISimpleCompiler
      • setDebuggingInformation

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

        public final void cook​(@Nullable
                               java.lang.String fileName,
                               java.io.Reader r)
                        throws CompileException,
                               java.io.IOException
        Scans, parses and compiles a given compilation unit from the given Reader. After completion, getClassLoader() returns a ClassLoader that allows for access to the compiled classes.
        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

        public void cook​(Scanner scanner)
                  throws CompileException,
                         java.io.IOException
        Scans, parses and compiles a given compilation unit from the given scanner. After completion, getClassLoader() returns a ClassLoader that allows for access to the compiled classes.
        Throws:
        CompileException
        java.io.IOException
      • getClassFiles

        public ClassFile[] getClassFiles()
        Returns:
        The ClassFiles that were generated during cooking
      • setSourceVersion

        public void setSourceVersion​(int version)
        JANINO ignores any set "source version", and throws "NYI" CompileException when a language element is used which is not supported.
        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
      • 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
      • getBytecodes2

        private java.util.Map<java.lang.String,​byte[]> getBytecodes2()
      • getClassLoader

        public java.lang.ClassLoader getClassLoader()
        Description copied from interface: ISimpleCompiler
        Returns a ClassLoader object through which the previously compiled classes can be accessed. This ClassLoader can be used for subsequent ISimpleCompilers in order to compile compilation units that use types (e.g. declare derived types) declared in the previous one.

        This method must only be called after exactly one of the ICookable.cook(String, java.io.Reader) methods was called.

        Specified by:
        getClassLoader in interface ISimpleCompiler
      • getClassLoader2

        private java.lang.ClassLoader getClassLoader2()
      • equals

        public boolean equals​(@Nullable
                              java.lang.Object o)
        Two SimpleCompilers are regarded equal iff
        • Both are objects of the same class (e.g. both are ScriptEvaluators)
        • Both generated functionally equal classes as seen by Object.equals(Object)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • options

        public java.util.EnumSet<JaninoOption> options()
        Returns:
        A reference to the currently effective compilation options; changes to it take effect immediately
      • options

        public SimpleCompiler options​(java.util.EnumSet<JaninoOption> options)
        Sets the options for all future compilations.
      • classToType

        protected Java.Type classToType​(Location location,
                                        java.lang.Class<?> clazz)
        Wraps a reflection Class in a Java.Type object.
      • classesToTypes

        protected Java.Type[] classesToTypes​(Location location,
                                             @Nullable
                                             java.lang.Class<?>[] classes)
        Converts an array of Classes into an array ofJava.Types.
      • compileToClassLoader

        protected final java.lang.ClassLoader compileToClassLoader​(Java.AbstractCompilationUnit abstractCompilationUnit)
                                                            throws CompileException
        Compiles the given compilation unit. (A "compilation unit" is typically the contents of a Java source file.)
        Parameters:
        abstractCompilationUnit - The parsed compilation unit
        Returns:
        The ClassLoader into which the compiled classes were defined
        Throws:
        CompileException
      • assertUncooked

        private void assertUncooked()
        Throws:
        java.lang.IllegalStateException - This SimpleCompiler is already cooked