didi/DoKit · error · IllegalArgumentException

segment of <{}> is illegal

Error message

segment of <{}> is illegal

What it means

PathUtils.getFirstSegment(segment) scans the string for a run of non-separator characters (SEP, i.e. '/') and returns it via substring(st, end + 1). If the string consists entirely of separators (or is empty), st stays -1 / end < st and the method throws IllegalArgumentException('segment of <...> is illegal'). It expects at least one non-separator character.

Source

Thrown at Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/PathUtils.java:65

        return newPath;
    }

    private static String getLegalSegment(String segment) {
        int st = -1, end = -1;
        char[] charArray = segment.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            char c = charArray[i];
            if (c != SEP) {
                if (st == -1) {
                    st = i;
                }
                end = i;
            }
        }
        if (st >= 0 && end >= st) {
            return segment.substring(st, end + 1);
        }
        throw new IllegalArgumentException("segment of <" + segment + "> is illegal");
    }

    /**
     * Return the path of /system.
     *
     * @return the path of /system
     */
    public static String getRootPath() {
        return getAbsolutePath(Environment.getRootDirectory());
    }

    /**
     * Return the path of /data.
     *
     * @return the path of /data
     */
    public static String getDataPath() {
        return getAbsolutePath(Environment.getDataDirectory());

View on GitHub (pinned to 626827cddb)

Solutions

  1. Validate before calling: reject segments where segment == null || segment.replace("/", "").isEmpty().
  2. Normalize with PathUtils or java.io.File first and handle the root path ("/") as a special case instead of segmenting it.
  3. If splitting, filter empty tokens before calling getFirstSegment.

Example fix

// before
String seg = PathUtils.getFirstSegment("/"); // throws

// after
String path = "/";
String seg = path.replace("/", "").isEmpty() ? null : PathUtils.getFirstSegment(path);
Defensive patterns

Strategy: validation

Validate before calling

boolean legal = segment != null && !segment.replace(String.valueOf('/'), "").isEmpty();
String first = legal ? PathUtils.getFirstSegment(segment) : null;

Try / catch

try { seg = PathUtils.getFirstSegment(s); } catch (IllegalArgumentException e) { seg = null; }

Prevention

When it happens

Trigger: Calling with "", "/", "//", or a whitespace-free separator-only string; often the result of splitting a path like "/" on '/' producing empty pieces, or of Environment paths resolving unexpectedly.

Common situations: Parsing user-entered or intent-supplied paths; unit tests passing the empty string; processing root-relative paths whose first segment after trimming separators is empty.

Related errors


AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14). Data as JSON: /api/errors/4bb77fdcdf7301a0. Report an issue: GitHub.