How to satisfy the code checker in this instance?

L

laredotornado

Hi,

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?

Thanks, - Dave
 
L

Lew

Hi,

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?

private static Date parse(
final String date, final String format) {
try {
final DateFormat df = new SimpleDateFormat( format );
return df.parse( date );
}
catch (Exception e) {
LOGGER.error("Could not parse time:" + dateStr, e);
}
}
 
J

John B. Matthews

laredotornado said:
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>
 
J

Jukka Lahtinen

Lew said:
private static Date parse(
final String date, final String format) {
try {
final DateFormat df = new SimpleDateFormat( format );
return df.parse( date );
}
catch (Exception e) {
LOGGER.error("Could not parse time:" + dateStr, e);
}
}

I think you should add
return null;
to the catch block.
 
J

Jim Janney

laredotornado said:
Hi,

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?

Thanks, - Dave


private static Date parse(final String dateStr, final String dateFormat) {
final DateFormat sdf = new SimpleDateFormat(dateFormat, Locale.getDefault());
return sdf.parse(dateStr);
}

According to the javadoc for SimpleDateFormat.parse(String), it
returns null in case of error, so no exception handling is needed.
 
J

Jim Janney

Jim Janney said:
private static Date parse(final String dateStr, final String dateFormat) {
final DateFormat sdf = new SimpleDateFormat(dateFormat, Locale.getDefault());
return sdf.parse(dateStr);
}

According to the javadoc for SimpleDateFormat.parse(String), it
returns null in case of error, so no exception handling is needed.

Doubly wrong. SimpleDateFormat.parse(String, ParsePosition) returns
null, but DateFormat.parse(String) throws a ParseException. Worse,
the constructor for SimpleDateFormat throws an IllegalArgumentException
if the date format is invalid.
 
L

Lew

Jukka said:
I think you should add
return null;
to the catch block.

Good call. Or one could throw a wrapper exception. One should also
not catch 'Exception' but specific exceptions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top