{"id":"491a0d893ded84fa","repo":"spring-projects/spring-framework","slug":"illegal-position-pos-in-advisor-list-with-size","errorCode":null,"errorMessage":"Illegal position {pos} in advisor list with size {advisors.size()}","messagePattern":"Illegal position (.+?) in advisor list with size (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java","lineNumber":409,"sourceCode":"\t\t\tadviceChanged();\n\t\t}\n\t}\n\n\tprivate void validateIntroductionAdvisor(IntroductionAdvisor advisor) {\n\t\tadvisor.validateInterfaces();\n\t\t// If the advisor passed validation, we can make the change.\n\t\tfor (Class<?> ifc : advisor.getInterfaces()) {\n\t\t\taddInterface(ifc);\n\t\t}\n\t}\n\n\tprivate void addAdvisorInternal(int pos, Advisor advisor) throws AopConfigException {\n\t\tAssert.notNull(advisor, \"Advisor must not be null\");\n\t\tif (isFrozen()) {\n\t\t\tthrow new AopConfigException(\"Cannot add advisor: Configuration is frozen.\");\n\t\t}\n\t\tif (pos > this.advisors.size()) {\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\"Illegal position \" + pos + \" in advisor list with size \" + this.advisors.size());\n\t\t}\n\t\tthis.advisors.add(pos, advisor);\n\t\tadviceChanged();\n\t}\n\n\t/**\n\t * Allows uncontrolled access to the {@link List} of {@link Advisor Advisors}.\n\t * <p>Use with care, and remember to {@link #adviceChanged() fire advice changed events}\n\t * when making any modifications.\n\t */\n\tprotected final List<Advisor> getAdvisorsInternal() {\n\t\treturn this.advisors;\n\t}\n\n\t@Override\n\tpublic void addAdvice(Advice advice) throws AopConfigException {\n\t\tint pos = this.advisors.size();","sourceCodeStart":391,"sourceCodeEnd":427,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java#L391-L427","documentation":"Thrown by AdvisedSupport.addAdvisorInternal when pos > advisors.size(), i.e. the requested insertion position would leave a gap in the list. Spring uses ArrayList.add(pos, x) which itself would throw IndexOutOfBoundsException; this guard surfaces a clearer message first. Position == size is allowed (append), position > size is not.","triggerScenarios":"Calling addAdvisor(pos, advisor) with pos equal to size+1 or larger; computing a position from a stale advisor count; off-by-one when inserting 'after the last' advisor (using size instead of size-1 is actually correct for append).","commonSituations":"Caching advisor count, then inserting after external code added/removed advisors; loop bookkeeping errors; passing indexOf()+1 when the advisor wasn't found (indexOf returns -1, +1 = 0 is fine, but indexOf of a missing advisor combined with other math can overshoot).","solutions":["Validate 0 <= pos <= advised.getAdvisorCount() before calling addAdvisor(pos, advisor).","To append, pass pos = advised.getAdvisors().length (== size), which is the permitted boundary.","Recompute the current size immediately before insertion rather than reusing a cached value."],"exampleFix":"// before\nint pos = advisors.size() + 1;  // overshoots\nadvised.addAdvisor(pos, newAdvisor);\n\n// after\nint pos = advised.getAdvisors().length;  // append is allowed\nadvised.addAdvisor(pos, newAdvisor);","handlingStrategy":"validation","validationCode":"int size = advised.getAdvisors().length;\nif (pos < 0 || pos > size) {\n  throw new IllegalArgumentException(\"pos \" + pos + \" invalid; size=\" + size);\n}\nadvised.addAdvisor(pos, advisor);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat size as the append boundary (allowed) and size+1 as invalid.","Compute the current size right before insertion; never cache it.","For 'insert last', prefer addAdvisor(advisor) which appends internally."],"tags":["spring-aop","bounds-check","advisor-mutation","validation"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}