Blankj/AndroidUtilCode · error · UnsupportedOperationException
u can't instantiate me...
Error message
u can't instantiate me...
What it means
UriUtils is a final static-only utility for URI/resource-path conversions. Its private constructor throws UnsupportedOperationException to prevent instantiation. All public methods (res2Uri, etc.) are static.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/UriUtils.java:36
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import androidx.core.content.FileProvider;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2018/04/20
* desc : utils about uri
* </pre>
*/
public final class UriUtils {
private UriUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Resource to uri.
* <p>res2Uri([res type]/[res name]) -> res2Uri(drawable/icon), res2Uri(raw/icon)</p>
* <p>res2Uri([resource_id]) -> res2Uri(R.drawable.icon)</p>
*
* @param resPath The path of res.
* @return uri
*/
public static Uri res2Uri(String resPath) {
return Uri.parse("android.resource://" + Utils.getApp().getPackageName() + "/" + resPath);
}
/**
* File to uri.
*
* @param file The file.View on GitHub (pinned to 7b4caf9e54)
Solutions
- Use static methods: UriUtils.res2Uri(resPath) instead of constructing the class.
- Provide a Gson InstanceCreator / Jackson mixin returning a sentinel to avoid constructor invocation.
- Use Mockito.mockStatic for mocking rather than constructing UriUtils.
- Keep the class final with a private constructor.
Example fix
// before
UriUtils uu = new UriUtils();
// after
Uri uri = UriUtils.res2Uri("drawable/icon"); Defensive patterns
Strategy: validation
Validate before calling
if (Modifier.isFinal(UriUtils.class.getModifiers())) {
throw new IllegalStateException("UriUtils is static-only.");
} Prevention
- Call UriUtils.res2Uri(...) statically.
- Register InstanceCreator/mixin for JSON fields.
- Use Mockito.mockStatic for mocking.
- Keep final + private constructor.
When it happens
Trigger: Reflective instantiation via UriUtils.class.getDeclaredConstructor().setAccessible(true).newInstance(), or transitively through Gson/Jackson deserialization, Mockito/PowerMock, Kotlin reflection, or a DI scan.
Common situations: JSON mapping a field to UriUtils; mock frameworks in tests; coverage probes; accidental construction after weakening access modifiers.
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/a83a985a12e8fc6f.
Report an issue: GitHub.