Wednesday, May 23, 2012

Sonar: Security - Array is stored directly

When we get this Sonar critical violation "Security - Array is stored directly", means that an array is being stored directly, e.g:

public void setInventoryClassId(String[] inventoryClassId) {
  this.inventoryClassId = inventoryClassId;
}

In order to solve this issue, we must do the following:


public void setInventoryClassId(String[] newInventoryClassId) {
  if(newInventoryClassId == null) {
    this.inventoryClassId = new String[0];
  } else {
   this.inventoryClassId = Arrays.copyOf(newInventoryClassId, newInventoryClassId.length);
  }
}

2 comments:

  1. It's not working... also... what happens if the array is null?

    ReplyDelete
    Replies
    1. if(newInventoryClassId == null) is taking care for a null array.
      This worked for me.

      Delete