{"record":{"id":"c7b33c2f2e250bf4","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-c7b33c","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/DeviceUtils.java","lineNumber":45,"sourceCode":"import androidx.annotation.RequiresPermission;\n\nimport static android.Manifest.permission.ACCESS_WIFI_STATE;\nimport static android.Manifest.permission.CHANGE_WIFI_STATE;\nimport static android.Manifest.permission.INTERNET;\nimport static android.content.Context.WIFI_SERVICE;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2016/8/1\n *     desc  : utils about device\n * </pre>\n */\npublic final class DeviceUtils {\n\n    private DeviceUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Return whether device is rooted.\n     *\n     * @return {@code true}: yes<br>{@code false}: no\n     */\n    public static boolean isDeviceRooted() {\n        String su = \"su\";\n        String[] locations = {\"/system/bin/\", \"/system/xbin/\", \"/sbin/\", \"/system/sd/xbin/\",\n                \"/system/bin/failsafe/\", \"/data/local/xbin/\", \"/data/local/bin/\", \"/data/local/\",\n                \"/system/sbin/\", \"/usr/bin/\", \"/vendor/bin/\"};\n        for (String location : locations) {\n            if (new File(location + su).exists()) {\n                return true;\n            }\n        }\n        return false;","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/DeviceUtils.java#L27-L63","documentation":"DeviceUtils is a final utility class whose private constructor throws UnsupportedOperationException(\"u can't instantiate me...\"). All device queries (rooted status, SDK version, device model, etc.) are static, so no instance is needed; the constructor only enforces the no-instantiation contract shared across the library's *Utils classes.","triggerScenarios":"Reflective instantiation via getDeclaredConstructor().newInstance() (with setAccessible), or passing the Class to a reflective factory/coverage tool. A normal `new DeviceUtils()` is a compile error because the constructor is private.","commonSituations":"Coverage tooling that instantiates every class; DI containers scanning packages; a developer who assumes instance methods; reflective test harnesses that exercise all constructors.","solutions":["Call the static API directly: DeviceUtils.isDeviceRooted(), DeviceUtils.getSDKVersion(), etc.","Exclude final utility classes from any reflective instantiation framework.","In a contract test, assert UnsupportedOperationException is thrown by the reflected constructor."],"exampleFix":"// before (reflection)\nDeviceUtils du = DeviceUtils.class.getDeclaredConstructor().newInstance();\n\n// after\nboolean rooted = DeviceUtils.isDeviceRooted();","handlingStrategy":"validation","validationCode":"// Never construct DeviceUtils; it is static-only.\nif (clazz == DeviceUtils.class || Modifier.isFinal(clazz.getModifiers())) {\n    // skip instantiation; use DeviceUtils.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 = DeviceUtils.class.getDeclaredConstructor();\n    c.setAccessible(true);\n    c.newInstance();\n} catch (UnsupportedOperationException e) {\n    // expected; use DeviceUtils.xxx() instead\n}","preventionTips":["DeviceUtils is static-only — never instantiate via `new` or reflection.","Exclude final 'Utils' classes from coverage/DI/mutation tools that construct everything.","Call DeviceUtils.isDeviceRooted()/getSDKVersion() statically."],"tags":["utility-class","instantiation","design-pattern","reflection","device"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}