projectlombok/lombok · error · UnsupportedOperationException

List is immutable

Error message

List is immutable

What it means

LombokImmutableList is a persistent, immutable list; its iterator's remove() (and mutating operations generally) always throws UnsupportedOperationException("List is immutable"). The class exists so cached/shared lists can never be modified in place.

Solutions

  1. Copy into a mutable list before mutating: new ArrayList<>(immutableList), then add/remove on the copy.
  2. Do not call remove() on its iterator; filter instead (build a new list of wanted elements).
  3. If you control the producer, return/maintain a mutable List when in-place mutation is required.
  4. In cleanup-style loops, use Collection.removeIf on a mutable copy.

Example fix

// before
for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
    if (it.next().isEmpty()) it.remove(); // UnsupportedOperationException
}
// after
List<String> mutable = new ArrayList<>(list);
mutable.removeIf(String::isEmpty);
Defensive patterns

Strategy: type-guard

Type guard

// treat any List as possibly immutable; wrap before mutating
static <T> List<T> mutableCopy(List<T> l) {
    return l instanceof java.util.ArrayList<T> ? l : new ArrayList<>(l);
}

Try / catch

try {
    it.remove();
} catch (UnsupportedOperationException e) {
    // list is immutable: fall back to a mutable copy
    list = new ArrayList<>(list);
}

Prevention

When it happens

Trigger: Calling iterator().remove(), or any add/set/remove mutating method, on a LombokImmutableList instance — e.g. iterating a list obtained from lombok internals and trying to remove elements, or passing it to code that mutates collections in place.

Common situations: Framework code (e.g. processors or serializers) that defensively removes items from a passed-in List; cleanup loops calling it.remove(); tests assuming java.util.ArrayList semantics.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of projectlombok/lombok@6d6a3e9fec (2026-09-07). Data as JSON: /api/errors/f1df02b383abfc1f. Report an issue: GitHub.

Appendix: source

Thrown at src/utils/lombok/core/LombokImmutableList.java:177

		for (Object e : content) if (in.equals(e)) return true;
		return false;
	}
	
	public Iterator<T> iterator() {
		return new Iterator<T>() {
			private int idx = 0;
			@Override public boolean hasNext() {
				return idx < content.length;
			}
			
			@SuppressWarnings("unchecked")
			@Override public T next() {
				if (idx < content.length) return (T) content[idx++];
				throw new NoSuchElementException();
			}
			
			@Override public void remove() {
				throw new UnsupportedOperationException("List is immutable");
			}
		};
	}
	
	@Override public String toString() {
		return Arrays.toString(content);
	}
	
	@Override public boolean equals(Object obj) {
		if (!(obj instanceof LombokImmutableList)) return false;
		if (obj == this) return true;
		return Arrays.equals(content, ((LombokImmutableList<?>) obj).content);
	}
	
	@Override public int hashCode() {
		return Arrays.hashCode(content);
	}
}

View on GitHub (pinned to 6d6a3e9fec)