J2EE rules: DoNotUseThreads gives false positive for getting the current thread
A source code analyzer
Brought to you by:
adangel,
juansotuyo
As a starting point, I was trying to follow the recommendations in UseProperClassLoader, which states:
"In J2EE getClassLoader() might not work as expected. Use Thread.currentThread().getContextClassLoader() instead."
And I ran into an issue with the DoNotUseThreads rule -- it looks like it's giving false positives to a number of cases that aren't actually creating threads.
public class Test public void method test() { // This doesn't trigger the rule: Thread.currentThread(); // But this does: Thread currentThread = Thread.currentThread(); } // This doesn't trigger the rule: private Object getThreadAsObject() { return Thread.currentThread(); } // But this does: private Thread getThread() { return Thread.currentThread(); } // This also triggers the rule even though it does no operations on the thread: private void takesAThread(final Thread thread) { } }
(This is using PMD 5.0.3)
Well, I think the intention of the "DoNotUseThreads" rule was, that you should not at all deal with the Thread objects directly.
The call to
Thread.currentThread().getContextClassLoader()
doesn't trigger this rule - so you can still follow "UseProperClassLoader".The rule is triggered, whenever you have a variable type or return type
Thread
. I'm not sure, if it's worth to extend the rule to exclude e.g.currentThread
.What do you think?