天天看點

FindBugs Java 類 Date 屬性 Getter/Setter 方法報錯

May expose internal representation by incorporating reference to mutable object

可以通過合并對可變對象的引用來公開内部表示

This code stores a reference to an externally mutable object into 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. Storing a copy of the object is better approach in many situations.

此代碼将對外部可變對象的引用存儲到對象的内部表示中。 如果不受信任的代碼通路執行個體,并且對可變對象的未經檢查的更改會危及安全性或其他重要屬性,則需要執行不同的操作。 在許多情況下,存儲對象的副本是更好的方法。

** 正确方法如下,用它的clone方法擷取副本進行操作。**

public Date getBirthday() {
        return birthday == null ? null : (Date) birthday.clone();
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday == null ? null : (Date) birthday.clone();
    }