Reports return of, or assignment from a method parameter to a field of an array or a mutable type such as Collection, Date, Map, Calendar, etc.

이러한 타입은 가변적이므로, 이 구문으로 인해 소유 클래스 외부에서 예기치 않은 객체 상태 수정이 발생할 수 있습니다. 이 구문은 성능면에서 유익할 수 있지만 본질적으로 버그가 발생하기 쉽습니다.

다음과 같은 가변 타입이 보고됩니다:

The quick-fix adds a call to the field's .clone() method for arrays or uses an unmodifiable collection wrapper.

예:


  import java.util.*;
  class Log {
    private String[] messages = {"one", "two", "three"};
    private Map<String, String> map = new HashMap<>();

    String[] getMessages() {
      return messages; // warning: Return of String[] field 'messages'
    }

    Map<String, String> mapping() {
      return map; // warning: Return of Map<String, String> field 'map'
    }
  }

빠른 수정을 적용한 후:


  import java.util.*;
  class Log {
    String[] messages = {"one", "two", "three"};
    private Map<String, String> map = new HashMap<>();

    String[] getMessages() {
      return messages.clone();
    }

    Map<String, String> mapping() {
      return Collections.unmodifiableMap(map);
    }
  }

private 메서드의 대입 및 반환 무시 옵션을 사용하여 private 메서드의 대입과 반환을 무시합니다.