Blankj/AndroidUtilCode · error · UnsupportedOperationException
u can't instantiate me...
Error message
u can't instantiate me...
What it means
StringUtils is a final static-only utility for string checks and manipulation. Its private constructor throws UnsupportedOperationException to prevent instantiation. All public methods are static, so construction is a contract violation.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/StringUtils.java:21
import android.content.res.Resources;
import androidx.annotation.ArrayRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import java.util.IllegalFormatException;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/08/16
* desc : utils about string
* </pre>
*/
public final class StringUtils {
private StringUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Return whether the string is null or 0-length.
*
* @param s The string.
* @return {@code true}: yes<br> {@code false}: no
*/
public static boolean isEmpty(final CharSequence s) {
return s == null || s.length() == 0;
}
/**
* Return whether the string is null or whitespace.
*
* @param s The string.
* @return {@code true}: yes<br> {@code false}: no
*/View on GitHub (pinned to 7b4caf9e54)
Solutions
- Use static methods: StringUtils.isEmpty(s) instead of constructing the class.
- Provide a Gson InstanceCreator / Jackson mixin returning a sentinel to avoid constructor invocation.
- Mock static methods with Mockito.mockStatic instead of constructing StringUtils.
- Keep the class final with a private constructor.
Example fix
// before StringUtils su = new StringUtils(); // after boolean empty = StringUtils.isEmpty(s);
Defensive patterns
Strategy: validation
Validate before calling
if (Modifier.isFinal(StringUtils.class.getModifiers())) {
throw new IllegalStateException("StringUtils is static-only.");
} Prevention
- Call StringUtils.isEmpty(...) etc.; never construct.
- Register InstanceCreator/mixin for JSON fields typed as StringUtils.
- Use Mockito.mockStatic for mocking.
- Keep final + private constructor.
When it happens
Trigger: Reflective instantiation (StringUtils.class.getDeclaredConstructor().setAccessible(true).newInstance()) or transitive construction through Gson/Jackson deserialization, Mockito/PowerMock, Kotlin reflection, or a DI classpath scan.
Common situations: JSON libraries binding a field to StringUtils; test frameworks mocking utility classes; coverage probes; accidental instantiation 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/1d00fc9320b61a6d.
Report an issue: GitHub.