jwtk/jjwt · error · UnsupportedOperationException

Not supported

Error message

Not supported

What it means

UnsupportedOperationException thrown by the remove() method of the EnumerationIterator adapter in io.jsonwebtoken.lang.Collections. The iterator wraps an Enumeration, which has no removal capability, so remove is intentionally unsupported — calling it always throws.

Solutions

  1. Copy the Enumeration into a real collection first (e.g. Collections.list(enumeration)) and iterate/remove on that
  2. Avoid calling remove(); filter elements into a new collection instead of mutating during iteration
  3. If removal is required, obtain a true collection iterator rather than an Enumeration adapter

Example fix

// before
Iterator<String> it = Collections.asIterator(enumeration);
it.next();
it.remove(); // throws UnsupportedOperationException
// after
List<String> list = Collections.list(enumeration);
Iterator<String> it = list.iterator();
it.next();
it.remove(); // works
Defensive patterns

Strategy: type-guard

Validate before calling

Iterator<E> it = ...;
if (!(it instanceof java.util.ListIterator) && isEnumerationBacked(it)) {
    it = Collections.list(enumeration).iterator(); // swap to a removable iterator
}

Type guard

static <E> boolean supportsRemove(Iterator<E> it) {
    try {
        it.getClass().getDeclaredMethod("remove");
        return !(it.getClass().getName().contains("Enumeration"));
    } catch (NoSuchMethodException e) {
        return false;
    }
}

Try / catch

try {
    it.remove();
} catch (UnsupportedOperationException e) {
    // enumeration-backed iterator; rebuild as a modifiable collection and retry
    List<E> list = new ArrayList<>();
    it.forEachRemaining(list::add);
    list.remove(list.size() - 1);
}

Prevention

When it happens

Trigger: Calling Iterator.remove() on an iterator obtained by iterating over an Enumeration (e.g. java.util.Collections.list(enumeration)-style adapters or Collections.asIterator/enumeration-wrapped loops), then attempting to remove the current element.

Common situations: Generic code that removes elements while iterating (fail-fast pattern) being fed an Enumeration-backed iterator; refactored loops that assumed a modifiable collection; passing enumeration-based iterators into APIs that try to remove.

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 jwtk/jjwt@fb71496164 (2026-09-09). Data as JSON: /api/errors/c292a54cfadba353. Report an issue: GitHub.

Appendix: source

Thrown at api/src/main/java/io/jsonwebtoken/lang/Collections.java:569

     */
    private static class EnumerationIterator<E> implements Iterator<E> {

        private final Enumeration<E> enumeration;

        public EnumerationIterator(Enumeration<E> enumeration) {
            this.enumeration = enumeration;
        }

        public boolean hasNext() {
            return this.enumeration.hasMoreElements();
        }

        public E next() {
            return this.enumeration.nextElement();
        }

        public void remove() throws UnsupportedOperationException {
            throw new UnsupportedOperationException("Not supported");
        }
    }
}

View on GitHub (pinned to fb71496164)