Class CompilationUnit

  • Direct Known Subclasses:
    JavaAwareCompilationUnit, JavaStubCompilationUnit

    public class CompilationUnit
    extends ProcessingUnit
    The CompilationUnit collects all compilation data as it is generated by the compiler system. You can use this object to add additional source units to the compilation, or force the compilation to be run again (to affect only the deltas).

    You can also add PhaseOperations to this compilation using the addPhaseOperation method. This is commonly used when you want to wire a new AST Transformation into the compilation.

    • Field Detail

      • summariesBySourceName

        @Deprecated
        protected java.util.Map summariesBySourceName
        Deprecated.
      • summariesByPublicClassName

        @Deprecated
        protected java.util.Map summariesByPublicClassName
        Deprecated.
      • classSourcesByPublicClassName

        @Deprecated
        protected java.util.Map classSourcesByPublicClassName
        Deprecated.
      • sources

        protected java.util.Map<java.lang.String,​SourceUnit> sources
      • names

        protected java.util.List<java.lang.String> names
      • queuedSources

        protected java.util.LinkedList<SourceUnit> queuedSources
      • generatedClasses

        protected java.util.List<GroovyClass> generatedClasses
      • debug

        protected boolean debug
      • configured

        protected boolean configured
    • Constructor Detail

      • CompilationUnit

        public CompilationUnit()
        Initializes the CompilationUnit with defaults.
      • CompilationUnit

        public CompilationUnit​(GroovyClassLoader loader)
        Initializes the CompilationUnit with defaults except for class loader.
      • CompilationUnit

        public CompilationUnit​(CompilerConfiguration configuration)
        Initializes the CompilationUnit with no security considerations.
      • CompilationUnit

        public CompilationUnit​(CompilerConfiguration configuration,
                               java.security.CodeSource security,
                               GroovyClassLoader loader)
        Initializes the CompilationUnit with a CodeSource for controlling security stuff and a class loader for loading classes.
      • CompilationUnit

        public CompilationUnit​(CompilerConfiguration configuration,
                               java.security.CodeSource security,
                               GroovyClassLoader loader,
                               GroovyClassLoader transformLoader)
        Initializes the CompilationUnit with a CodeSource for controlling security stuff, a class loader for loading classes, and a class loader for loading AST transformations. Note The transform loader must be able to load compiler classes. That means CompilationUnit.class.classLoader must be at last a parent to transformLoader. The other loader has no such constraint.
        Parameters:
        transformLoader - - the loader for transforms
        loader - - loader used to resolve classes against during compilation
        security - - security setting for the compilation
        configuration - - compilation configuration
    • Method Detail

      • getTransformLoader

        public GroovyClassLoader getTransformLoader()
        Returns the class loader for loading AST transformations.
      • configure

        public void configure​(CompilerConfiguration configuration)
        Configures its debugging mode and classloader classpath from a given compiler configuration. This cannot be done more than once due to limitations in URLClassLoader.
        Overrides:
        configure in class ProcessingUnit
      • getAST

        public CompileUnit getAST()
        Returns the CompileUnit that roots our AST.
      • getSummariesBySourceName

        @Deprecated
        public java.util.Map getSummariesBySourceName()
        Deprecated.
      • getSummariesByPublicClassName

        @Deprecated
        public java.util.Map getSummariesByPublicClassName()
        Deprecated.
      • getClassSourcesByPublicClassName

        @Deprecated
        public java.util.Map getClassSourcesByPublicClassName()
        Deprecated.
      • isPublicClass

        @Deprecated
        public boolean isPublicClass​(java.lang.String className)
        Deprecated.
      • getClasses

        public java.util.List getClasses()
        Get the GroovyClasses generated by compile().
      • getFirstClassNode

        public ClassNode getFirstClassNode()
        Convenience routine to get the first ClassNode, for when you are sure there is only one.
      • getClassNode

        public ClassNode getClassNode​(java.lang.String name)
        Convenience routine to get the named ClassNode.
      • getASTTransformationsContext

        public ASTTransformationsContext getASTTransformationsContext()
        Returns:
        the AST transformations current context
      • addSources

        public void addSources​(java.lang.String[] paths)
        Adds a set of file paths to the unit.
      • addSources

        public void addSources​(java.io.File[] files)
        Adds a set of source files to the unit.
      • addSource

        public SourceUnit addSource​(java.io.File file)
        Adds a source file to the unit.
      • addSource

        public SourceUnit addSource​(java.net.URL url)
        Adds a source file to the unit.
      • addSource

        public SourceUnit addSource​(java.lang.String name,
                                    java.io.InputStream stream)
        Adds a InputStream source to the unit.
      • addSource

        public SourceUnit addSource​(java.lang.String name,
                                    java.lang.String scriptText)
      • iterator

        public java.util.Iterator<SourceUnit> iterator()
        Returns an iterator on the unit's SourceUnits.
      • addClassNode

        public void addClassNode​(ClassNode node)
        Adds a ClassNode directly to the unit (ie. without source). WARNING: the source is needed for error reporting, using this method without setting a SourceUnit will cause NullPinterExceptions
      • setClassgenCallback

        public void setClassgenCallback​(CompilationUnit.ClassgenCallback visitor)
        Sets a ClassgenCallback. You can have only one, and setting it to null removes any existing setting.
      • setProgressCallback

        public void setProgressCallback​(CompilationUnit.ProgressCallback callback)
        Sets a ProgressCallback. You can have only one, and setting it to null removes any existing setting.
      • dequeued

        protected boolean dequeued()
                            throws CompilationFailedException
        Dequeues any source units add through addSource and resets the compiler phase to initialization.

        Note: this does not mean a file is recompiled. If a SourceUnit has already passed a phase it is skipped until a higher phase is reached.

        Returns:
        true if there was a queued source
        Throws:
        CompilationFailedException
      • createClassVisitor

        protected org.objectweb.asm.ClassVisitor createClassVisitor()
      • setClassNodeResolver

        public void setClassNodeResolver​(ClassNodeResolver classNodeResolver)