Class OrderedPropertiesCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
-
- com.puppycrawl.tools.checkstyle.checks.OrderedPropertiesCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
,FileSetCheck
public class OrderedPropertiesCheck extends AbstractFileSetCheck
Detects if keys in properties files are in correct order.
Rationale: Sorted properties make it easy for people to find required properties by name in file. It makes merges more easy. While there are no problems at runtime. This check is valuable only on files with string resources where order of lines does not matter at all, but this can be improved. E.g.: checkstyle/src/main/resources/com/puppycrawl/tools/checkstyle/messages.properties You may suppress warnings of this check for files that have an logical structure like build files or log4j configuration files. See SuppressionFilter.
<suppress checks="OrderedProperties" files="log4j.properties|ResourceBundle/Bug.*.properties|logging.properties"/>
Known limitation: The key should not contain a newline. The string compare will work, but not the line number reporting.
-
Property
fileExtensions
- Specify file type extension of the files to check. Type isjava.lang.String[]
. Default value is.properties
.
To configure the check:
<module name="OrderedProperties"/>
Example properties file:
A =65 a =97 key =107 than nothing key.sub =k is 107 and dot is 46 key.png =value - violation
We check order of key's only. Here we would like to use an Locale independent order mechanism, an binary order. The order is case insensitive and ascending.
- The capital A is on 65 and the lowercase a is on position 97 on the ascii table.
- Key and key.sub are in correct order here, because only keys are relevant. Therefore on line 5 you have only "key" an nothing behind. On line 6 you have "key." The dot is on position 46 which is higher than nothing. key.png will reported as violation because "png" comes before "sub".
Parent is
com.puppycrawl.tools.checkstyle.Checker
Violation Message Keys:
-
properties.notSorted.property
-
unable.open.cause
- Since:
- 8.22
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
MSG_IO_EXCEPTION_KEY
Localization key for IO exception occurred on file open.static java.lang.String
MSG_KEY
Localization key for check violation.
-
Constructor Summary
Constructors Constructor Description OrderedPropertiesCheck()
Construct the check with default values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
processFiltered(java.io.File file, FileText fileText)
Processes the file and check order.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
addMessages, beginProcessing, destroy, finishProcessing, fireErrors, getFileContents, getFileExtensions, getMessageDispatcher, getMessages, getTabWidth, init, log, log, process, setFileContents, setFileExtensions, setMessageDispatcher, setTabWidth
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.puppycrawl.tools.checkstyle.api.Configurable
configure
-
Methods inherited from interface com.puppycrawl.tools.checkstyle.api.Contextualizable
contextualize
-
-
-
-
Field Detail
-
MSG_KEY
public static final java.lang.String MSG_KEY
Localization key for check violation.- See Also:
- Constant Field Values
-
MSG_IO_EXCEPTION_KEY
public static final java.lang.String MSG_IO_EXCEPTION_KEY
Localization key for IO exception occurred on file open.- See Also:
- Constant Field Values
-
-
Method Detail
-
processFiltered
protected void processFiltered(java.io.File file, FileText fileText)
Processes the file and check order.- Specified by:
processFiltered
in classAbstractFileSetCheck
- Parameters:
file
- the file to be processedfileText
- the contents of the file.
-
-