I'm using the java.lang.SuppressWarnings
package in Android Studio.
I can't get rid of this one:
EI_EXPOSE_REP2: May expose internal representation by incorporating reference to mutable object (findbugs task)
It is happening with a setter method.
How to get rid of this warning?
public class PropertyDetailDocumentStorageModel implements Parcelable {
@SerializedName("picture")
private byte[] mPicture;
public void setmPicture(byte[] mPicture) { this.mPicture = mPicture; }
Warning:
setmPicture(byte[]) may expose internal representation by storing an externally mutable object into PropertyDetailDocumentStorageModel.mPicture
Note this is happening on the only field whose type is byte[]
. Other fields in the same class which have getters are not throwing this warning.
After clarifying some stuff in the comments, I think the answer is this.
Arrays.copyOf()
, or, again, suppress the warning.FindBugs warnings are suppressed via the @SuppressFBWarnings
annotation (doc). You need annotations.jar and jsr305.jar from the FindBugs lib folder on the classpath of the analysis process for FindBugs annotations to work. Example:
@SuppressFBWarnings("URF_UNREAD_FIELD")
So like @Thomas suggested, Arrays are always mutable. The fix was returning a copy of the property instead of the property itself:
public byte[] getmPicture() { return Arrays.copyOf(mPicture, mPicture.length); }
public void setmPicture(final byte[] picture) { this.mPicture = Arrays.copyOf(picture, picture.length); }
instead of
public byte[] getmPicture() { return mPicture; }
public void setmPicture(byte[] picture) { this.mPicture = picture; }
What I didn't know is that for other type like String for example, a simple getter would always return a copy of the object. That is not the case for Arrays.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With