Package org.codehaus.groovy.control.customizers

# Class SourceAwareCustomizer

java.lang.Object
org.codehaus.groovy.control.customizers.CompilationCustomizer
org.codehaus.groovy.control.customizers.DelegatingCustomizer
org.codehaus.groovy.control.customizers.SourceAwareCustomizer

All Implemented Interfaces:
  `CompilationUnitAware`, `CompilationUnit.IPrimaryClassNodeOperation`

public class SourceAwareCustomizer extends DelegatingCustomizer
A base class for customizers which only have to be applied on specific source units. This is for example useful if you want a customizer to be applied only for files matching some extensions.

For convenience, this class implements several methods that you may extend to customize the behaviour of this utility. For example, if you want to apply a customizer only for classes matching the '.foo' file extension, then you only have to override the `acceptExtension(String)` method:

    return "foo".equals(extension)

Since:
  2.1.0

- ## Field Summary

  ### Fields inherited from class org.codehaus.groovy.control.customizers.DelegatingCustomizer

  `delegate`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SourceAwareCustomizer(CompilationCustomizer delegate)`
  Creates a source-aware wrapper around another compilation customizer.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `accept(String fileName)`
  Checks whether a source file name is accepted by the base-name and extension validators.
  `boolean`
  `acceptBaseName(String baseName)`
  Checks whether a base file name is accepted.
  `boolean`
  `acceptClass(ClassNode cnode)`
  Checks whether a class node is accepted.
  `boolean`
  `acceptExtension(String extension)`
  Checks whether a file extension is accepted.
  `boolean`
  `acceptSource(SourceUnit unit)`
  Checks whether a source unit is accepted.
  `void`
  `call(SourceUnit source, GeneratorContext context, ClassNode classNode)`
  Invokes the delegate only when the source and class validators accept the current input.
  `void`
  `setBaseNameValidator(Closure<Boolean> baseNameValidator)`
  Sets the predicate used to validate source base names.
  `void`
  `setClassValidator(Closure<Boolean> classValidator)`
  Sets the predicate used to validate class nodes.
  `void`
  `setExtensionValidator(Closure<Boolean> extensionValidator)`
  Sets the predicate used to validate source file extensions.
  `void`
  `setSourceUnitValidator(Closure<Boolean> sourceUnitValidator)`
  Sets the predicate used to validate whole source units.

  ### Methods inherited from class org.codehaus.groovy.control.customizers.DelegatingCustomizer

  `setCompilationUnit`

  ### Methods inherited from class org.codehaus.groovy.control.customizers.CompilationCustomizer

  `getPhase`

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

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

  ### Methods inherited from interface org.codehaus.groovy.control.CompilationUnit.IPrimaryClassNodeOperation

  `doPhaseOperation, needSortedInput`

- ## Constructor Details

  - ### SourceAwareCustomizer

    public SourceAwareCustomizer(CompilationCustomizer delegate)
    Creates a source-aware wrapper around another compilation customizer.
    Parameters:
      `delegate` - the customizer to invoke when the source matches

- ## Method Details

  - ### call

    public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException
    Invokes the delegate only when the source and class validators accept the current input.

    A `null` source unit selects nothing: this customizer exists to match against source units, so a class node which has none (see `call`) cannot satisfy any source-based criterion, and the delegate is not invoked.

    Specified by:
      `call` in interface `CompilationUnit.IPrimaryClassNodeOperation`
    Overrides:
      `call` in class `DelegatingCustomizer`
    Parameters:
      `source` - the source unit being customized, possibly `null`
      `context` - the current generator context
      `classNode` - the class node being customized
    Throws:
      `CompilationFailedException` - if the delegate fails

  - ### setBaseNameValidator

    public void setBaseNameValidator(Closure<Boolean> baseNameValidator)
    Sets the predicate used to validate source base names.
    Parameters:
      `baseNameValidator` - the validator to use

  - ### setExtensionValidator

    public void setExtensionValidator(Closure<Boolean> extensionValidator)
    Sets the predicate used to validate source file extensions.
    Parameters:
      `extensionValidator` - the validator to use

  - ### setSourceUnitValidator

    public void setSourceUnitValidator(Closure<Boolean> sourceUnitValidator)
    Sets the predicate used to validate whole source units.
    Parameters:
      `sourceUnitValidator` - the validator to use

  - ### setClassValidator

    public void setClassValidator(Closure<Boolean> classValidator)
    Sets the predicate used to validate class nodes.
    Parameters:
      `classValidator` - the validator to use

  - ### accept

    public boolean accept(String fileName)
    Checks whether a source file name is accepted by the base-name and extension validators.
    Parameters:
      `fileName` - the file name to inspect
    Returns:
      `true` if the file name is accepted

  - ### acceptClass

    public boolean acceptClass(ClassNode cnode)
    Checks whether a class node is accepted.
    Parameters:
      `cnode` - the class node to inspect
    Returns:
      `true` if the class is accepted

  - ### acceptSource

    public boolean acceptSource(SourceUnit unit)
    Checks whether a source unit is accepted.
    Parameters:
      `unit` - the source unit to inspect
    Returns:
      `true` if the source is accepted

  - ### acceptExtension

    public boolean acceptExtension(String extension)
    Checks whether a file extension is accepted.
    Parameters:
      `extension` - the extension to inspect
    Returns:
      `true` if the extension is accepted

  - ### acceptBaseName

    public boolean acceptBaseName(String baseName)
    Checks whether a base file name is accepted.
    Parameters:
      `baseName` - the base file name to inspect
    Returns:
      `true` if the base name is accepted