jeecgboot/JeecgBoot · error · IllegalArgumentException

编码长度必须能被固定位数整除

Error message

编码长度必须能被固定位数整除

What it means

Thrown by SysDepartServiceImpl.getCodeHierarchy when code.length() % fixedLength != 0. This is a standard IllegalArgumentException (unchecked) used to enforce that department org-codes follow a fixed-length segment convention (e.g., 'A01A01A01' with fixedLength=3). The method builds a hierarchy of parent codes by splitting at fixedLength boundaries, so a non-divisible length would produce malformed partial codes.

Source

Thrown at jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysDepartServiceImpl.java:1943

        }
        return "";
    }
    
    /**
     * 获取编码及其所有上级编码
     * 
     * @param code 完整编码,如 "A01A01A01"
     * @param fixedLength 固定位数,如 3
     * @return 包含所有上级编码的列表,如 ['A01','A01A01','A01A01A01']
     */
    public List<String> getCodeHierarchy(String code, int fixedLength) {
        List<String> hierarchy = new ArrayList<>();
        if (code == null || code.isEmpty() || fixedLength <= 0) {
            return hierarchy;
        }
        // 检查编码长度是否能被固定位数整除
        if (code.length() % fixedLength != 0) {
            throw new IllegalArgumentException("编码长度必须能被固定位数整除");
        }
        // 按固定位数分割并生成所有上级编码
        for (int i = fixedLength; i <= code.length(); i += fixedLength) {
            hierarchy.add(code.substring(0, i));
        }
        return hierarchy;
    }

    /**
     * 根据多个部门id删除主岗位和兼职岗位
     * 
     * @param idList
     */
    private void deleteDepartPostByDepIds(List<String> idList) {
        //更新用户主岗位位空,使用LambdaUpdateWrapper,避免为空时受全局 updateStrategy 影响导致误更新
        LambdaUpdateWrapper<SysUser> userQuery = new LambdaUpdateWrapper<>();
        userQuery.in(SysUser::getMainDepPostId, idList);
        userQuery.set(SysUser::getMainDepPostId, null);

View on GitHub (pinned to 96fb33f5ec)

Solutions

  1. Confirm the org-code format matches the expected fixedLength segment convention (e.g., each level is exactly N characters).
  2. Trim and validate the code before calling: ensure code.length() % fixedLength == 0.
  3. If mixed code formats are expected, split the method into separate code paths rather than forcing one fixedLength.
  4. Check the department code-generation configuration to verify segment length matches the value passed as fixedLength.

Example fix

// before
List<String> h = service.getCodeHierarchy("A01B", 3);
// throws IllegalArgumentException: 编码长度必须能被固定位数整除

// after — validate before calling
String code = "A01B".trim();
int fixedLength = 3;
if (code.isEmpty() || code.length() % fixedLength != 0) {
    log.warn("Invalid org code format: {} for fixedLength {}", code, fixedLength);
    return Collections.emptyList();
}
List<String> h = service.getCodeHierarchy(code, fixedLength);
Defensive patterns

Strategy: validation

Validate before calling

public boolean isValidOrgCodeFormat(String code, int fixedLength) {
    if (code == null || code.isEmpty() || fixedLength <= 0) return false;
    return code.length() % fixedLength == 0;
}

// Before calling:
if (!isValidOrgCodeFormat(code, fixedLength)) {
    log.warn("Invalid org code format: {} for fixedLength {}", code, fixedLength);
    return Collections.emptyList();
}

Type guard

public boolean isCodeDivisible(String code, int fixedLength) {
    return code != null && !code.isEmpty() && fixedLength > 0
        && code.length() % fixedLength == 0;
}

Try / catch

try {
    List<String> hierarchy = service.getCodeHierarchy(code, fixedLength);
} catch (IllegalArgumentException e) {
    log.warn("Org code hierarchy failed: code={}, fixedLength={}", code, fixedLength);
    return Collections.emptyList();
}

Prevention

When it happens

Trigger: Passing an org-code whose length is not a multiple of the expected segment size — for example code='A01B' with fixedLength=3, or a code with trailing/leading whitespace that throws off the length, or a fixedLength of 0 (though that's guarded earlier and returns empty). Also triggered when mixing different department coding schemes.

Common situations: Department org-code format mismatch after a data migration or schema change; a manually-entered org-code that doesn't follow the A01A01A01 pattern; calling getCodeHierarchy with a fixedLength derived from config that doesn't match the actual code generation scheme.

Related errors


AI-assisted analysis of jeecgboot/JeecgBoot@96fb33f5ec (2026-08-14). Data as JSON: /api/errors/9149827b86bd4c38. Report an issue: GitHub.