Class 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
  • 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.
      Specified by:
      call in interface CompilationUnit.IPrimaryClassNodeOperation
      Overrides:
      call in class DelegatingCustomizer
      Parameters:
      source - the source unit being customized
      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