These checks address miscellaneous problems.
The PropertyCatalog check helps to keep a property file in sync with a piece of code that contains the property keys. That piece of code is called the “property catalog”, and may be:
The property catalog and the properties file are expected to be connected to each other either by a configurable naming convention.
The property catalog code could look like this (other forms are possible, see below):
The constructor arguments of the enum constants are the keys to the property file, which in turn looks like this:
In this example, the property with key one
is orphaned, because it is not referenced by the property catalog. It would be flagged along with the duplicate reference to two
.
In a property catalog, the enum constants themselves may also be used as keys:
The property catalog can also consist of normal constants. Private constants are ignored.
Important: The keys in a property catalog must be simple literals of type String
, int
,
long
, or boolean
. No arithmetic allowed, no method calls, no references to other constants.
Just simple literals. Remember that Checkstyle works on source code.
The check roughly works like this:
selection
are checked.excludedFields
are ignored.propertyFile
, the corresponding property file is located.If the property catalog is a class or interface, the values of the defined constants are the property keys. If the property catalog is an enum, you can choose whether the enum constant itself shall be the key, or the first parameter passed to the enum constant’s constructor (via enumArgument
). In a class, private constants are never considered.
This check find duplicate keys in the Java code, but not in the property file. Use UniqueProperties for that. It also does not help keep the translations of your property files in sync (e.g. file.properties, file_de.properties, file_fr.properties, etc.). Use Translation for that, and configure this check only for one of the property files.
$
signs, for example
com.foo.Outer$Inner
.
^(?!x)x
(check is disabled)excludedFields
is applied to the name of the constant, not the key. With the
exception of an enum constant used as key, constant and key are two separate things (see examples above).
serialVersionUID
false
), or
the first argument of the enum constant's constructor (true
). When the property catalog is not an
enum, then this property is ignored.
false
com.foo.Bar$Inner
):
{0}
com.foo.Bar$Inner
){1}
com/foo/Bar/Inner
){2}
com.foo.Bar
){3}
com/foo/Bar
){4}
..
's (../../..
){5}
com/foo
){6}
Bar
){7}
Inner
)UTF-8
true
true
true
, the property keys are treated as case sensitive; if false
,
case is ignored.
true
In the following example, it is assumed that you have a naming convention which requires all property catalogs to have a type name that ends in Catalog
. The corresponding property file is assumed to share the name of the Java file and reside in the same package, but under src/main/resources:
The above example is for Eclipse, where ${workspace_loc}
may be used to refer to the file system location of the current workspace. For SonarQube, you may use relative file paths. For the other environments, you may define a custom module property, which you dynamically set to the project directory. Example for Gradle (here, projectDir
was used instead of workspace_loc
):