Check for SimpleDateFormat as singleton?
A source code analyzer
Brought to you by:
adangel,
juansotuyo
Not sure how if this should be done. But since
SimpleDateFormat should not be reused by other
threads as it is not thread safe, we should check for
(public,private,protected) static final? AnyType foo =
new SimpleDateFormat(...)
I don't recommend spreading this to all formatters,
though it says that it does not guarantee thread safety
on formatters some are threadsafe such as
FastDateFormat from commons-lang.
Logged In: YES
user_id=5159
Hm, something like this would probably work:
//FieldDeclaration[@Static='true']//AllocationExpression/Name[@Image='SimpleDateFormat']
The problem is that it's hard to tell what's going on in the
code around it - I mean, if the accessors are synchronized,
it may well be OK.
Yours,
Tom
Logged In: YES
user_id=47007
I believe that this rule applies to all the DateFormats, not
just simple date format.
Here are the relevant bugs:
http://developer.java.sun.com/developer/bugParade/bugs/4093418.html
http://developer.java.sun.com/developer/bugParade/bugs/4228335.html
http://developer.java.sun.com/developer/bugParade/bugs/4261469.html
Logged In: YES
user_id=1127445
Tom
I had this as a rule on my wishlist, it's a common problem I
see (unsynchronized static formatters)
The rule I have for this is on the bug I just entered - but
basically, it's checking every time you touch the formatter
that it is in a synchronized manner.
Sound good?
Logged In: YES
user_id=1127445
This has been implemented since PMD 3.6 - see rule
UnsynchronizedStaticDateFormatter