Menu

#1093 J2EE rules: DoNotUseThreads gives false positive for getting the current thread

PMD-Backlog
more-info-needed
nobody
None
PMD
3-Major
Bug
2015-01-11
2013-05-09
No

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) {
    }
}

Discussion

  • Timothy Tucker

    Timothy Tucker - 2013-05-09

    (This is using PMD 5.0.3)

     
  • Andreas Dangel

    Andreas Dangel - 2013-08-10

    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?

     
  • Andreas Dangel

    Andreas Dangel - 2013-08-10
    • status: open --> more-info-needed
    • Milestone: New Tickets --> PMD-5.1.x
     
  • Andreas Dangel

    Andreas Dangel - 2014-02-09
    • Milestone: PMD-5.1.x --> PMD-Backlog
     

Log in to post a comment.