{"record":{"id":"6f9415812e93152d","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-6f9415","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/ServiceUtils.java","lineNumber":27,"sourceCode":"\nimport java.util.HashSet;\nimport java.util.List;\nimport java.util.Set;\n\nimport androidx.annotation.NonNull;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2016/08/02\n *     desc  : utils about service\n * </pre>\n */\npublic final class ServiceUtils {\n\n    private ServiceUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Return all of the services are running.\n     *\n     * @return all of the services are running\n     */\n    public static Set<String> getAllRunningServices() {\n        ActivityManager am = (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);\n        List<RunningServiceInfo> info = am.getRunningServices(0x7FFFFFFF);\n        Set<String> names = new HashSet<>();\n        if (info == null || info.size() == 0) return null;\n        for (RunningServiceInfo aInfo : info) {\n            names.add(aInfo.service.getClassName());\n        }\n        return names;\n    }\n","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/ServiceUtils.java#L9-L45","documentation":"ServiceUtils is a final static-only utility class for querying running services. Its private constructor throws UnsupportedOperationException to make accidental instantiation fail loudly. Because every method is static, the constructor is never meant to run; the guard converts a silent misuse into an immediate, diagnosable exception.","triggerScenarios":"Triggered by reflective instantiation: ServiceUtils.class.getDeclaredConstructor().setAccessible(true).newInstance(), or transitively through Gson/Jackson deserialization, Mockito/PowerMock mocking attempts, Kotlin reflection, or a DI/container classpath scan that treats the class as instantiable.","commonSituations":"Test suites that mock or parameterize over utility classes; JSON mappers binding a field to ServiceUtils; CI coverage tooling that reflectively probes classes; accidental instantiation after a refactor weakened access modifiers.","solutions":["Call the static API directly: ServiceUtils.getAllRunningServices() instead of `new ServiceUtils()`.","Register a no-op InstanceCreator/TypeAdapter (Gson) or mixin (Jackson) so the deserializer never invokes the constructor.","For mocking, use Mockito.mockStatic on the specific static method rather than instantiating the class.","Keep the class marked final and the constructor private so IDE tooling does not suggest constructing it."],"exampleFix":"// before\nGson gson = new Gson();\nServiceUtils su = gson.fromJson(json, ServiceUtils.class);\n\n// after\nSet<String> running = ServiceUtils.getAllRunningServices();","handlingStrategy":"validation","validationCode":"if (Modifier.isFinal(ServiceUtils.class.getModifiers())) {\n  throw new IllegalStateException(\"ServiceUtils is static-only; call methods directly.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use ServiceUtils static methods directly; never construct.","For JSON, register a no-op InstanceCreator for utility-typed fields.","Mock static methods (Mockito 3.4+) instead of instantiating.","Keep final + private constructor intact in any fork."],"tags":["android","utility-class","reflection","instantiation","service"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}