{"record":{"id":"8a3357a0ea99d054","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-8a3357","errorCode":null,"errorMessage":"u can't instantiate me...","messagePattern":"u can't instantiate me\\.\\.\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"info","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/BrightnessUtils.java","lineNumber":22,"sourceCode":"import android.provider.Settings;\nimport android.view.Window;\nimport android.view.WindowManager;\n\nimport androidx.annotation.IntRange;\nimport androidx.annotation.NonNull;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2018/02/08\n *     desc  : utils about brightness\n * </pre>\n */\npublic final class BrightnessUtils {\n\n    private BrightnessUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Return whether automatic brightness mode is enabled.\n     *\n     * @return {@code true}: yes<br>{@code false}: no\n     */\n    public static boolean isAutoBrightnessEnabled() {\n        try {\n            int mode = Settings.System.getInt(\n                    Utils.getApp().getContentResolver(),\n                    Settings.System.SCREEN_BRIGHTNESS_MODE\n            );\n            return mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;\n        } catch (Settings.SettingNotFoundException e) {\n            e.printStackTrace();\n            return false;\n        }","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/BrightnessUtils.java#L4-L40","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Never instantiate; call static methods directly.\nboolean auto = BrightnessUtils.isAutoBrightnessEnabled();","typeGuard":null,"tryCatchPattern":"try {\n    Constructor<BrightnessUtils> c = BrightnessUtils.class.getDeclaredConstructor();\n    c.setAccessible(true);\n    c.newInstance();\n} catch (UnsupportedOperationException | InvocationTargetException e) {\n    // expected: instantiation is forbidden by design\n}","preventionTips":["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."],"tags":["utility-class","private-constructor","reflection","brightnessutils","android","java"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}