Class OrderedPropertiesCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
-
- 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. This makes it easier to merge. 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 a 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 the file extensions of the files to process. Type isjava.lang.String[]
. Default value is.properties
.
Parent is
com.puppycrawl.tools.checkstyle.Checker
Violation Message Keys:
-
properties.notSorted.property
-
unable.open.cause
- Since:
- 8.22
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
OrderedPropertiesCheck.SequencedProperties
Private property implementation that keeps order of properties like in file.-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description static String
MSG_IO_EXCEPTION_KEY
Localization key for IO exception occurred on file open.static String
MSG_KEY
Localization key for check violation.private static Pattern
SPACE_PATTERN
Pattern matching single space.
-
Constructor Summary
Constructors Constructor Description OrderedPropertiesCheck()
Construct the check with default values.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static int
getIndex(int startLineNo, FileText fileText, String keyName)
Inner method to get the index number of the position of keyName.private static Pattern
getKeyPattern(String keyName)
Method returns regular expression pattern given key name.private static int
getLineNumber(int startLineNo, FileText fileText, String previousProp, String propKey)
Method returns the index number where the key is detected (starting at 0).protected void
processFiltered(File file, FileText fileText)
Processes the file and check order.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
addViolations, beginProcessing, destroy, finishProcessing, fireErrors, getFileContents, getFileExtensions, getMessageDispatcher, getTabWidth, getViolations, 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.AbstractAutomaticBean
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 String MSG_KEY
Localization key for check violation.- See Also:
- Constant Field Values
-
MSG_IO_EXCEPTION_KEY
public static final String MSG_IO_EXCEPTION_KEY
Localization key for IO exception occurred on file open.- See Also:
- Constant Field Values
-
SPACE_PATTERN
private static final Pattern SPACE_PATTERN
Pattern matching single space.
-
-
Constructor Detail
-
OrderedPropertiesCheck
public OrderedPropertiesCheck()
Construct the check with default values.
-
-
Method Detail
-
processFiltered
protected void processFiltered(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.
-
getLineNumber
private static int getLineNumber(int startLineNo, FileText fileText, String previousProp, String propKey)
Method returns the index number where the key is detected (starting at 0). To assure that we get the correct line it starts at the point of the last occurrence. Also, the previousProp should be in file before propKey.- Parameters:
startLineNo
- start searching at linefileText
-FileText
object contains the lines to processpreviousProp
- key name found last iteration, works only if validpropKey
- key name to look for- Returns:
- index number of first occurrence. If no key found in properties file, 0 is returned
-
getIndex
private static int getIndex(int startLineNo, FileText fileText, String keyName)
Inner method to get the index number of the position of keyName.- Parameters:
startLineNo
- start searching at linefileText
-FileText
object contains the lines to processkeyName
- key name to look for- Returns:
- index number of first occurrence. If no key found in properties file, 0 is returned
-
getKeyPattern
private static Pattern getKeyPattern(String keyName)
Method returns regular expression pattern given key name.- Parameters:
keyName
- key name to look for- Returns:
- regular expression pattern given key name
-
-