Class PropertyBuilder<B extends PropertyBuilder<B,T>,T>

java.lang.Object
net.sourceforge.pmd.properties.PropertyBuilder<B,T>
Type Parameters:
B - Concrete type of this builder instance
T - Type of values the property handles
Direct Known Subclasses:
PropertyBuilder.GenericCollectionPropertyBuilder, PropertyBuilder.GenericPropertyBuilder, PropertyBuilder.RegexPropertyBuilder

public abstract class PropertyBuilder<B extends PropertyBuilder<B,T>,T> extends Object
Base class for generic property builders. Property builders are obtained from the PropertyFactory, and are used to build PropertyDescriptors.

All properties must specify the following attributes to build properly:

The PropertyDescriptor may be built after those required steps by calling build().

A property builder may throw IllegalArgumentException at any stage during the build process to indicate invalid input. It usually tries to do so as early as possible, rather than waiting for the call to build().

Author:
Clément Fournier
Since:
6.10.0
  • Field Details

    • typeId

      protected @Nullable net.sourceforge.pmd.properties.internal.PropertyTypeId typeId
      Non-null if declared in XML.
    • isXPathAvailable

      protected boolean isXPathAvailable
  • Method Details

    • desc

      public B desc(String desc)
      Specify the description of the property. This is used for documentation. Please describe precisely how the property may change the behaviour of the rule. Providing complete information should be preferred over being concise.

      Calling this method is required for build() to succeed.

      Parameters:
      desc - The description
      Returns:
      The same builder
      Throws:
      IllegalArgumentException - If the description is null or whitespace
    • availableInXPath

      public B availableInXPath(boolean b)
      If true, the property will be made available to XPath queries as an XPath variable. The default is false (except for properties of XPath rules that were defined in XML).
      Parameters:
      b - Whether to enable or not
      Returns:
      This builder
    • require

      public abstract B require(PropertyConstraint<? super T> constraint)
      Add a constraint on the values that this property may take. The validity of values will be checked when parsing the XML, and invalid values will be reported. A rule will never be run if some of its properties violate some constraints.

      Constraints should be independent from each other, and should perform no side effects. PMD doesn't specify how many times a constraint predicate will be executed, or in what order.

      Parameters:
      constraint - The constraint
      Returns:
      The same builder
      See Also:
    • defaultValue

      public B defaultValue(@NonNull T val)
      Specify a default value. Some subclasses provide convenient related methods, see e.g. PropertyBuilder.GenericCollectionPropertyBuilder.defaultValues(Object, Object[]). Using the null value is prohibited.

      Calling this method is required for build() to succeed.

      Parameters:
      val - Default value
      Returns:
      The same builder
      Throws:
      IllegalArgumentException - If the argument is null
    • build

      public abstract PropertyDescriptor<T> build()
      Builds the descriptor and returns it.
      Returns:
      The built descriptor
      Throws:
      IllegalArgumentException - if the description or default value were not provided
      IllegalArgumentException - if the default value does not satisfy the given constraints
    • getName

      public String getName()
      Returns the name of the property to be built.