{"record":{"id":"1303062f48be4542","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-130306","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/PhoneUtils.java","lineNumber":29,"sourceCode":"\nimport androidx.annotation.NonNull;\nimport androidx.annotation.RequiresPermission;\n\nimport static android.Manifest.permission.CALL_PHONE;\nimport static android.Manifest.permission.READ_PHONE_STATE;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2016/08/02\n *     desc  : utils about phone\n * </pre>\n */\npublic final class PhoneUtils {\n\n    private PhoneUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Return whether the device is phone.\n     *\n     * @return {@code true}: yes<br>{@code false}: no\n     */\n    public static boolean isPhone() {\n        TelephonyManager tm = getTelephonyManager();\n        return tm.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE;\n    }\n\n    /**\n     * Return the unique device id.\n     * <p>If the version of SDK is greater than 28, it will return an empty string.</p>\n     * <p>Must hold {@code <uses-permission android:name=\"android.permission.READ_PHONE_STATE\" />}</p>\n     *\n     * @return the unique device id","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/PhoneUtils.java#L11-L47","documentation":"PhoneUtils is a final utility class providing static methods for telephony operations (isPhone, getDeviceIMEI, getSerial, dial, 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; Mockito mock creation targeting PhoneUtils; a serialization library targets the class; ProGuard keep rules enable reflective construction.","solutions":["Use static methods: PhoneUtils.isPhone(), PhoneUtils.getDeviceIMEI(). Never call new PhoneUtils().","Exclude utility classes from DI scanning and serialization configs.","Mock static methods with mockito-inline for testing."],"exampleFix":"// before\nPhoneUtils pu = new PhoneUtils();\nboolean isPhone = pu.isPhone();\n\n// after\nboolean isPhone = PhoneUtils.isPhone();","handlingStrategy":"type-guard","validationCode":"// PhoneUtils is static-only\nboolean isPhone = PhoneUtils.isPhone();\nString imei = PhoneUtils.getDeviceIMEI();","typeGuard":"static boolean isStaticUtility(Class<?> c) {\n    return Modifier.isFinal(c.getModifiers()) && c.getSimpleName().endsWith(\"Utils\");\n}","tryCatchPattern":"try {\n    PhoneUtils.class.getDeclaredConstructor().setAccessible(true);\n    PhoneUtils.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.","Mock static methods with mockito-inline for testing PhoneUtils."],"tags":["java","android","utility-class","instantiation","telephony"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}