Package net.sourceforge.pmd.properties
Class PropertyFactory
- java.lang.Object
-
- net.sourceforge.pmd.properties.PropertyFactory
-
public final class PropertyFactory extends Object
Provides factory methods for common property types. Note: from 7.0.0 on, this will be the only way to build property descriptors. Concrete property classes and their constructors/ builders will be gradually deprecated before 7.0.0.Usage
Properties are a way to make your rule configurable by letting a user fill in some config value in their ruleset XML. As a rule developer, to declare a property on your rule, you must:- Build a
PropertyDescriptor
using one of the factory methods of this class, and providing the builder with the required info. - Define the property on your rule using
PropertySource.definePropertyDescriptor(PropertyDescriptor)
. This should be done in your rule's constructor.
PropertySource.getProperty(PropertyDescriptor)
.Example
class MyRule { // The property descriptor may be static, it can be shared across threads. private static final
PropertyDescriptor
<Integer> myIntProperty = PropertyFactory.intProperty("myIntProperty") .desc("This is my property") .defaultValue(3) .require(inRange(0, 100)) // constraints are checked before the rule is run .build(); // .. public MyRule() { definePropertyDescriptor(myIntProperty); } // ... somewhere in the rule int myPropertyValue = getProperty(myIntProperty); // use it. }- Author:
- Clément Fournier
- Since:
- 6.10.0
- Build a
-
-
Field Summary
Fields Modifier and Type Field Description static char
DEFAULT_DELIMITER
Default delimiter for all properties.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static PropertyBuilder.GenericPropertyBuilder<Boolean>
booleanProperty(String name)
Returns a builder for a boolean property.static PropertyBuilder.GenericCollectionPropertyBuilder<Character,List<Character>>
charListProperty(String name)
Returns a builder for a property having as value a list of characters.static PropertyBuilder.GenericPropertyBuilder<Character>
charProperty(String name)
Returns a builder for a character property.static PropertyBuilder.GenericCollectionPropertyBuilder<Double,List<Double>>
doubleListProperty(String name)
Returns a builder for a property having as value a list of decimal numbers.static PropertyBuilder.GenericPropertyBuilder<Double>
doubleProperty(String name)
Returns a builder for a double property.static <T extends Enum<T>>
PropertyBuilder.GenericCollectionPropertyBuilder<T,List<T>>enumListProperty(String name, Class<T> enumClass, Function<? super T,String> labelMaker)
Returns a builder for a property having as value a list of<T>
.static <T> PropertyBuilder.GenericCollectionPropertyBuilder<T,List<T>>
enumListProperty(String name, Map<String,T> nameToValue)
Returns a builder for a property having as value a list of<T>
.static <T extends Enum<T>>
PropertyBuilder.GenericPropertyBuilder<T>enumProperty(String name, Class<T> enumClass)
Returns a builder for an enumerated property for the given enum class, using thetoString
of its enum constants as labels.static <T extends Enum<T>>
PropertyBuilder.GenericPropertyBuilder<T>enumProperty(String name, Class<T> enumClass, Function<? super T,@NonNull String> labelMaker)
Returns a builder for an enumerated property for the given enum class, using the given function to label its constants.static <T> PropertyBuilder.GenericPropertyBuilder<T>
enumProperty(String name, Map<String,T> nameToValue)
Returns a builder for an enumerated property.static PropertyBuilder.GenericCollectionPropertyBuilder<Integer,List<Integer>>
intListProperty(String name)
Returns a builder for a property having as value a list of integers.static PropertyBuilder.GenericPropertyBuilder<Integer>
intProperty(String name)
Returns a builder for an integer property.static PropertyBuilder.GenericCollectionPropertyBuilder<Long,List<Long>>
longIntListProperty(String name)
Returns a builder for a property having as value a list of long integers.static PropertyBuilder.GenericPropertyBuilder<Long>
longIntProperty(String name)
Returns a builder for a long integer property.static PropertyBuilder.RegexPropertyBuilder
regexProperty(String name)
Returns a builder for a regex property.static PropertyBuilder.GenericCollectionPropertyBuilder<String,List<String>>
stringListProperty(String name)
Returns a builder for a property having as value a list of strings.static PropertyBuilder.GenericPropertyBuilder<String>
stringProperty(String name)
Returns a builder for a string property.
-
-
-
Field Detail
-
DEFAULT_DELIMITER
public static final char DEFAULT_DELIMITER
Default delimiter for all properties. Note that in PMD 6 this was the pipe character|
, while now it is 44. In PMD 6, numeric properties had a different delimiter, whereas in PMD 7, all properties have the same delimiter.- See Also:
- Constant Field Values
-
-
Method Detail
-
intProperty
public static PropertyBuilder.GenericPropertyBuilder<Integer> intProperty(String name)
Returns a builder for an integer property. The property descriptor will by default accept any value conforming to the format specified byInteger.parseInt(String)
, e.g.1234
or-123
.Note that that parser only supports decimal representations.
Acceptable values may be further refined by adding constraints. The class
NumericConstraints
provides some useful ready-made constraints for that purpose.- Parameters:
name
- Name of the property to build- Returns:
- A new builder
- See Also:
NumericConstraints
-
intListProperty
public static PropertyBuilder.GenericCollectionPropertyBuilder<Integer,List<Integer>> intListProperty(String name)
Returns a builder for a property having as value a list of integers. The format of the individual items is the same as for intProperty.- Parameters:
name
- Name of the property to build- Returns:
- A new builder
-
longIntProperty
public static PropertyBuilder.GenericPropertyBuilder<Long> longIntProperty(String name)
Returns a builder for a long integer property. The property descriptor will by default accept any value conforming to the format specified byLong.parseLong(String)
, e.g.1234455678854
.Note that that parser only supports decimal representations, and that neither the character L nor l is permitted to appear at the end of the string as a type indicator, as would be permitted in Java source.
Acceptable values may be further refined by adding constraints. The class
NumericConstraints
provides some useful ready-made constraints for that purpose.- Parameters:
name
- Name of the property to build- Returns:
- A new builder
- See Also:
NumericConstraints
-
longIntListProperty
public static PropertyBuilder.GenericCollectionPropertyBuilder<Long,List<Long>> longIntListProperty(String name)
Returns a builder for a property having as value a list of long integers. The format of the individual items is the same as for longIntProperty(String) longIntProperty}.- Parameters:
name
- Name of the property to build- Returns:
- A new builder
-
doubleProperty
public static PropertyBuilder.GenericPropertyBuilder<Double> doubleProperty(String name)
Returns a builder for a double property. The property descriptor will by default accept any value conforming to the format specified byDouble.valueOf(String)
, e.g.0
,.93
, or1e-1
. Acceptable values may be further refined by adding constraints. The classNumericConstraints
provides some useful ready-made constraints for that purpose.- Parameters:
name
- Name of the property to build- Returns:
- A new builder
- See Also:
NumericConstraints
-
doubleListProperty
public static PropertyBuilder.GenericCollectionPropertyBuilder<Double,List<Double>> doubleListProperty(String name)
Returns a builder for a property having as value a list of decimal numbers. The format of the individual items is the same as for doubleProperty.- Parameters:
name
- Name of the property to build- Returns:
- A new builder
-
regexProperty
public static PropertyBuilder.RegexPropertyBuilder regexProperty(String name)
Returns a builder for a regex property. The value type of such a property isPattern
. For this use case, this type of property should be preferred over stringProperty as pattern compilation, including syntax errors, are handled transparently to the rule.- Parameters:
name
- Name of the property to build- Returns:
- A new builder
-
stringProperty
public static PropertyBuilder.GenericPropertyBuilder<String> stringProperty(String name)
Returns a builder for a string property. The property descriptor will accept any string, and performs no expansion of escape sequences (e.g.\n
in the XML will be represented as the character sequence '\' 'n' and not the line-feed character '\n'). The value of a string attribute is trimmed. This behaviour could be changed with PMD 7.0.0.- Parameters:
name
- Name of the property to build- Returns:
- A new builder
-
stringListProperty
public static PropertyBuilder.GenericCollectionPropertyBuilder<String,List<String>> stringListProperty(String name)
Returns a builder for a property having as value a list of strings. The format of the individual items is the same as for stringProperty.- Parameters:
name
- Name of the property to build- Returns:
- A new builder
-
charProperty
public static PropertyBuilder.GenericPropertyBuilder<Character> charProperty(String name)
Returns a builder for a character property. The property descriptor will accept any single character string. No unescaping is performed other than what the XML parser does itself. That means that Java escape sequences are not expanded: e.g. "\n", will be represented as the character sequence '\' 'n', so it's not a valid value for this type of property. On the other hand, XML character references are expanded, like & ('&') or < ('<').- Parameters:
name
- Name of the property to build- Returns:
- A new builder
-
charListProperty
public static PropertyBuilder.GenericCollectionPropertyBuilder<Character,List<Character>> charListProperty(String name)
Returns a builder for a property having as value a list of characters. The format of the individual items is the same as for charProperty.- Parameters:
name
- Name of the property to build- Returns:
- A new builder
-
booleanProperty
public static PropertyBuilder.GenericPropertyBuilder<Boolean> booleanProperty(String name)
Returns a builder for a boolean property. The boolean is parsed from the XML usingBoolean.valueOf(String)
, i.e. the only truthy value is the string "true", and all other string values are falsy.- Parameters:
name
- Name of the property to build- Returns:
- A new builder
-
enumProperty
public static <T> PropertyBuilder.GenericPropertyBuilder<T> enumProperty(String name, Map<String,T> nameToValue)
Returns a builder for an enumerated property. Such a property can be defined for any type<T>
, provided the possible values can be indexed by strings. This is enforced by passing aMap<String, T>
at construction time, which maps labels to values. IfMap.get(Object)
returns null for a given label, then the value is rejected. Null values are hence prohibited.- Type Parameters:
T
- Value type of the property- Parameters:
name
- Name of the property to buildnameToValue
- Map of labels to values. The null key is ignored.- Returns:
- A new builder
- Throws:
IllegalArgumentException
- If the map contains a null value or key
-
enumProperty
public static <T extends Enum<T>> PropertyBuilder.GenericPropertyBuilder<T> enumProperty(String name, Class<T> enumClass)
Returns a builder for an enumerated property for the given enum class, using thetoString
of its enum constants as labels.- Type Parameters:
T
- Type of the enum class- Parameters:
name
- Property nameenumClass
- Enum class- Returns:
- A new builder
-
enumProperty
public static <T extends Enum<T>> PropertyBuilder.GenericPropertyBuilder<T> enumProperty(String name, Class<T> enumClass, Function<? super T,@NonNull String> labelMaker)
Returns a builder for an enumerated property for the given enum class, using the given function to label its constants.- Type Parameters:
T
- Type of the enum class- Parameters:
name
- Property nameenumClass
- Enum classlabelMaker
- Function that labels each constant- Returns:
- A new builder
- Throws:
NullPointerException
- If any of the arguments is nullIllegalArgumentException
- If the label maker returns null on some constantIllegalStateException
- If the label maker maps two constants to the same label
-
enumListProperty
public static <T> PropertyBuilder.GenericCollectionPropertyBuilder<T,List<T>> enumListProperty(String name, Map<String,T> nameToValue)
Returns a builder for a property having as value a list of<T>
. The format of the individual items is the same as for enumProperty(String, Map).- Type Parameters:
T
- Value type of the property- Parameters:
name
- Name of the property to buildnameToValue
- Map of labels to values. The null key is ignored.- Returns:
- A new builder
-
enumListProperty
public static <T extends Enum<T>> PropertyBuilder.GenericCollectionPropertyBuilder<T,List<T>> enumListProperty(String name, Class<T> enumClass, Function<? super T,String> labelMaker)
Returns a builder for a property having as value a list of<T>
. The format of the individual items is the same as for enumProperty(String, Map).- Type Parameters:
T
- Value type of the property- Parameters:
name
- Name of the property to buildenumClass
- Class of the valueslabelMaker
- Function that associates enum constants to their label- Returns:
- A new builder
-
-