Gre*_*reg 7 java arrays android findbugs
我java.lang.SuppressWarnings在Android Studio中使用该软件包.
我无法摆脱这一个:
EI_EXPOSE_REP2: May expose internal representation by incorporating reference to mutable object (findbugs task)
Run Code Online (Sandbox Code Playgroud)
它发生在setter方法中.
如何摆脱这种警告?
public class PropertyDetailDocumentStorageModel implements Parcelable {
@SerializedName("picture")
private byte[] mPicture;
public void setmPicture(byte[] mPicture) { this.mPicture = mPicture; }
Run Code Online (Sandbox Code Playgroud)
警告:
setmPicture(byte[]) may expose internal representation by storing an externally mutable object into PropertyDetailDocumentStorageModel.mPicture
Run Code Online (Sandbox Code Playgroud)
请注意,这是在类型为唯一的字段上发生的byte[].同一类中具有getter的其他字段不会抛出此警告.
所以就像@Thomas 建议的那样,数组总是可变的。修复方法是返回属性的副本而不是属性本身:
public byte[] getmPicture() { return Arrays.copyOf(mPicture, mPicture.length); }
public void setmPicture(final byte[] picture) { this.mPicture = Arrays.copyOf(picture, picture.length); }
Run Code Online (Sandbox Code Playgroud)
代替
public byte[] getmPicture() { return mPicture; }
public void setmPicture(byte[] picture) { this.mPicture = picture; }
Run Code Online (Sandbox Code Playgroud)
我不知道的是,对于其他类型(例如 String),简单的 getter 总是会返回对象的副本。数组的情况并非如此。
| 归档时间: |
|
| 查看次数: |
1241 次 |
| 最近记录: |