I have a copy constructor for class, but Android Studio code inspection throws a warning I don’t understand:
Suspicious assignment in copy constructor of
‘java.util.Arrays.copyOf(other.value, other.value.length)’ to field
value
public class CpuVariable extends BaseIdentifier {
private int memoryType;
private byte[] value;
public CpuVariable(@NonNull CpuVariable other) {
super(other);
this.memoryType = other.memoryType;
if (other.value != null) {
this.value = java.util.Arrays.copyOf(other.value, other.value.length);
}
}
}
Changing code to
this.value = other.value
would remove the warning, but this is not an option since I need to create a deep copy or a clone for the field.
Am I coding something wrong or is it safe to ignore or suppress the warning?
2
Answers
It is clearly a false positive. There is nothing actually wrong with your constructor.
I think that the code that produced this warning is based on this code. Note that this is not the real Android Studio code, but there are clues to suggest that Android Studio may have "borrowed" it via some path.
If you look at the
constructorAssignsAllFields
method (line 63), the intent of the code seems to be to look for code bugs where a copy constructor is copying the wrong fields; e.g. something like this:But the method is not correctly dealing with the case where the copy constructor is transforming one of the fields.
Looking at the code, you need to write
this.value =
in a way that makes the checker not realize that it is assigning to a field. For example, if you used a setter method something like this:It appears this can also happen if there’s any other problem with the copy constructor, such as if the copy constructor is not copying all fields. I’m also seeing this same error in IntelliJ in that case.