Blankj/AndroidUtilCode · error · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

PathUtils is a final utility class providing static methods for Android filesystem path operations (join, getRootPath, getDataPath, getExternalStoragePath, etc.). Its private constructor throws UnsupportedOperationException to enforce static-only access.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/PathUtils.java:22

import android.os.Environment;
import android.text.TextUtils;

import java.io.File;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2018/04/15
 *     desc  : utils about path
 * </pre>
 */
public final class PathUtils {

    private static final char SEP = File.separatorChar;

    private PathUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * Join the path.
     *
     * @param parent The parent of path.
     * @param child  The child path.
     * @return the path
     */
    public static String join(String parent, String child) {
        if (TextUtils.isEmpty(child)) return parent;
        if (parent == null) {
            parent = "";
        }
        int len = parent.length();
        String legalSegment = getLegalSegment(child);
        String newPath;
        if (len == 0) {

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Use static methods: PathUtils.join(parent, child), PathUtils.getRootPath(). Never call new PathUtils().
  2. Exclude utility classes from DI scanning and serialization configs.
  3. Mock static methods with mockito-inline for testing.

Example fix

// before
PathUtils pu = new PathUtils();
String path = pu.join("/data", "files");

// after
String path = PathUtils.join("/data", "files");
Defensive patterns

Strategy: type-guard

Validate before calling

// PathUtils is static-only
String path = PathUtils.join("/data", "files");
String root = PathUtils.getRootPath();

Type guard

static boolean isStaticUtility(Class<?> c) {
    return Modifier.isFinal(c.getModifiers()) && c.getSimpleName().endsWith("Utils");
}

Try / catch

try {
    PathUtils.class.getDeclaredConstructor().setAccessible(true);
    PathUtils.class.getDeclaredConstructor().newInstance();
} catch (UnsupportedOperationException e) {
    // Use static methods
}

Prevention

When it happens

Trigger: Direct or reflective instantiation by DI frameworks, mocking libraries, serialization frameworks, or manual reflection.

Common situations: DI component scanning traverses the utility package; a test framework instantiates all classes for coverage; a serialization library targets the class.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/06c63c4a09ffd9ca. Report an issue: GitHub.