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
Issue-637 #1802
base: master
Are you sure you want to change the base?
Issue-637 #1802
Conversation
…at String Checks"
-Split "Issue637" class into two separate classes, one expecting errors and one expecting no errors -Refactored "Issue637Test" file to reflect new, simplified test format -Added JavaDocs for public functions
…d constructors to DateFormatStringChecker -Added tests for new functionality to corresponding test classes
@@ -0,0 +1,137 @@ | |||
/* | |||
* FindBugs - Find bugs in Java programs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New files should not use a FindBugs copyright notice.
spotbugs/src/main/java/edu/umd/cs/findbugs/detect/DateFormatStringChecker.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution!
Please check my feedback to keep the code maintainable. Also, run ./gradlew spotlessApply
to format java code.
AM_PM_HOURS_FLAG_1, AM_PM_HOURS_FLAG_2, MILITARY_HOURS_FLAG_1, MILITARY_HOURS_FLAG_2, WEEK_YEAR_FLAG_1 | ||
)); | ||
|
||
if (CONST_ARRAY_LIST.contains(seen) && stack.getStackDepth() > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better to apply the early return, to keep nests in the code less.
if (!CONST_ARRAY_LIST.contains(seen) || stack.getStackDepth() == 0) {
return;
}
|
||
private static final String BUG_TYPE = "FS_BAD_DATE_FORMAT_FLAG_COMBO"; | ||
|
||
String dateFormatString; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This field is used only by one method, then better to replace it with a local variable.
return result; | ||
} | ||
|
||
private boolean checkStringForBadCombo(String stringToCheck, String missingFlag, List<String> requiredFlags) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me runCheckStringForBadCombo
and checkStringForBadCombo
sounds the same.
Could you consider renaming them, or adding documentation comments to clarify the difference?
spotbugs/etc/messages.xml
Outdated
This format stream includes a bad combination of format flags which may lead to unexpected behavior. Examples include | ||
using a week year ("Y") without specifying week in year ("w") or using an AM/PM hour ("h" or "K") without specifying | ||
an AM/PM marker ("a"). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider providing the list of full examples.
Users will not read SpotBugs source code, we need a document or URL to tell 'what is the problem' and 'how you can fix it'.
spotbugs/src/main/java/edu/umd/cs/findbugs/detect/DateFormatStringChecker.java
Outdated
Show resolved
Hide resolved
private static final List<String> AM_PM_HOURS_FLAG_2 = new ArrayList<>(Arrays.asList("a", "K")); | ||
private static final List<String> MILITARY_HOURS_FLAG_1 = new ArrayList<>(Arrays.asList(null,"H","a")); | ||
private static final List<String> MILITARY_HOURS_FLAG_2 = new ArrayList<>(Arrays.asList(null,"k","a")); | ||
private static final List<String> WEEK_YEAR_FLAG_1 = new ArrayList<>(Arrays.asList("w","Y","M","d")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this list Arrays.asList("w","Y","M","d")
the first element has different meaning. It would be better to introduce a class to replace this list.
private static final class DateFormatRule {
/** Required flag to run verification by this rule. Always apply rule if this field is {@code null}. */
@Nullable
String precondition;
/** List of required flags in the pattern. */
@NonNull
List<String> requiredFlags;
/** @return {@code true} if precondition matches and all required flags exist in the given pattern. */
boolean verify(String dateFormatString) { ... }
}
Co-authored-by: Kengo TODA <skypencil+github@gmail.com>
Co-authored-by: Kengo TODA <skypencil+github@gmail.com>
Co-authored-by: Kengo TODA <skypencil+github@gmail.com>
-Updates description for FS_BAD_DATE_FORMAT_FLAG_COMBO in "messages.xml" -Reflects suggested changes to DateFormatStringChecker, including adding a nested DateFormatRule object and other smaller updates
Thank you for the feedback, @KengoTODA! I have reflected your suggestions in the latest commit. Please let me know what you think. |
private static final String BUG_TYPE = "FS_BAD_DATE_FORMAT_FLAG_COMBO"; | ||
|
||
String dateFormatString; | ||
final BugReporter bugReporter; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This field could be private.
if (this.dateFormatString != null && (sig.indexOf(SIG_CONSTANT_OPERAND_1) >= 0 || | ||
sig.indexOf(SIG_CONSTANT_OPERAND_2) >= 0 || sig.indexOf(SIG_CONSTANT_OPERAND_3) >= 0 ) | ||
&& CLASS_CONSTANT_OPERAND.equals(cl) && (NAME_CONSTANT_OPERAND_1.equals(nm) | ||
|| NAME_CONSTANT_OPERAND_2.equals(nm) || NAME_CONSTANT_OPERAND_3.equals(nm))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To enhance readability, it's better to have the related parts in one line, and have the line break consistently before the ||
and &&
signs.
BugInstanceMatcher bugTypeMatcher = new BugInstanceMatcherBuilder() | ||
.bugType(DESIRED_BUG_TYPE) | ||
.build(); | ||
assertThat(getBugCollection(), containsExactly(25, bugTypeMatcher)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add asserts for every bug checking the information you added to the BugInstance (e.g. containing method, source line)?
Addresses Issue #637 by adding new detector & associated test case classes for bad combinations of format strings used with SimpleDateFormat
(Apologies for any confusion, I know @arnab44 had been assigned to this issue about a month ago, but I am a first-time contributor who took up this issue for a university project)
Make sure these boxes are checked before submitting your PR -- thank you!
CHANGELOG.md
if you have changed SpotBugs code