Suppress Android Unread Field Warning Using Byte []

I am using java.lang.SuppressWarnings in Android Studio.

I can not get rid of this:

 EI_EXPOSE_REP2: May expose internal representation by incorporating reference to mutable object (findbugs task) 

This happens using the installation 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; } 

A warning:

 setmPicture(byte[]) may expose internal representation by storing an externally mutable object into PropertyDetailDocumentStorageModel.mPicture 

Note that this happens in a single field whose type is byte[] . Other fields in the same class that getters do not generate this warning.

+6
source share
2 answers

As @Thomas suggested, arrays are always mutable. The fix returned 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

 public byte[] getmPicture() { return mPicture; } public void setmPicture(byte[] picture) { this.mPicture = picture; } 

I did not know that for another type, such as String, a simple getter always returns a copy of the object. This does not apply to arrays.

+1
source

After clarifying some of the comments in the comments, I think the answer is this.

  • URF_UNREAD_FIELD - A field is considered read if the getter exists, because then FindBugs assumes that the field is read from outside the class. If this does not happen, you have some kind of false positive that will require further analysis or suppression.
  • EI_EXPOSE_REP2 - Arrays are always mutable, so when you return an array from the receiver, you modify it. You can return a copy of the array via Arrays.copyOf() or, again, suppress the warning.

FindBugs warnings are suppressed using the @SuppressFBWarnings ( doc ) annotation. You need annotations.jar and jsr305.jar from the FindBugs lib folder on the way to the analysis process classes for FindBugs annotations to work. Example:

 @SuppressFBWarnings("URF_UNREAD_FIELD") 
0
source

All Articles