Package groovy.lang

Class GroovyClassLoader

  • All Implemented Interfaces:
    Closeable, AutoCloseable
    Direct Known Subclasses:

    public class GroovyClassLoader
    extends URLClassLoader
    A ClassLoader which can load Groovy classes. The loaded classes are cached, classes from other classloaders should not be cached. To be able to load a script that was asked for earlier but was created later it is essential not to keep anything like a "class not found" information for that class name. This includes possible parent loaders. Classes that are not cached are always reloaded.
    • Field Detail

      • classCache

        protected final EvictableCache<String,​Class> classCache
        this cache contains the loaded classes or PARSING, if the class is currently parsed
      • sourceCache

        protected final StampedCommonCache<String,​Class> sourceCache
        This cache contains mappings of file name to class. It is used to bypass compilation.
    • Constructor Detail

      • GroovyClassLoader

        public GroovyClassLoader()
        creates a GroovyClassLoader using the current Thread's context Class loader as parent.
      • GroovyClassLoader

        public GroovyClassLoader​(ClassLoader loader)
        creates a GroovyClassLoader using the given ClassLoader as parent
      • GroovyClassLoader

        public GroovyClassLoader​(GroovyClassLoader parent)
        creates a GroovyClassLoader using the given GroovyClassLoader as parent. This loader will get the parent's CompilerConfiguration
      • GroovyClassLoader

        public GroovyClassLoader​(ClassLoader parent,
                                 CompilerConfiguration config,
                                 boolean useConfigurationClasspath)
        creates a GroovyClassLoader.
        parent - the parent class loader
        config - the compiler configuration
        useConfigurationClasspath - determines if the configurations classpath should be added
      • GroovyClassLoader

        public GroovyClassLoader​(ClassLoader loader,
                                 CompilerConfiguration config)
        creates a GroovyClassLoader using the given ClassLoader as parent.
    • Method Detail

      • defineClass

        public Class defineClass​(ClassNode classNode,
                                 String file,
                                 String newCodeBase)
        Loads the given class node returning the implementation Class.

        WARNING: this compilation is not synchronized

        classNode -
        a class
      • hasCompatibleConfiguration

        public boolean hasCompatibleConfiguration​(CompilerConfiguration config)
        Check if this class loader has compatible CompilerConfiguration with the provided one.
        config - the compiler configuration to test for compatibility
        true if the provided config is exactly the same instance of CompilerConfiguration as this loader has
      • parseClass

        public Class parseClass​(String text,
                                String fileName)
                         throws CompilationFailedException
        Parses the given text into a Java class capable of being run
        text - the text of the script/class to parse
        fileName - the file name to use as the name of the class
        the main class defined in the given script
      • generateScriptName

        public String generateScriptName()
      • parseClass

        public Class parseClass​(GroovyCodeSource codeSource,
                                boolean shouldCacheSource)
                         throws CompilationFailedException
        Parses the given code source into a Java class. If there is a class file for the given code source, then no parsing is done, instead the cached class is returned.
        shouldCacheSource - if true then the generated class will be stored in the source cache
        the main class defined in the given script
      • getClassPath

        protected String[] getClassPath()
        gets the currently used classpath.
        a String[] containing the file information of the urls
        See Also:
      • createCompilationUnit

        protected CompilationUnit createCompilationUnit​(CompilerConfiguration config,
                                                        CodeSource source)
        creates a new CompilationUnit. If you want to add additional phase operations to the CompilationUnit (for example to inject additional methods, variables, fields), then you should overwrite this method.
        config - the compiler configuration, usually the same as for this class loader
        source - the source containing the initial file to compile, more files may follow during compilation
        the CompilationUnit
      • defineClass

        public Class defineClass​(String name,
                                 byte[] b)
        open up the super class define that takes raw bytes
      • getClassCacheEntry

        protected Class getClassCacheEntry​(String name)
        gets a class from the class cache. This cache contains only classes loaded through this class loader or an InnerLoader instance. If no class is stored for a specific name, then the method should return null.
        name - of the class
        the class stored for the given name
        See Also:
        removeClassCacheEntry(String), setClassCacheEntry(Class), clearCache()
      • addURL

        public void addURL​(URL url)
        adds a URL to the classloader.
        addURL in class URLClassLoader
        url - the new classpath element
      • isRecompilable

        protected boolean isRecompilable​(Class cls)
        Indicates if a class is recompilable. Recompilable means, that the classloader will try to locate a groovy source file for this class and then compile it again, adding the resulting class as entry to the cache. Giving null as class is like a recompilation, so the method should always return true here. Only classes that are implementing GroovyObject are compilable and only if the timestamp in the class is lower than Long.MAX_VALUE.

        NOTE: First the parent loaders will be asked and only if they don't return a class the recompilation will happen. Recompilation also only happen if the source file is newer.

        cls - the class to be tested. If null the method should return true
        true if the class should be compiled again
        See Also:
        isSourceNewer(URL, Class)
      • setShouldRecompile

        public void setShouldRecompile​(Boolean mode)
        sets if the recompilation should be enable. There are 3 possible values for this. Any value different than null overrides the value from the compiler configuration. true means to recompile if needed false means to never recompile.
        mode - the recompilation mode
        See Also:
      • isShouldRecompile

        public Boolean isShouldRecompile()
        gets the currently set recompilation mode. null means, the compiler configuration is used. False means no recompilation and true means that recompilation will be done if needed.
        the recompilation mode
      • recompile

        protected Class recompile​(URL source,
                                  String className,
                                  Class oldClass)
                           throws CompilationFailedException,
        (Re)Compiles the given source. This method starts the compilation of a given source, if the source has changed since the class was created. For this isSourceNewer is called.
        source - the source pointer for the compilation
        className - the name of the class to be generated
        oldClass - a possible former class
        the old class if the source wasn't new enough, the new class else
        CompilationFailedException - if the compilation failed
        IOException - if the source is not readable
        See Also:
        isSourceNewer(URL, Class)
      • getTimeStamp

        protected long getTimeStamp​(Class cls)
        gets the time stamp of a given class. For groovy generated classes this usually means to return the value of the static field __timeStamp. If the parameter doesn't have such a field, then Long.MAX_VALUE is returned
        cls - the class
        the time stamp
      • isSourceNewer

        protected boolean isSourceNewer​(URL source,
                                        Class cls)
                                 throws IOException
        Decides if the given source is newer than a class.
        source - the source we may want to compile
        cls - the former class
        true if the source is newer, false else
        IOException - if it is not possible to open an connection for the given source
        See Also:
      • addClasspath

        public void addClasspath​(String path)
        adds a classpath to this classloader.
        path - is a jar file or a directory.
        See Also:
      • getLoadedClasses

        public Class[] getLoadedClasses()

        Returns all Groovy classes loaded by this class loader.

        all classes loaded by this class loader