Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SonarQube:不应直接存储或返回可变成员_Java_Spring_Spring Mvc_Sonarqube_Set - Fatal编程技术网

Java SonarQube:不应直接存储或返回可变成员

Java SonarQube:不应直接存储或返回可变成员,java,spring,spring-mvc,sonarqube,set,Java,Spring,Spring Mvc,Sonarqube,Set,这是我目前的代码: public class RestApplication extends Application { private final Set<Class<?>> resourceSet; public RestApplication() { this.resourceSet = new HashSet<Class<?>>(); resourceSet.add(PCardController.class);

这是我目前的代码:

public class RestApplication extends Application {

  private final Set<Class<?>> resourceSet;
  public RestApplication() {
    this.resourceSet = new HashSet<Class<?>>();
    resourceSet.add(PCardController.class);
    resourceSet.add(PricingController.class);
    resourceSet.add(TransactionFeedController.class);
    initializeMaps();
  }

  @Override
  public Set<Class<?>> getClasses() {
    return resourceSet;
    // ...
  }   // Added by edit!
  // ...
}     // Added by edit!
公共类重新启动应用程序扩展了应用程序{
私人决赛>();
添加(PCardController.class);
添加(PricingController.class);
add(TransactionFeedController.class);
初始化映射();
}
@凌驾

公共集您可以返回一个新的哈希集:

@Override
public Set<Class<?>> getClasses() {
    return new HashSet<>(resourceSet);
}
@覆盖

公共设置或者使结果不可修改;更好的使用,更好的占地面积和速度

@Override
public Set<Class<?>> getClasses() {
    return Collections.unmodifiableSet(resourceSet);
}
@覆盖

public Set此错误是因为Java在某些情况下通过引用返回。这里有一个测试来演示引用通过。size()返回3

List<String> list=new ArrayList<String>();;

List<String> getList(){
    list.add("hello");
    return list;
}
@Test
public void run(){
    getList().add("hey");
    List<String> list = getList();
    list.size();
}
List List=new ArrayList();;
List getList(){
添加(“你好”);
退货清单;
}
@试验
公开募捐{
getList().add(“hey”);
List=getList();
list.size();
}