Class 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 file type extension of the files to check. Type is java.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 a Locale independent order mechanism and 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" and nothing behind. On line 6 you have "key." The dot is on position 46 which is higher than nothing. key.png will be 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
    • Method Detail

      • getLineNumber

        private static int getLineNumber​(int startLineNo,
                                         FileText fileText,
                                         java.lang.String previousProp,
                                         java.lang.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 line
        fileText - FileText object contains the lines to process
        previousProp - key name found last iteration, works only if valid
        propKey - 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,
                                    java.lang.String keyName)
        Inner method to get the index number of the position of keyName.
        Parameters:
        startLineNo - start searching at line
        fileText - FileText object contains the lines to process
        keyName - key name to look for
        Returns:
        index number of first occurrence. If no key found in properties file, 0 is returned
      • getKeyPattern

        private static java.util.regex.Pattern getKeyPattern​(java.lang.String keyName)
        Method returns regular expression pattern given key name.
        Parameters:
        keyName - key name to look for
        Returns:
        regular expression pattern given key name