{"record":{"id":"442510182c99f074","repo":"Blankj/AndroidUtilCode","slug":"u-can-t-instantiate-me-442510","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/FileUtils.java","lineNumber":42,"sourceCode":"import java.util.Comparator;\nimport java.util.List;\n\nimport javax.net.ssl.HttpsURLConnection;\n\n/**\n * <pre>\n *     author: Blankj\n *     blog  : http://blankj.com\n *     time  : 2016/05/03\n *     desc  : utils about file\n * </pre>\n */\npublic final class FileUtils {\n\n    private static final String LINE_SEP = System.getProperty(\"line.separator\");\n\n    private FileUtils() {\n        throw new UnsupportedOperationException(\"u can't instantiate me...\");\n    }\n\n    /**\n     * Return the file by path.\n     *\n     * @param filePath The path of file.\n     * @return the file\n     */\n    public static File getFileByPath(final String filePath) {\n        return UtilsBridge.isSpace(filePath) ? null : new File(filePath);\n    }\n\n    /**\n     * Return whether the file exists.\n     *\n     * @param file The file.\n     * @return {@code true}: yes<br>{@code false}: no\n     */","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/FileUtils.java#L24-L60","documentation":"FileUtils is a final utility class whose private constructor throws UnsupportedOperationException(\"u can't instantiate me...\"). All file operations (getFileByPath, createOrExistsFile, copy, delete, listFilesInDir, etc.) are static; the constructor exists only to prevent instantiation, consistent with the library-wide *Utils pattern.","triggerScenarios":"Reflective instantiation via getDeclaredConstructor().newInstance() with setAccessible, or passing the Class to a reflective factory/coverage tool. A normal `new FileUtils()` 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 state; service loaders that scan packages.","solutions":["Call the static API directly: FileUtils.getFileByPath(...), FileUtils.createOrExistsFile(...), etc.","Add FileUtils to the exclusion list of any tool that reflectively instantiates classes.","For a contract test, expect UnsupportedOperationException from the reflected constructor."],"exampleFix":"// before (reflection)\nFileUtils fu = FileUtils.class.getDeclaredConstructor().newInstance();\n\n// after\nFile f = FileUtils.getFileByPath(\"/sdcard/test.txt\");","handlingStrategy":"validation","validationCode":"// Never construct FileUtils; it is static-only.\nif (clazz == FileUtils.class || Modifier.isFinal(clazz.getModifiers())) {\n    // skip instantiation; use FileUtils.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 = FileUtils.class.getDeclaredConstructor();\n    c.setAccessible(true);\n    c.newInstance();\n} catch (UnsupportedOperationException e) {\n    // expected; use FileUtils.xxx() instead\n}","preventionTips":["FileUtils is static-only — never instantiate via `new` or reflection.","Exclude final 'Utils' classes from coverage/DI/mutation tools that construct everything.","Call FileUtils.getFileByPath()/createOrExistsFile()/copy()/delete() statically."],"tags":["utility-class","instantiation","design-pattern","reflection","file"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}