{"record":{"id":"7b96e50c7d03c42d","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-7b96e5","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/KeyboardUtils.java","lineNumber":37,"sourceCode":"import android.widget.FrameLayout;\nimport androidx.annotation.NonNull;\nimport androidx.annotation.Nullable;\nimport java.lang.reflect.Field;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2016/08/02\n *     desc  : utils about keyboard\n * </pre>\n */\npublic final class KeyboardUtils {\n\n    private static final int TAG_ON_GLOBAL_LAYOUT_LISTENER = -8;\n\n    private KeyboardUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Show the soft input.\n     */\n    public static void showSoftInput() {\n        InputMethodManager imm =\n            (InputMethodManager) Utils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE);\n        if (imm == null) {\n            return;\n        }\n        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);\n    }\n\n    /**\n     * Show the soft input.\n     */\n    public static void showSoftInput(@Nullable Activity activity) {","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/KeyboardUtils.java#L19-L55","documentation":"KeyboardUtils is a final utility class providing static methods for soft-input keyboard control (showSoftInput, hideSoftInput, etc.). Its private constructor throws UnsupportedOperationException to prevent instantiation, enforcing static-only access.","triggerScenarios":"Direct instantiation via reflection, or frameworks (DI containers, mocking libraries, serialization) that attempt to construct KeyboardUtils by calling its constructor through reflection.","commonSituations":"Mockito or PowerMock tries to create an instance for static-method mocking; a DI framework scans the utility package and attempts construction; a test runner instantiates all classes in a package.","solutions":["Call static methods directly: KeyboardUtils.showSoftInput(), KeyboardUtils.hideSoftInput(view). Never instantiate.","Exclude utility classes from DI component scanning or serialization configuration.","For testing, use mockito-inline or PowerMock to mock static methods without instantiation.","If reflection is the cause, verify the target class is not a utility before calling newInstance()."],"exampleFix":"// before\nKeyboardUtils kb = new KeyboardUtils();\nkb.showSoftInput();\n\n// after\nKeyboardUtils.showSoftInput();","handlingStrategy":"type-guard","validationCode":"// Only call static methods; verify before reflective instantiation\nif (target != KeyboardUtils.class) {\n    target.getDeclaredConstructor().newInstance();\n}","typeGuard":"static boolean isUtilityClass(Class<?> clazz) {\n    return Modifier.isFinal(clazz.getModifiers())\n        && clazz.getName().endsWith(\"Utils\");\n}","tryCatchPattern":"try {\n    KeyboardUtils.class.getDeclaredConstructor().setAccessible(true).newInstance();\n} catch (UnsupportedOperationException e) {\n    // Utility class — switch to static method calls\n}","preventionTips":["Never instantiate classes ending in 'Utils' from this library.","Configure DI frameworks to exclude the util package.","Use mockito-inline for static-method mocking in tests instead of instantiation.","Add ProGuard rules that don't keep utility classes for instantiation."],"tags":["java","android","utility-class","instantiation","keyboard"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}