Search code examples
javaimmutabilityencapsulationgetter-setterfindbugs

Findbug immutable date fixes do not work


I have maven findbug plugin version 2.5.4, and in my code it shows me error:

.getDateRlpx() may expose internal representation by returning Price.dateRlpx 
.setDateRlpx(Date) may expose internal representation by storing an externally mutable object into Price.dateRlpx 

However in my code I use:

public Date getDateRlpx() {
    return DateUtil.immutableDate(dateRlpx);
}

public void setDateRlpx(final Date dateRlpx) {
    this.dateRlpx = DateUtil.immutableDate(dateRlpx);
}

And my immutableDate:

public static Date immutableDate(final Date sourceDate) {
    if (sourceDate == null) {
        return null;
    }
    return new Date(sourceDate.getTime());
}

Why does findbug show this error? I created a new Date object. I even tried simple return new Date(dateRlpx.getTimes()) but it also did not work. I could not find any solution.


Solution

  • Yeah, Thomas was right, it was problem with environment, specificly with running tomcat server from IDEA IDE, it somehow prevented findbug to check fixed source code and instead it checked the one in target directory or in .war i didnt check it for sure. So now it's all work and findbug agree that new Date(date.getTime()) is a solution :)