Guava의 Immutable Collection에 put/add 를 호출하면, UnsupportedOperationException 예외가 발생한다. 


public class ImmutableTest {

 @Test 

 public void test() {

  ImmutableList<String> list = ImmutableList.of("1", "2");

  list.add("put");

 }

}


에러내용


java.lang.UnsupportedOperationException

at com.google.common.collect.ImmutableCollection.add(ImmutableCollection.java:96)




참고 : guava source


 public abstract class ImmutableCollection<E> extends AbstractCollection<E>

    implements Serializable {


 /**

   * Guaranteed to throw an exception and leave the collection unmodified.

   *

   * @throws UnsupportedOperationException always

   * @deprecated Unsupported operation.

   */

  @Deprecated

  @Override

  public final boolean add(E e) {

    throw new UnsupportedOperationException();

  }


Posted by '김용환'
,