{"record":{"id":"eb15769539d864bb","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-eb1576","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/ImageUtils.java","lineNumber":70,"sourceCode":"import androidx.annotation.FloatRange;\nimport androidx.annotation.IntRange;\nimport androidx.annotation.NonNull;\nimport androidx.annotation.Nullable;\nimport androidx.annotation.RequiresApi;\nimport androidx.core.content.ContextCompat;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2016/08/12\n *     desc  : utils about image\n * </pre>\n */\npublic final class ImageUtils {\n\n    private ImageUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Bitmap to bytes.\n     *\n     * @param bitmap The bitmap.\n     * @return bytes\n     */\n    public static byte[] bitmap2Bytes(final Bitmap bitmap) {\n        return bitmap2Bytes(bitmap, CompressFormat.PNG, 100);\n    }\n\n    /**\n     * Bitmap to bytes.\n     *\n     * @param bitmap  The bitmap.\n     * @param format  The format of bitmap.\n     * @param quality The quality.","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/ImageUtils.java#L52-L88","documentation":"ImageUtils is a final utility class whose private constructor throws UnsupportedOperationException(\"u can't instantiate me...\"). All image helpers (bitmap2Bytes, bytes2Bitmap, rotate, scale, round, save, etc.) are static; the constructor exists only to block instantiation, matching the library-wide *Utils convention.","triggerScenarios":"Reflective instantiation via getDeclaredConstructor().newInstance() with setAccessible, or passing the Class to a reflective factory/coverage tool. A normal `new ImageUtils()` is a compile error because the constructor is private.","commonSituations":"Coverage/quality tools that instantiate every class; DI or reflective object mappers; a developer who expects instance methods; service loaders that scan packages.","solutions":["Call the static API directly: ImageUtils.bitmap2Bytes(...), ImageUtils.rotate(...), ImageUtils.save(...), etc.","Add ImageUtils to the exclusion list of any tool that reflectively instantiates classes.","For a contract test, expect UnsupportedOperationException from the reflected constructor."],"exampleFix":"// before (reflection)\nImageUtils iu = ImageUtils.class.getDeclaredConstructor().newInstance();\n\n// after\nbyte[] png = ImageUtils.bitmap2Bytes(bitmap);","handlingStrategy":"validation","validationCode":"// Never construct ImageUtils; it is static-only.\nif (clazz == ImageUtils.class || Modifier.isFinal(clazz.getModifiers())) {\n    // skip instantiation; use ImageUtils.xxx() statically\n}","typeGuard":"if (clazz.getName().endsWith(\"Utils\") && Modifier.isFinal(clazz.getModifiers())) {\n    // static-only; do not newInstance()\n}","tryCatchPattern":"try {\n    Constructor<?> c = ImageUtils.class.getDeclaredConstructor();\n    c.setAccessible(true);\n    c.newInstance();\n} catch (UnsupportedOperationException e) {\n    // expected; use ImageUtils.xxx() instead\n}","preventionTips":["ImageUtils is static-only — never instantiate via `new` or reflection.","Exclude final 'Utils' classes from coverage/DI/mutation tools that construct everything.","Call ImageUtils.bitmap2Bytes()/rotate()/scale()/save() statically."],"tags":["utility-class","instantiation","design-pattern","reflection","image"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}