{"record":{"id":"3aa355dbaca4c952","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-3aa355","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/FragmentUtils.java","lineNumber":49,"sourceCode":" * </pre>\n */\npublic final class FragmentUtils {\n\n    private static final int TYPE_ADD_FRAGMENT       = 0x01;\n    private static final int TYPE_SHOW_FRAGMENT      = 0x01 << 1;\n    private static final int TYPE_HIDE_FRAGMENT      = 0x01 << 2;\n    private static final int TYPE_SHOW_HIDE_FRAGMENT = 0x01 << 3;\n    private static final int TYPE_REPLACE_FRAGMENT   = 0x01 << 4;\n    private static final int TYPE_REMOVE_FRAGMENT    = 0x01 << 5;\n    private static final int TYPE_REMOVE_TO_FRAGMENT = 0x01 << 6;\n\n    private static final String ARGS_ID           = \"args_id\";\n    private static final String ARGS_IS_HIDE      = \"args_is_hide\";\n    private static final String ARGS_IS_ADD_STACK = \"args_is_add_stack\";\n    private static final String ARGS_TAG          = \"args_tag\";\n\n    private FragmentUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Add fragment.\n     *\n     * @param fm          The manager of fragment.\n     * @param add         The fragment will be add.\n     * @param containerId The id of container.\n     */\n    public static void add(@NonNull final FragmentManager fm,\n                           @NonNull final Fragment add,\n                           @IdRes final int containerId) {\n        add(fm, add, containerId, null, false, false);\n    }\n\n    /**\n     * Add fragment.\n     *","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/FragmentUtils.java#L31-L67","documentation":"FragmentUtils is a final utility class whose private constructor throws UnsupportedOperationException(\"u can't instantiate me...\"). All fragment operations (add, show, hide, replace, remove, pop) are static and driven by integer TYPE_* flags plus static ARGS_* bundles; no instance is needed and the constructor only blocks instantiation, per the library-wide *Utils convention.","triggerScenarios":"Reflective instantiation via getDeclaredConstructor().newInstance() with setAccessible, or passing the Class to a reflective factory/coverage tool. A normal `new FragmentUtils()` is a compile error because the constructor is private.","commonSituations":"Coverage/quality tools that instantiate every class; DI or reflective object mappers; a developer who expects instance methods; service loaders that scan packages.","solutions":["Call the static API directly: FragmentUtils.add(fm, fragment, containerId), FragmentUtils.replace(...), etc.","Add FragmentUtils to the exclusion list of any tool that reflectively instantiates classes.","For a contract test, expect UnsupportedOperationException from the reflected constructor."],"exampleFix":"// before (reflection)\nFragmentUtils fu = FragmentUtils.class.getDeclaredConstructor().newInstance();\n\n// after\nFragmentUtils.add(getSupportFragmentManager(), frag, R.id.container);","handlingStrategy":"validation","validationCode":"// Never construct FragmentUtils; it is static-only.\nif (clazz == FragmentUtils.class || Modifier.isFinal(clazz.getModifiers())) {\n    // skip instantiation; use FragmentUtils.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 = FragmentUtils.class.getDeclaredConstructor();\n    c.setAccessible(true);\n    c.newInstance();\n} catch (UnsupportedOperationException e) {\n    // expected; use FragmentUtils.xxx() instead\n}","preventionTips":["FragmentUtils is static-only — never instantiate via `new` or reflection.","Exclude final 'Utils' classes from coverage/DI/mutation tools that construct everything.","Call FragmentUtils.add()/replace()/remove()/pop() statically."],"tags":["utility-class","instantiation","design-pattern","reflection","fragment"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}