Package: groovy.lang

## [Java] Class GroovyCodeSource

- groovy.lang.GroovyCodeSource

-     public class GroovyCodeSource
      extends Object

  CodeSource wrapper class that allows specific security policies to be associated with a class compiled from groovy source.

- - ### Constructor Summary

    Constructors
    Constructor and description
    `GroovyCodeSource(String script, String name, String codeBase)` Creates a code source from script text with an explicit logical name and code base.
    `GroovyCodeSource(Reader reader, String name, String codeBase)` Construct a GroovyCodeSource for an inputStream of groovyCode that has an unknown provenance -- meaning it didn't come from a File or a URL (e.g. a String).
    `GroovyCodeSource(File infile, String encoding)` Creates a code source from a Groovy source file.
    `GroovyCodeSource(File infile)` Parameters: `infile` - the file to create a GroovyCodeSource for.
    `GroovyCodeSource(URI uri)` Creates a code source from the Groovy source available at the supplied URI.
    `GroovyCodeSource(URL url)` Creates a code source from the Groovy source available at the supplied URL.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public boolean` | `equals(Object o)` {@inheritDoc}
    `` | `public CodeSource` | `getCodeSource()` Returns the CodeSource associated with compiled script classes.
    `` | `public File` | `getFile()` Returns the backing file when this code source was created from a file.
    `` | `public String` | `getName()` Returns the logical name used for the script.
    `` | `public String` | `getScriptText()` Returns the Groovy source text.
    `` | `public URL` | `getURL()` Returns the backing URL when this code source was created from a URL.
    `` | `public int` | `hashCode()` {@inheritDoc}
    `` | `public boolean` | `isCachable()` Indicates whether classes compiled from this source may be cached.
    `` | `public void` | `setCachable(boolean b)` Controls whether classes compiled from this source may be cached by GroovyClassLoader.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### public GroovyCodeSource(String script, String name, String codeBase)

      Creates a code source from script text with an explicit logical name and code base.

      Parameters:
        `script` - the Groovy source text
        `name` - the logical script name
        `codeBase` - the code base used to create the backing CodeSource

    - #### public GroovyCodeSource(Reader reader, String name, String codeBase)

      Construct a GroovyCodeSource for an inputStream of groovyCode that has an unknown provenance -- meaning it didn't come from a File or a URL (e.g. a String). The supplied codeBase will be used to construct a File URL that should match up with a java Policy entry that determines the grants to be associated with the class that will be built from the InputStream.

      The permission groovy.security.GroovyCodeSourcePermission will be used to determine if the given codeBase may be specified. That is, the current Policy set must have a GroovyCodeSourcePermission that implies the codeBase, or an exception will be thrown. This is to prevent callers from hijacking existing codeBase policy entries unless explicitly authorized by the user.

    - #### public GroovyCodeSource(File infile, String encoding)

      Creates a code source from a Groovy source file.

      throws:
        IOException if the file cannot be read
      Parameters:
        `infile` - the source file
        `encoding` - the character encoding used to read the file, or `null` for the default handling

    - #### public GroovyCodeSource(File infile)

      throws:
        IOException if an issue arises opening and reading the file.
      Parameters:
        `infile` - the file to create a GroovyCodeSource for.

    - #### public GroovyCodeSource(URI uri)

      Creates a code source from the Groovy source available at the supplied URI.

      throws:
        IOException if the source cannot be read
      Parameters:
        `uri` - the source URI

    - #### public GroovyCodeSource(URL url)

      Creates a code source from the Groovy source available at the supplied URL.

      Parameters:
        `url` - the source URL

  - ### Method Detail

    - #### public boolean equals(Object o)

      {@inheritDoc}

    - #### public CodeSource getCodeSource()

      Returns the CodeSource associated with compiled script classes.

      Returns:
        the code source for this script

    - #### public File getFile()

      Returns the backing file when this code source was created from a file.

      Returns:
        the backing file, or `null` if this source does not originate from a file

    - #### public String getName()

      Returns the logical name used for the script.

      Returns:
        the script name

    - #### public String getScriptText()

      Returns the Groovy source text.

      Returns:
        the script text

    - #### public URL getURL()

      Returns the backing URL when this code source was created from a URL.

      Returns:
        the backing URL, or `null` if this source does not originate from a URL

    - #### public int hashCode()

      {@inheritDoc}

    - #### public boolean isCachable()

      Indicates whether classes compiled from this source may be cached.

      Returns:
        `true` if the source is cacheable

    - #### public void setCachable(boolean b)

      Controls whether classes compiled from this source may be cached by GroovyClassLoader.

      Parameters:
        `b` - `true` to allow caching, `false` otherwise