Package groovy.lang

# Class GroovyCodeSource

java.lang.Object
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
  Description
  `GroovyCodeSource(File infile)`
  `GroovyCodeSource(File infile, String encoding)`
  Creates a code source from a Groovy source file.
  `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(String script, String name, String codeBase)`
  Creates a code source from script text with an explicit logical name and code base.
  `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.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `equals(Object o)`
  `CodeSource`
  `getCodeSource()`
  Returns the `CodeSource` associated with compiled script classes.
  `File`
  `getFile()`
  Returns the backing file when this code source was created from a file.
  `String`
  `getName()`
  Returns the logical name used for the script.
  `String`
  `getScriptText()`
  Returns the Groovy source text.
  `URL`
  `getURL()`
  Returns the backing URL when this code source was created from a URL.
  `int`
  `hashCode()`
  `boolean`
  `isCachable()`
  Indicates whether classes compiled from this source may be cached.
  `void`
  `setCachable(boolean b)`
  Controls whether classes compiled from this source may be cached by `GroovyClassLoader`.

  ### Methods inherited from class java.lang.Object

  `clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### GroovyCodeSource

    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`

  - ### GroovyCodeSource

    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.

  - ### GroovyCodeSource

    public GroovyCodeSource(File infile, String encoding) throws IOException
    Creates a code source from a Groovy source file.
    Parameters:
      `infile` - the source file
      `encoding` - the character encoding used to read the file, or `null` for the default handling
    Throws:
      `IOException` - if the file cannot be read

  - ### GroovyCodeSource

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

  - ### GroovyCodeSource

    public GroovyCodeSource(URI uri) throws IOException
    Creates a code source from the Groovy source available at the supplied URI.
    Parameters:
      `uri` - the source URI
    Throws:
      `IOException` - if the source cannot be read

  - ### GroovyCodeSource

    public GroovyCodeSource(URL url)
    Creates a code source from the Groovy source available at the supplied URL.
    Parameters:
      `url` - the source URL

- ## Method Details

  - ### getCodeSource

    public CodeSource getCodeSource()
    Returns the `CodeSource` associated with compiled script classes.
    Returns:
      the code source for this script

  - ### getScriptText

    public String getScriptText()
    Returns the Groovy source text.
    Returns:
      the script text

  - ### getName

    public String getName()
    Returns the logical name used for the script.
    Returns:
      the script name

  - ### getFile

    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

  - ### getURL

    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

  - ### setCachable

    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

  - ### isCachable

    public boolean isCachable()
    Indicates whether classes compiled from this source may be cached.
    Returns:
      `true` if the source is cacheable

  - ### equals

    public boolean equals(Object o)
    Overrides:
      `equals` in class `Object`

  - ### hashCode

    public int hashCode()
    Overrides:
      `hashCode` in class `Object`