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);

Tuesday, May 22, 2012

Convert String to int

What is the best way to convert the String Object that represents a int value to the primitive type int? Just follow this suggestion, that takes advantage of the parseInt method of the Integer object:

String strValue = "1";
int intValue = Integer.parseInt(strValue);

Convert int to String

What is the best way to convert the primitive type int to a String object?

int intValue = 1;
String stringValue = Integer.toString(intValue);