Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PMD message "Avoid using java.lang.ThreadGroup; it is not thread safe"

Tags:

java

pmd

Question:

Why the maven PMD plugin is giving me the following warning for the line of code below: Warning:

Avoid using java.lang.ThreadGroup; it is not thread safe

Code (second line):

Calendar cal = Calendar.getInstance();
java.sql.Date endDate = new java.sql.Date(cal.getTime().getTime());

Context:

I have this confit a java.sql.Date instantiation:

public class XYZServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    this.doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp)
        throws ServletException, IOException {


    Connection conn = null;
    CallableStatement stmt = null;
    try {
        conn = ...
        ...

        Calendar cal = Calendar.getInstance();
        java.sql.Date endDate = new java.sql.Date(cal.getTime().getTime());

        ...
    } catch ...
    }finally {
        try {
            stmt.close();
            conn.close();
        } catch(Exception e) {}
    }
}

}

The line reported by PMD is

java.sql.Date endDate = new java.sql.Date(cal.getTime().getTime());

And the message is:

Avoid using java.lang.ThreadGroup; it is not thread safe
like image 707
Lorenzo Solano Martinez Avatar asked Mar 19 '12 14:03

Lorenzo Solano Martinez


2 Answers

This seems to be a bug in PMD 4.2.6.

SourceForge-Link:
http://sourceforge.net/projects/pmd/forums/forum/188192/topic/4781145

like image 101
oers Avatar answered Oct 03 '22 22:10

oers


Relevant bugs from the project. This was fixed in 5.X of PMD. It is also in the 3.0.1 version of the PMD maven plugin.

  • http://sourceforge.net/p/pmd/code/ci/master/tree/pmd/etc/changelog.txt
    • Fixed bug 2724653 - AvoidThreadGroup reports false positives
  • Relevant bugs:
    • http://sourceforge.net/p/pmd/bugs/858/
    • http://sourceforge.net/p/pmd/bugs/532/
    • http://sourceforge.net/p/pmd/bugs/1018/ -- Won't Fix in 4.3.x
    • Workaround remains to avoid using package name inline.

I saw this question before I found the bugs. Maybe this'll help the next coder.

like image 34
Jason Avatar answered Oct 03 '22 21:10

Jason