EI_EXPOSE_REP

EI: May expose internal representation by returning reference to mutable object

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.

回傳mutable object可能會造成被外部程式修改。

拷貝一份新的回去或改用immutable object。

Before:

    public Date getLeftDate(){
        return mLeftDate;
    }

After:

    public Date getLeftDate(){
        if( mLeftDate == null )
            return null;
        return new Date(mLeftDate.getTime());
    }

Array的情況最簡單的方法是用Arrays.copyOf。而因為直接使用getTime,必須小心null的情況。