Blankj/AndroidUtilCode · info · UnsupportedOperationException
u can't instantiate me...
Error message
u can't instantiate me...
What it means
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.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/FileUtils.java:42
import java.util.Comparator;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/05/03
* desc : utils about file
* </pre>
*/
public final class FileUtils {
private static final String LINE_SEP = System.getProperty("line.separator");
private FileUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Return the file by path.
*
* @param filePath The path of file.
* @return the file
*/
public static File getFileByPath(final String filePath) {
return UtilsBridge.isSpace(filePath) ? null : new File(filePath);
}
/**
* Return whether the file exists.
*
* @param file The file.
* @return {@code true}: yes<br>{@code false}: no
*/View on GitHub (pinned to 7b4caf9e54)
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.
Example fix
// before (reflection)
FileUtils fu = FileUtils.class.getDeclaredConstructor().newInstance();
// after
File f = FileUtils.getFileByPath("/sdcard/test.txt"); Defensive patterns
Strategy: validation
Validate before calling
// Never construct FileUtils; it is static-only.
if (clazz == FileUtils.class || Modifier.isFinal(clazz.getModifiers())) {
// skip instantiation; use FileUtils.xxx() statically
} Type guard
if (clazz.getName().endsWith("Utils") && Modifier.isFinal(clazz.getModifiers())) {
// static-only; do not newInstance()
} Try / catch
try {
Constructor<?> c = FileUtils.class.getDeclaredConstructor();
c.setAccessible(true);
c.newInstance();
} catch (UnsupportedOperationException e) {
// expected; use FileUtils.xxx() instead
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Coverage/quality tools that instantiate every class; DI or reflective object mappers; a developer who expects instance state; service loaders that scan packages.
Related errors
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
- u can't instantiate me...
AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14).
Data as JSON: /api/errors/442510182c99f074.
Report an issue: GitHub.