From 2756fe43840e930af6ebf882907a43586f09c023 Mon Sep 17 00:00:00 2001 From: Ivan Hetman Date: Tue, 31 Oct 2023 10:46:43 +0200 Subject: [PATCH] classlib: more argument validation in Collections methods (#839) --- .../classlib/java/util/TCollections.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TCollections.java b/classlib/src/main/java/org/teavm/classlib/java/util/TCollections.java index 5927f019c..4381f2306 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TCollections.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TCollections.java @@ -183,6 +183,9 @@ public class TCollections extends TObject { } public static TList nCopies(final int n, final T o) { + if (n < 0) { + throw new IllegalArgumentException(); + } return new TAbstractList<>() { @Override public T get(int index) { if (index < 0 || index >= n) { @@ -455,6 +458,7 @@ public class TCollections extends TObject { } public static TList unmodifiableList(final TList list) { + TObjects.requireNonNull(list); return new TAbstractList<>() { @Override public T get(int index) { return list.get(index); @@ -462,10 +466,14 @@ public class TCollections extends TObject { @Override public int size() { return list.size(); } + @Override public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } }; } public static TCollection unmodifiableCollection(final TCollection c) { + TObjects.requireNonNull(c); return new TAbstractCollection<>() { @Override public TIterator iterator() { return unmodifiableIterator(c.iterator()); @@ -473,6 +481,9 @@ public class TCollections extends TObject { @Override public int size() { return c.size(); } + @Override public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } }; } @@ -491,6 +502,7 @@ public class TCollections extends TObject { } public static TSet unmodifiableSet(final TSet s) { + TObjects.requireNonNull(s); return new TAbstractSet<>() { @Override public TIterator iterator() { return unmodifiableIterator(s.iterator()); @@ -498,14 +510,21 @@ public class TCollections extends TObject { @Override public int size() { return s.size(); } + @Override public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } }; } public static TMap unmodifiableMap(final TMap m) { + TObjects.requireNonNull(m); return new TAbstractMap<>() { @Override public TSet> entrySet() { return unmodifiableMapEntrySet(m.entrySet()); } + @Override public V remove(Object o) { + throw new UnsupportedOperationException(); + } }; } @@ -518,6 +537,9 @@ public class TCollections extends TObject { @Override public TIterator> iterator() { return unmodifiableMapEntryIterator(c.iterator()); } + @Override public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } }; }