Blankj/AndroidUtilCode · error · UnsupportedOperationException
u can't instantiate me...
Error message
u can't instantiate me...
What it means
MapUtils is a utility class providing static factory methods for creating and populating maps (newHashMap, newUnmodifiableMap, newTreeMap, etc.) from pairs. Its private constructor throws UnsupportedOperationException to enforce static-only access.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/MapUtils.java:24
import java.util.Comparator;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
/**
* <pre>
* author: blankj
* blog : http://blankj.com
* time : 2019/08/12
* desc : utils about map
* </pre>
*/
public class MapUtils {
private MapUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Returns a new read-only map with the specified contents, given as a list of pairs
* where the first value is the key and the second is the value.
*
* @param pairs a list of pairs
* @return a new read-only map with the specified contents
*/
@SafeVarargs
public static <K, V> Map<K, V> newUnmodifiableMap(final Pair<K, V>... pairs) {
return Collections.unmodifiableMap(newHashMap(pairs));
}
@SafeVarargs
public static <K, V> HashMap<K, V> newHashMap(final Pair<K, V>... pairs) {
HashMap<K, V> map = new HashMap<>();
if (pairs == null || pairs.length == 0) {View on GitHub (pinned to 7b4caf9e54)
Solutions
- Use static factory methods: MapUtils.newHashMap(pairs), MapUtils.newUnmodifiableMap(pairs). Never instantiate.
- Exclude utility classes from DI scanning and serialization configs.
- Mock static methods with mockito-inline for testing.
Example fix
// before
MapUtils mu = new MapUtils();
Map<String, Integer> map = mu.newHashMap(Pair.create("a", 1));
// after
Map<String, Integer> map = MapUtils.newHashMap(Pair.create("a", 1)); Defensive patterns
Strategy: type-guard
Validate before calling
// MapUtils is static-only — use factory methods directly
Map<String, Integer> map = MapUtils.newHashMap(
Pair.create("a", 1), Pair.create("b", 2)); Type guard
static boolean isUtilityClass(Class<?> c) {
return Modifier.isFinal(c.getModifiers()) && c.getSimpleName().endsWith("Utils");
} Try / catch
try {
MapUtils.class.getDeclaredConstructor().setAccessible(true);
MapUtils.class.getDeclaredConstructor().newInstance();
} catch (UnsupportedOperationException e) {
// Use static factory methods instead
} Prevention
- Never instantiate classes ending in 'Utils'.
- Exclude util packages from DI and serialization scanning.
- Use static factory methods: MapUtils.newHashMap(), newTreeMap(), etc.
When it happens
Trigger: Direct or reflective instantiation by DI frameworks (Dagger, Guice, Spring), mocking libraries (Mockito, PowerMock), serialization frameworks (Gson, Jackson), or manual reflection.
Common situations: Component scanning in a DI framework traverses the utility package and attempts construction; a test suite instantiates all classes for coverage analysis; a serialization library targets the class.
Related errors
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14).
Data as JSON: /api/errors/383c5832fa7a77bd.
Report an issue: GitHub.