{"record":{"id":"f4c85ec82566459d","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-f4c85e","errorCode":null,"errorMessage":"u can't instantiate me...","messagePattern":"u can't instantiate me\\.\\.\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/SizeUtils.java","lineNumber":20,"sourceCode":"\nimport android.content.res.Resources;\nimport android.util.DisplayMetrics;\nimport android.util.TypedValue;\nimport android.view.View;\nimport android.view.ViewGroup;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2016/08/02\n *     desc  : utils about size\n * </pre>\n */\npublic final class SizeUtils {\n\n    private SizeUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Value of dp to value of px.\n     *\n     * @param dpValue The value of dp.\n     * @return value of px\n     */\n    public static int dp2px(final float dpValue) {\n        final float scale = Resources.getSystem().getDisplayMetrics().density;\n        return (int) (dpValue * scale + 0.5f);\n    }\n\n    /**\n     * Value of px to value of dp.\n     *\n     * @param pxValue The value of px.\n     * @return value of dp","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/SizeUtils.java#L2-L38","documentation":"SizeUtils is a final static-only utility for dp/sp/px conversions. Its private constructor throws UnsupportedOperationException as an instantiation guard. Every method is static, so the class is meant to be used as a namespace, not constructed.","triggerScenarios":"Reflective instantiation (SizeUtils.class.getDeclaredConstructor().setAccessible(true).newInstance()) or transitive construction via Gson/Jackson, Mockito/PowerMock, Kotlin reflection, or a DI scan.","commonSituations":"Deserialization mapping a field to SizeUtils; mock frameworks instantiating utility classes; coverage tools; accidental `new SizeUtils()` after dropping the final modifier.","solutions":["Call static methods: SizeUtils.dp2px(8) instead of constructing the class.","Register a Gson InstanceCreator/Jackson mixin returning a sentinel to prevent reflective construction.","Mock static methods with Mockito.mockStatic rather than instantiating.","Preserve final + private constructor so tooling does not treat the class as instantiable."],"exampleFix":"// before\nSizeUtils su = gson.fromJson(json, SizeUtils.class);\n\n// after\nint px = SizeUtils.dp2px(8);","handlingStrategy":"validation","validationCode":"if (Modifier.isFinal(SizeUtils.class.getModifiers())) {\n  throw new IllegalStateException(\"SizeUtils is static-only; use dp2px etc.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use SizeUtils static methods; never construct.","Provide InstanceCreator/mixin for JSON utility fields.","Mock statically with Mockito.mockStatic.","Preserve final + private constructor."],"tags":["android","utility-class","reflection","instantiation","dimension"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}