{"record":{"id":"6ec842a170ca75bd","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-6ec842","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/ResourceUtils.java","lineNumber":28,"sourceCode":"\nimport androidx.annotation.DrawableRes;\nimport androidx.annotation.RawRes;\nimport androidx.core.content.ContextCompat;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2018/05/07\n *     desc  : utils about resource\n * </pre>\n */\npublic final class ResourceUtils {\n\n    private static final int BUFFER_SIZE = 8192;\n\n    private ResourceUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Return the drawable by identifier.\n     *\n     * @param id The identifier.\n     * @return the drawable by identifier\n     */\n    public static Drawable getDrawable(@DrawableRes int id) {\n        return ContextCompat.getDrawable(Utils.getApp(), id);\n    }\n\n    /**\n     * Return the id identifier by name.\n     *\n     * @param name The name of id.\n     * @return the id identifier by name\n     */","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/ResourceUtils.java#L10-L46","documentation":"ResourceUtils is a final utility class providing static methods for Android resource access (getDrawable, readFileFromAssets, copyFileFromAssets, readStringFromAssets, etc.). Its private constructor throws UnsupportedOperationException to enforce static-only access.","triggerScenarios":"Direct or reflective instantiation by DI frameworks, mocking libraries, serialization frameworks, or manual reflection.","commonSituations":"DI component scanning traverses the utility package; a test framework instantiates all classes for coverage; a serialization library targets the class.","solutions":["Use static methods: ResourceUtils.getDrawable(id), ResourceUtils.readFileFromAssets(name). Never call new ResourceUtils().","Exclude utility classes from DI scanning and serialization configs.","Mock static methods with mockito-inline for testing."],"exampleFix":"// before\nResourceUtils ru = new ResourceUtils();\nDrawable d = ru.getDrawable(R.drawable.icon);\n\n// after\nDrawable d = ResourceUtils.getDrawable(R.drawable.icon);","handlingStrategy":"type-guard","validationCode":"// ResourceUtils is static-only\nDrawable d = ResourceUtils.getDrawable(R.drawable.icon);\nString content = ResourceUtils.readFileFromAssets(\"config.json\");","typeGuard":"static boolean isStaticUtility(Class<?> c) {\n    return Modifier.isFinal(c.getModifiers()) && c.getSimpleName().endsWith(\"Utils\");\n}","tryCatchPattern":"try {\n    ResourceUtils.class.getDeclaredConstructor().setAccessible(true);\n    ResourceUtils.class.getDeclaredConstructor().newInstance();\n} catch (UnsupportedOperationException e) {\n    // Use static methods\n}","preventionTips":["Never instantiate classes ending in 'Utils'.","Exclude util packages from DI and serialization scanning.","Use ResourceUtils.getDrawable(), readFileFromAssets(), etc. statically."],"tags":["java","android","utility-class","instantiation","resource"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}