{"record":{"id":"4e3039b7de6d7e8e","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-4e3039","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/EncryptUtils.java","lineNumber":37,"sourceCode":"import javax.crypto.Mac;\nimport javax.crypto.SecretKey;\nimport javax.crypto.SecretKeyFactory;\nimport javax.crypto.spec.DESKeySpec;\nimport javax.crypto.spec.IvParameterSpec;\nimport javax.crypto.spec.SecretKeySpec;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2016/08/02\n *     desc  : utils about encrypt\n * </pre>\n */\npublic final class EncryptUtils {\n\n    private EncryptUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    ///////////////////////////////////////////////////////////////////////////\n    // hash encryption\n    ///////////////////////////////////////////////////////////////////////////\n\n    /**\n     * Return the hex string of MD2 encryption.\n     *\n     * @param data The data.\n     * @return the hex string of MD2 encryption\n     */\n    public static String encryptMD2ToString(final String data) {\n        if (data == null || data.length() == 0) return \"\";\n        return encryptMD2ToString(data.getBytes());\n    }\n\n    /**","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/EncryptUtils.java#L19-L55","documentation":"EncryptUtils is a final utility class whose private constructor throws UnsupportedOperationException(\"u can't instantiate me...\"). Every hash/symmetric/asymmetric/hmac routine is static, so no instance is needed; the constructor exists purely to block instantiation, matching the *Utils convention used throughout the library.","triggerScenarios":"Reflective instantiation via getDeclaredConstructor().newInstance() with setAccessible, or handing the Class to a reflective factory. A plain `new EncryptUtils()` is a compile error because the constructor is private.","commonSituations":"Coverage tooling that walks and instantiates classes; DI containers that scan packages; a developer who assumes instance methods; mutation testing that targets constructors.","solutions":["Call the static API directly: EncryptUtils.encryptMD5ToString(...), EncryptUtils.encryptAES(...), etc.","Configure any reflective instantiation tool to skip final classes or specifically exclude EncryptUtils.","In a contract unit test, assert UnsupportedOperationException is thrown rather than using the instance."],"exampleFix":"// before (reflection)\nEncryptUtils eu = EncryptUtils.class.getDeclaredConstructor().newInstance();\n\n// after\nString md5 = EncryptUtils.encryptMD5ToString(\"data\");","handlingStrategy":"validation","validationCode":"// Never construct EncryptUtils; it is static-only.\nif (clazz == EncryptUtils.class || Modifier.isFinal(clazz.getModifiers())) {\n    // skip instantiation; use EncryptUtils.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 = EncryptUtils.class.getDeclaredConstructor();\n    c.setAccessible(true);\n    c.newInstance();\n} catch (UnsupportedOperationException e) {\n    // expected; use EncryptUtils.xxx() instead\n}","preventionTips":["EncryptUtils is static-only — never instantiate via `new` or reflection.","Exclude final 'Utils' classes from coverage/DI/mutation tools that construct everything.","Call EncryptUtils.encryptMD5ToString()/encryptAES()/encryptRSA() statically."],"tags":["utility-class","instantiation","design-pattern","reflection","encrypt"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}