{"record":{"id":"a4d358d44b1289d6","repo":"java-native-access/jna","slug":"remove-enummoniker","errorCode":null,"errorMessage":"remove","messagePattern":"remove","errorType":"exception","errorClass":"java.lang.UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/COM/util/EnumMoniker.java","lineNumber":127,"sourceCode":"                // moniker.getPointer(), ppszDisplayName);\n                // COMUtils.checkRC(hr);\n                // String name = ppszDisplayName.getString();\n                // Ole32.INSTANCE.CoTaskMemFree(ppszDisplayName.getPointer().getPointer(0));\n                // TODO: Can we assume that the object is an\n                // IDispatch ?\n                // Unknown unk = new\n                // Unknown(ppunkObject.getValue());\n                Dispatch dispatch = new Dispatch(ppunkObject.getValue());\n                EnumMoniker.this.cacheNext();\n                IDispatch d = EnumMoniker.this.factory.createProxy(IDispatch.class, dispatch);\n                //must release a COM Ref, GetObject returns a pointer with +1\n                int n = dispatch.Release();\n                return d;\n            }\n\n            @Override\n            public void remove() {\n                throw new UnsupportedOperationException(\"remove\");\n            }\n\n        };\n    }\n\n}\n","sourceCodeStart":109,"sourceCodeEnd":134,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/COM/util/EnumMoniker.java#L109-L134","documentation":"EnumMoniker's iterator over the running-object-table monikers does not implement element removal; calling Iterator.remove() throws UnsupportedOperationException with the message 'remove'. The enumeration is read-only by design because the underlying IEnumMoniker does not support safe removal through this wrapper.","triggerScenarios":"Calling iterator.remove() (directly or via e.g. collection.removeIf or a loop removing entries) while iterating the EnumMoniker list of ROT monikers.","commonSituations":"Trying to clean up stale entries in the Running Object Table by removing them from the Java iterator; generic code that always calls remove() after filtering; assuming the Java iterator mirrors mutable collection semantics.","solutions":["Do not call remove() on this iterator; only iterate","Filter the results into your own collection and manipulate that instead","Release/unregister monikers via the appropriate ROT/COM API (IRunningObjectTable.Revoke) if removal is truly required"],"exampleFix":"// before\nfor (Iterator<?> it = enumMoniker.iterator(); it.hasNext();) {\n    Object m = it.next();\n    if (isStale(m)) it.remove(); // UnsupportedOperationException\n}\n// after\nList<?> stale = new ArrayList<>();\nfor (Object m : enumMoniker) {\n    if (isStale(m)) stale.add(m);\n}\n// revoke stale monikers via IRunningObjectTable.Revoke instead","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static boolean supportsRemove(Iterator<?> it) {\n    // EnumMoniker's iterator is read-only; guard before calling remove()\n    try { it.remove(); return true; } catch (UnsupportedOperationException e) { return false; }\n}","tryCatchPattern":"try { it.remove(); } catch (UnsupportedOperationException e) { /* collect and handle removal via ROT Revoke instead */ }","preventionTips":["Treat COM moniker enumerations as read-only","Never call remove() on third-party iterators without checking the contract","Filter into your own collection before manipulating"],"tags":["java","com","iterator","unsupported-operation","rot"],"backgroundTag":"unsupported-operation","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}