Blankj/AndroidUtilCode · info · UnsupportedOperationException
u can't instantiate me...
Error message
u can't instantiate me...
What it means
ImageUtils is a final utility class whose private constructor throws UnsupportedOperationException("u can't instantiate me..."). All image helpers (bitmap2Bytes, bytes2Bitmap, rotate, scale, round, save, etc.) are static; the constructor exists only to block instantiation, matching the library-wide *Utils convention.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/ImageUtils.java:70
import androidx.annotation.FloatRange;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/08/12
* desc : utils about image
* </pre>
*/
public final class ImageUtils {
private ImageUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Bitmap to bytes.
*
* @param bitmap The bitmap.
* @return bytes
*/
public static byte[] bitmap2Bytes(final Bitmap bitmap) {
return bitmap2Bytes(bitmap, CompressFormat.PNG, 100);
}
/**
* Bitmap to bytes.
*
* @param bitmap The bitmap.
* @param format The format of bitmap.
* @param quality The quality.View on GitHub (pinned to 7b4caf9e54)
Solutions
- Call the static API directly: ImageUtils.bitmap2Bytes(...), ImageUtils.rotate(...), ImageUtils.save(...), etc.
- Add ImageUtils to the exclusion list of any tool that reflectively instantiates classes.
- For a contract test, expect UnsupportedOperationException from the reflected constructor.
Example fix
// before (reflection) ImageUtils iu = ImageUtils.class.getDeclaredConstructor().newInstance(); // after byte[] png = ImageUtils.bitmap2Bytes(bitmap);
Defensive patterns
Strategy: validation
Validate before calling
// Never construct ImageUtils; it is static-only.
if (clazz == ImageUtils.class || Modifier.isFinal(clazz.getModifiers())) {
// skip instantiation; use ImageUtils.xxx() statically
} Type guard
if (clazz.getName().endsWith("Utils") && Modifier.isFinal(clazz.getModifiers())) {
// static-only; do not newInstance()
} Try / catch
try {
Constructor<?> c = ImageUtils.class.getDeclaredConstructor();
c.setAccessible(true);
c.newInstance();
} catch (UnsupportedOperationException e) {
// expected; use ImageUtils.xxx() instead
} Prevention
- ImageUtils is static-only — never instantiate via `new` or reflection.
- Exclude final 'Utils' classes from coverage/DI/mutation tools that construct everything.
- Call ImageUtils.bitmap2Bytes()/rotate()/scale()/save() statically.
When it happens
Trigger: Reflective instantiation via getDeclaredConstructor().newInstance() with setAccessible, or passing the Class to a reflective factory/coverage tool. A normal `new ImageUtils()` is a compile error because the constructor is private.
Common situations: Coverage/quality tools that instantiate every class; DI or reflective object mappers; a developer who expects instance methods; service loaders that scan packages.
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/eb15769539d864bb.
Report an issue: GitHub.