Repository navigation
Closed
Labels
a:false-negativePMD doesn't flag a problematic piece of code
Milestone
Description
Affects PMD Version:
7.26.0
Rule:
ReplaceJavaUtilDate
Likely also the case for ReplaceJavaUtilCalendar
Description:
Using pattern variable suppresses violations.
Code Sample demonstrating the issue:
import java.time.LocalDateTime;
import java.time.ZoneId;
public final class PmdCanary {
private PmdCanary() {
}
// no warnings
//@SuppressWarnings("PMD.ReplaceJavaUtilDate")
public static LocalDateTime toLocalDateTimePatternVariable(Object o) {
if (o instanceof LocalDateTime localDateTime) {
return localDateTime;
}
if (o instanceof java.sql.Timestamp timestamp) {
return timestamp.toLocalDateTime();
}
if (o instanceof java.sql.Date sqlDate) {
return sqlDate.toLocalDate().atStartOfDay();
}
if (o instanceof java.util.Date date) {
return date.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
}
return null;
}
@SuppressWarnings("PMD.ReplaceJavaUtilDate")
public static LocalDateTime toLocalDateTime(Object o) {
if (o instanceof LocalDateTime) {
return (LocalDateTime) o;
}
if (o instanceof java.sql.Timestamp) {
java.sql.Timestamp timestamp = (java.sql.Timestamp) o; // warns
return timestamp.toLocalDateTime();
}
if (o instanceof java.sql.Date) {
java.sql.Date sqlDate = (java.sql.Date) o; // warns
return sqlDate.toLocalDate().atStartOfDay();
}
if (o instanceof java.util.Date) {
java.util.Date date = (java.util.Date) o; // warns
return date.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
}
return null;
}
}Expected outcome:
PMD should report violations consistently for both versions of the method.
The version using pattern variables is currently not causing any violations.
I also agree with #6025 that java.sql classes should probably not trigger a violation at all.
Running PMD through: Gradle
Metadata
Metadata
Assignees
Labels
a:false-negativePMD doesn't flag a problematic piece of code
Activity
[java] Fix #7009: ReplaceJavaUtilDate/Calendar miss pattern variables (…
[-][java] ReplaceJavaUtilDate is suppressed by using pattern variable[/-][+][java] ReplaceJavaUtilDate: False negative when using pattern matching[/+]huxi commentedon Aug 30, 2026
Now that was super fast! Thanks a lot! ❤️