{"record":{"id":"2efce24f5e6c8e46","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me","errorCode":null,"errorMessage":"u can't instantiate me...","messagePattern":"u can't instantiate me\\.\\.\\.","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"warning","filePath":"lib/subutil/src/main/java/com/blankj/subutil/util/BitUtils.java","lineNumber":16,"sourceCode":"package com.blankj.subutil.util;\n\nimport android.util.Log;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2018/03/21\n *     desc  : 位运算工具类\n * </pre>\n */\npublic final class BitUtils {\n\n    private BitUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * 获取运算数指定位置的值<br>\n     * 例如： 0000 1011 获取其第 0 位的值为 1, 第 2 位 的值为 0<br>\n     *\n     * @param source 需要运算的数\n     * @param pos    指定位置 (0...7)\n     * @return 指定位置的值(0 or 1)\n     */\n    public static byte getBitValue(byte source, int pos) {\n        return (byte) ((source >> pos) & 1);\n\n    }\n\n\n    /**\n     * 将运算数指定位置的值置为指定值<br>","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/subutil/src/main/java/com/blankj/subutil/util/BitUtils.java#L1-L34","documentation":"BitUtils is a final utility class with a private constructor that throws UnsupportedOperationException. The class is designed for static access only; the throw is the guard against reflection-based instantiation.","triggerScenarios":"Instantiating via reflection (Constructor.newInstance) or an unsafe allocator, since the private constructor prevents normal `new BitUtils()` at compile time.","commonSituations":"Reflection/serialization/DI frameworks that try to instantiate any class; tooling that scans and constructs classes; accidental test helpers that reflectively build utility classes.","solutions":["Do not instantiate BitUtils; call its static methods directly, e.g. BitUtils.getBitValue(source, pos).","If a framework forces instantiation, exclude BitUtils from its scanning or configure it to use static-only access.","Remove any reflection-based newInstance call targeting utility classes."],"exampleFix":"// before\nBitUtils b = BitUtils.class.getDeclaredConstructor().newInstance(); // throws\n\n// after\nbyte v = BitUtils.getBitValue(source, 2);","handlingStrategy":"validation","validationCode":"// BitUtils is static-only; never instantiate.\nbyte bit = BitUtils.getBitValue(source, 2);","typeGuard":"private static boolean isStaticUtility(Class<?> c) {\n    return Modifier.isFinal(c.getModifiers()) && hasOnlyStaticMethods(c);\n}","tryCatchPattern":null,"preventionTips":["Use BitUtils via its static methods only.","Exclude utility classes from reflective instantiation/DI scanning.","Do not write tests that reflectively build utility classes."],"tags":["android","utility","instantiation","reflection"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}