1

I am facing a problem while storing arrays in setter method. I tried to resolve this issue by solution like below.

Problem:

public void setMyArray(String[] myArray) { 
    this.myArray = myArray; 
} 

Solution:

public void setMyArray(String[] newMyArray) { 
    if (newMyArray == null) { 
        this.myArray = new String[0]; 
    } else { 
       this.myArray = Arrays.copyOf(newMyArray, newMyArray.length); 
    } 
}

I also tried with clone() method as described in below link Sonar Violation: Security - Array is stored directly but is of no use to get rid of sonar violation. Please suggest me some methods. Thanks in advance

Community
  • 1
  • 1
  • This is a findbugs violation. Using the clone method should actually solve the issue. – benzonico Nov 04 '15 at 09:05
  • @benzonico I tried using clone method too but of no help. – user3526989 Nov 04 '15 at 09:07
  • Then you appear to have found a bug in FindBugs. :) – G. Ann - SonarSource Team Nov 04 '15 at 12:05
  • @G.Ann-SonarSourceTeam, I doubt there's a FindBugs bug like this (I worked with this part of FindBugs code). To OP: please provide the whole code snippet including clone() as well as exact warning issued by FindBugs (having FindBugs XML report excerpt would be even better, don't know how to extract it in Sonar though). – Tagir Valeev Nov 09 '15 at 13:07

0 Answers0