Search code examples
javasonarqube

Sonar violation on "disallowed assignment of parameters"


I have the following code and I got an sonar violation error: disallowed assignment of parameters What is the best way to fix this?

   /**
     * @param lastAccessTime the lastAccessTime to set
     */
    public void setLastAccessTime(Date lastAccessTime)
    {
        this.lastAccessTime = lastAccessTime == null ? null : new Date(lastAccessTime.getTime());
    }

Solution

  • I suspect one of 2 things are happening here:

    1 - There is a bug in the checkstyle plugin

    2 - The code sonar analysed is not quite the code you posted here

    I believe that violation should apply in the following case:

        /**
         * @param lastAccessTime the lastAccessTime to set
         */
        public void setLastAccessTime(Date lastAccessTime)
        {
            lastAccessTime = lastAccessTime == null ? null : new Date(lastAccessTime.getTime());
        }
    

    So when you are reassigning the method parameter it would be expected, but in your example you are not, you are assigning it to a class field so it should be ok.

    Try changing the method parameter to final and see if you still see the violation.