chinabugotech/hutool · error · IllegalArgumentException

Invalid Getter or Setter name:

Error message

Invalid Getter or Setter name: 

What it means

BeanUtil.getFieldName derives the field name from a getter/setter method name by stripping get/set (3 chars) or is (2 chars) and lowercasing the next letter. If the supplied name matches none of those prefixes, it is not a JavaBean accessor and the method throws IllegalArgumentException. The message echoes the offending name.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/bean/BeanUtil.java:998

	 * <ul>
	 *     <li>getXxxx获取为xxxx,如getName得到name。</li>
	 *     <li>setXxxx获取为xxxx,如setName得到name。</li>
	 *     <li>isXxxx获取为xxxx,如isName得到name。</li>
	 *     <li>其它不满足规则的方法名抛出{@link IllegalArgumentException}</li>
	 * </ul>
	 *
	 * @param getterOrSetterName Getter或Setter方法名
	 * @return 字段名称
	 * @throws IllegalArgumentException 非Getter或Setter方法
	 * @since 5.7.23
	 */
	public static String getFieldName(String getterOrSetterName) {
		if (getterOrSetterName.startsWith("get") || getterOrSetterName.startsWith("set")) {
			return StrUtil.removePreAndLowerFirst(getterOrSetterName, 3);
		} else if (getterOrSetterName.startsWith("is")) {
			return StrUtil.removePreAndLowerFirst(getterOrSetterName, 2);
		} else {
			throw new IllegalArgumentException("Invalid Getter or Setter name: " + getterOrSetterName);
		}
	}

	/**
	 * 判断source与target的所有公共字段的值是否相同
	 *
	 * @param source           待检测对象1
	 * @param target           待检测对象2
	 * @param ignoreProperties 不需要检测的字段
	 * @return 判断结果,如果为true则证明所有字段的值都相同
	 * @author Takak11
	 * @since 5.8.4
	 */
	public static boolean isCommonFieldsEqual(Object source, Object target, String... ignoreProperties) {

		if (null == source && null == target) {
			return true;
		}

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Pre-filter: only call getFieldName on names starting with get/set/is (and longer than the prefix).
  2. Use BeanUtil.getBeanDesc(...) to obtain validated PropDesc entries instead of deriving names manually.
  3. Catch IllegalArgumentException for non-accessor methods when iterating.

Example fix

// before
String field = BeanUtil.getFieldName(method.getName()); // may throw
// after
String name = method.getName();
String field = (name.startsWith("get")||name.startsWith("set")||name.startsWith("is"))
    ? BeanUtil.getFieldName(name) : null;
Defensive patterns

Strategy: type-guard

Validate before calling

static boolean isAccessor(String n) { return (n.startsWith("get")||n.startsWith("set")) && n.length()>3 || (n.startsWith("is") && n.length()>2); }

Type guard

static boolean isAccessorName(String n) { return n != null && (n.startsWith("get")||n.startsWith("set")||n.startsWith("is")) && n.length() > (n.startsWith("is")?2:3); }

Prevention

When it happens

Trigger: Passing arbitrary method names like "hashCode", "toString", "value", "isActive" (works) vs "active" (fails), or names shorter than the prefix.

Common situations: Iterating over all methods of a class and calling getFieldName without filtering to accessors; receiving a method name from reflection that may not be a getter/setter.

Related errors


AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14). Data as JSON: /api/errors/d8260d34c791e2aa. Report an issue: GitHub.