Re: How to satisfy the code checker in this instance?
In article
<08bfc392-d9ee-4714-97b6-7f9eb7e11d28@v15g2000prn.googlegroups.com>,
laredotornado <laredotornado@zipmail.com> wrote:
I'm using Java 1.5 with a code checker named PMD. It is complaining
about the following method ...
private static Date parse(final String dateStr, final String dateFormat) {
Date date = null;
try {
final DateFormat sdf = new SimpleDateFormat(dateFormat,
Locale.getDefault());
date = sdf.parse(dateStr);
} catch (Exception e) {
LOGGER.error("Could not parse time:" + dateStr, e);
}
return date;
} // parse
specifically, complaining about the fact that the variable "Date date
= null" is redefined -- first set to null and then later set to a new
value (Found 'DD' anomaly for variable date). Yes, quite a bizarre
warning, but do you know how to rewrite the above to preserve the
functionality while not redefining the variable?
The initialization seems superfluous: just pass a new ParsePosition()
to sdf.parse(). As long as dateStr is not null and dateFormat is
well-formed, there's no exception to catch. Alternatively, you can move
the initialization to a finally clause, but you should probably catch
specific exceptions. In addition, consider using the SimpleDateFormat
constructor that uses the default locale. Finally, note that DD is not
an indisputable data flow anomaly:
<http://pmd.sourceforge.net/rules/controversial.html>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>