spring-projects/spring-framework · error · IllegalArgumentException

Illegal position {} in advisor list with size {}

Error message

Illegal position {} in advisor list with size {}

What it means

addAdvisorInternal() (line 408) validates that the requested insertion position does not exceed the current advisor list size. Because List.add(pos, x) only allows 0..size, a position beyond size is rejected with the current size reported.

Source

Thrown at spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java:409

			adviceChanged();
		}
	}

	private void validateIntroductionAdvisor(IntroductionAdvisor advisor) {
		advisor.validateInterfaces();
		// If the advisor passed validation, we can make the change.
		for (Class<?> ifc : advisor.getInterfaces()) {
			addInterface(ifc);
		}
	}

	private void addAdvisorInternal(int pos, Advisor advisor) throws AopConfigException {
		Assert.notNull(advisor, "Advisor must not be null");
		if (isFrozen()) {
			throw new AopConfigException("Cannot add advisor: Configuration is frozen.");
		}
		if (pos > this.advisors.size()) {
			throw new IllegalArgumentException(
					"Illegal position " + pos + " in advisor list with size " + this.advisors.size());
		}
		this.advisors.add(pos, advisor);
		adviceChanged();
	}

	/**
	 * Allows uncontrolled access to the {@link List} of {@link Advisor Advisors}.
	 * <p>Use with care, and remember to {@link #adviceChanged() fire advice changed events}
	 * when making any modifications.
	 */
	protected final List<Advisor> getAdvisorsInternal() {
		return this.advisors;
	}

	@Override
	public void addAdvice(Advice advice) throws AopConfigException {
		int pos = this.advisors.size();

View on GitHub (pinned to 69bf83ad71)

Solutions

  1. To append, call addAdvisor(advisor) (no index) which always uses the correct size
  2. If you must insert at a position, clamp/validate: int pos = Math.min(requested, advised.getAdvisorCount());
  3. Recompute the index from getAdvisorCount() immediately before the call

Example fix

// before
advised.addAdvisor(advised.getAdvisorCount() + 1, advisor);

// after
advised.addAdvisor(advisor);  // append at end safely
Defensive patterns

Strategy: validation

Validate before calling

int size = advised.getAdvisorCount();
if (pos < 0 || pos > size) {
  throw new IndexOutOfBoundsException("Illegal position " + pos + " (size " + size + ")");
}
advised.addAdvisor(pos, advisor);

Type guard

private static boolean validInsertPosition(AdvisedSupport a, int pos) {
  return a != null && pos >= 0 && pos <= a.getAdvisorCount();
}

Prevention

When it happens

Trigger: Calling addAdvisor(pos, advisor) where pos > advisors.size(); computing a position from an external/stale count and passing an out-of-range value.

Common situations: Off-by-one when inserting at the end (using size+1 instead of size); using an index derived from a different snapshot of the advisor list; appending then inserting at a position computed before the append.

Related errors


AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09). Data as JSON: /api/errors/06a95212448ca367. Report an issue: GitHub.