Blankj/AndroidUtilCode · info · UnsupportedOperationException
u can't instantiate me...
Error message
u can't instantiate me...
What it means
BrightnessUtils is a final utility class with only static brightness helpers; its private constructor throws UnsupportedOperationException('u can't instantiate me...') to prevent construction. This is an intentional guard reachable only through reflection, because the class is final and the constructor is private.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/BrightnessUtils.java:22
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2018/02/08
* desc : utils about brightness
* </pre>
*/
public final class BrightnessUtils {
private BrightnessUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Return whether automatic brightness mode is enabled.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isAutoBrightnessEnabled() {
try {
int mode = Settings.System.getInt(
Utils.getApp().getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE
);
return mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
return false;
}View on GitHub (pinned to 7b4caf9e54)
Solutions
- Call static methods directly (e.g., BrightnessUtils.isAutoBrightnessEnabled()) and never instantiate.
- Exclude the constructor from coverage or let the coverage helper handle it.
- If testing the guard, write a test asserting UnsupportedOperationException is thrown, not one that tries to succeed.
- Strip reflective newInstance() calls against BrightnessUtils.class.
Defensive patterns
Strategy: validation
Validate before calling
// Never instantiate; call static methods directly. boolean auto = BrightnessUtils.isAutoBrightnessEnabled();
Try / catch
try {
Constructor<BrightnessUtils> c = BrightnessUtils.class.getDeclaredConstructor();
c.setAccessible(true);
c.newInstance();
} catch (UnsupportedOperationException | InvocationTargetException e) {
// expected: instantiation is forbidden by design
} Prevention
- Call BrightnessUtils static methods directly; never reflectively instantiate it.
- Exclude private constructors from coverage tooling that auto-instantiates.
- For guard tests, assert UnsupportedOperationException instead of trying to succeed.
When it happens
Trigger: Reflectively invoking the private constructor (setAccessible(true) + newInstance()); coverage tooling or generic reflection utilities that instantiate every class; test helpers that brute-force private constructors.
Common situations: Code-coverage tools (JaCoCo/Cobertura) generating coverage for private constructors; reflection-based unit tests that instantiate all classes in a package; serialization/DI frameworks attempting default construction.
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/8a3357a0ea99d054.
Report an issue: GitHub.