GeezerVison is showing github.com — [core] Consistent implementation of enum properties · Issue #6184 · pmd/pmd
[core] Consistent implementation of enum properties #6184DescriptionIs your feature request related to a problem? Please describe.
As pointed out in #6131 rule properties based on enums are implemented in different ways. They should be implemented in a consistent way.
The Enum Property Builder uses a "labelMaker" function, that is used to convert the enum constant name into a string for usage in the ruleset xml.
Some use toString (net.sourceforge.pmd.lang.java.rule.codestyle.ConfusingTernaryRule.NullCheckBranch), some use a custom adhoc mapping (net.sourceforge.pmd.lang.java.rule.codestyle.ModifierOrderRule.TypeAnnotationPosition, net.sourceforge.pmd.lang.java.rule.documentation.CommentRequiredRule.CommentRequirement) and other use camel case conversion via CaseConvention.CAMEL_CASE (net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidReassigningLoopVariablesRule.ForeachReassignOption).
Ideally, we would use the same notation for all enum properties.
Describe the solution you'd like
- Make it programmatically impossible to do the wrong thing: have only one overloaded method net.sourceforge.pmd.properties.PropertyFactory#enumProperty
- Other methods to create a enum property should be deprecated
- We need a solution for backwards compatibility - we might find the old notations used in rulesets, so we should still be able to read it. Ideally issuing a deprecation warning
- When generating the rule docs, we should generate a possible values for an enum property. This is a smaller task than [doc] Produce example configs for rule properties #4297 but would still be helpful I think.
- The notation and convention should be
enum values in Java follow the usual naming convention to be SCREAMING_SNAKE_CASE
- enum values, that are used for rule properties, should use the "camelCase" naming convention
- the values in ruleset xml should use the same notation - no extra conversion (Enum::name() and Enum::valueOf)
Describe alternatives you've considered
Additional context
- this will affect compatibility...
Activityadangel commented on Oct 29, 2025 Note that the following comment is wrong:
|
* The RuleDocGenerator uses toString() to determine the default value. |
The RuleDocGenerator uses the properties' serialization form and not toString...
|
defaultValue = propertyDescriptor.serializer().toString(realDefaultValue); |
adangel commented on Nov 13, 2025 Currently used enum properties:
- mapping function
- AvoidReassigningLoopVariables
- foreachReassign:
allow, deny, firstOnly -> camelCase
- forReassign:
allow, deny, skip -> camelCase
- ModifierOrder
- typeAnnotations:
anywhere, ondecl, ontype -> camelCase
- CommentRequired
- methodWithOverrideCommentRequirement:
Ignored, Required, Unwanted -> PascalCase
- note: the possible values are documented for each property in the property description.
- toString mapping
- mapping map
- JavaLanguageProperties: xTypeInferenceLogging
- xTypeInferenceLogging:
DISABLED, SIMPLE, VERBOSE -> SCREAMING_SNAKE_CASE
- enumList with mapping map
- enumList with mapping function
Current doc https://docs.pmd-code.org/latest/pmd_userdocs_extending_defining_properties.html suggests to use no explicit mapping (so, defaults to toString). But uses camel case in the example enum.
Summary:
- ⭐ camelCase: 5 rules
- PascalCase: 2 rules
- need to be changed:
- CommentRequired -> methodWithOverrideCommentRequirement:
ignored, required, unwanted
- ConfusingTernary -> nullCheckBranch:
any, else, then
- space separated: 1 rule
- needs to be changed:
- AvoidUsingHardCodedIP -> checkAddressTypes:
IPv4, IPv4MappedIPv6, IPv6
- SCREAMING_SNAKE_CASE: once for language properties
- we could decided to leave this as is. xTypeInferenceLogging is anyway a internal property.
added a commit that references this issue on Jan 16, 2026This issue will close once commit 6af8c38 is merged into the 'main' branch.6af8c38
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|