New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[java] New rule: Format strings must match the arguments passed and their count #2187
Comments
This also happens for me in the following code |
@jscancella As far as I know, this can only be done on a best efforts basis with the resource bundles available in the classpath or auxiliary classpath. I presume you wish to read in all the language resource bundles available. Frankly, I don't think this additional requirement can be accommodated. |
@Fernal73 well in my case I only have 1 language currently, but even with that it fails to find it because I am getting this error. |
See above edit. |
But this is the standard way of dealing with internationalization https://docs.oracle.com/en/java/javase/14/intl/internationalization-overview.html#GUID-75FA808F-CA61-4C31-87C5-BD5C12DA5945 |
You would need to decipher the name of the resource file. If it's specified
within the same Java source file, it's straightforward.
Do you have any suggestions as to how to accomplish this? Although I'm not
familiar with the PMD API, this might be beyond its capabilities.
A simpler approach would be to allow the rule to specify the resource file(s) containing the key-value pairs , rather than parse the source.
Is that what you were driving at, @jscancella?
|
I also don't know the PMD API, but how I access it in my project is Being able to list the resource bundle files would be a good start and would allow for others that are not following the default standards of naming convention. |
https://dzone.com/articles/resource-bundle-tricks-and This will need another rule to reinforce it; one that enforces unique keys across families of ResourceBundles. And another to ensure that keys are the same within a family of ResourceBundles. |
This is for the PMD team of maintainers to decide. I do not arbite which issues will be addressed and when. |
https://checkerframework.org/manual/#formatter-checker https://checkerframework.org/manual/#i18n-formatter-checker The Checker framework provides the desired functionality at the byte code level. Personally , I prefer not to add dependencies in the source code unless absolutely necessary i.e., unnecessary imports. Test classes, not so much. I don't mind hearing arguments---pro and against. |
A simpler version of this rule could be implemented where the string format is always expected to be a string literal much like |
Another feature that could be added while supporting multiple languages via resource bundles is to validate that resource keys match in value and number across bundles and that format specifier(s) count for each matching resource string is the same across bundles. This, of course, implies that a resource bundle (usually English) is the master reference and validation flows from this. Do I need to illustrate this or is my language clear enough to comprehend? |
Affects PMD Version:
6.20.0
Rule:
New rule.
Description
SonarSource has an existing rule for Java that verifies argument types and their count for formatted string specifiers lest a runtime-error occurs when incorrectly coded.
https://rules.sonarsource.com/java/RSPEC-3457
I've had this occur a couple of times while coding.
This rule, however, will not cover instances when the format specifiers are dynamically created or loaded from an input file such as a ResourceBundle.
Note: AFAIK, this rule exists neither in Checkstyle nor Spotbugs.
Note: A similar rule for log messages is InvalidLogMessageFormat.
Code Sample demonstrating the issue:
See link above.
Java-API-Methods to be checked:
Similar to InvalidLogMessageFormat:
Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
The text was updated successfully, but these errors were encountered: