While working on model class i face an following warning :--
com.model.products.CategoryModel.setCreatedDate(Date) may expose internal representation by storing an externally mutable object into CategoryModel.createdDate
My model Class
@JsonInclude(Include.NON_NULL)
public class CategoryModel {
private Date createdDate;
public Date getCreatedDate() {
return createdDate;
//Warning Message:--com.model.products.CategoryModel.getCreatedDate(Date) may expose internal representation by storing an externally mutable object into CategoryModel.createdDate
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
//Warning Message:--com.model.products.CategoryModel.setCreatedDate(Date) may expose internal representation by storing an externally mutable object into CategoryModel.createdDate
}
Description of the warning:--
Returning a reference to a mutable object value stored in one of the object's fields exposes the internal representation of the object. If instances are accessed by untrusted code, and unchecked changes to the mutable object would compromise security or other important properties, you will need to do something different. Returning a new copy of the object is better approach in many situations.
In above model class i get an warning , what is the better approach to handle this ?? Thanks in advance...