{"id":"59d509c986ce8a02","repo":"spring-projects/spring-framework","slug":"number-of-constructorargs-constructorargs-leng","errorCode":null,"errorMessage":"Number of 'constructorArgs' ({constructorArgs.length}) must match number of 'constructorArgTypes' ({constructorArgTypes.length})","messagePattern":"Number of 'constructorArgs' \\((.+?)\\) must match number of 'constructorArgTypes' \\((.+?)\\)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java","lineNumber":151,"sourceCode":"\t * exception in this case, rather than let a mysterious failure happen later.\n\t */\n\tpublic CglibAopProxy(AdvisedSupport config) throws AopConfigException {\n\t\tAssert.notNull(config, \"AdvisedSupport must not be null\");\n\t\tthis.advised = config;\n\t\tthis.advisedDispatcher = new AdvisedDispatcher(this.advised);\n\t}\n\n\t/**\n\t * Set constructor arguments to use for creating the proxy.\n\t * @param constructorArgs the constructor argument values\n\t * @param constructorArgTypes the constructor argument types\n\t */\n\tpublic void setConstructorArguments(Object @Nullable [] constructorArgs, Class<?> @Nullable [] constructorArgTypes) {\n\t\tif (constructorArgs == null || constructorArgTypes == null) {\n\t\t\tthrow new IllegalArgumentException(\"Both 'constructorArgs' and 'constructorArgTypes' need to be specified\");\n\t\t}\n\t\tif (constructorArgs.length != constructorArgTypes.length) {\n\t\t\tthrow new IllegalArgumentException(\"Number of 'constructorArgs' (\" + constructorArgs.length +\n\t\t\t\t\t\") must match number of 'constructorArgTypes' (\" + constructorArgTypes.length + \")\");\n\t\t}\n\t\tthis.constructorArgs = constructorArgs;\n\t\tthis.constructorArgTypes = constructorArgTypes;\n\t}\n\n\n\t@Override\n\tpublic Object getProxy() {\n\t\treturn buildProxy(null, false);\n\t}\n\n\t@Override\n\tpublic Object getProxy(@Nullable ClassLoader classLoader) {\n\t\treturn buildProxy(classLoader, false);\n\t}\n\n\t@Override","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java#L133-L169","documentation":"Thrown by CglibAopProxy.setConstructorArguments when constructorArgs.length != constructorArgTypes.length. CGLIB resolves the constructor by matching the type array against declared constructors and then passes the value array positionally, so the two arrays must be equal in length. A length mismatch indicates a wiring bug and would otherwise surface as a confusing CGLIB failure.","triggerScenarios":"Calling setConstructorArguments with arrays of different lengths; mutating one array after the other was built; off-by-one in loops that build the two arrays separately.","commonSituations":"Builder/helper code that assembles args and types from separate collections; copy-paste where values were updated but types were not; varargs confusion.","solutions":["Build both arrays from a single paired list (e.g. List<Pair<Class<?>,Object>>) so they cannot diverge.","Add an assertion args.length == types.length before calling setConstructorArguments.","Log both lengths when the error appears to quickly locate the divergent source."],"exampleFix":"// before\ncglibProxy.setConstructorArguments(\n  new Object[]{1, \"a\", true},          // 3\n  new Class<?>[]{int.class, String.class}); // 2 -> throws\n\n// after\ncglibProxy.setConstructorArguments(\n  new Object[]{1, \"a\", true},\n  new Class<?>[]{int.class, String.class, boolean.class});","handlingStrategy":"validation","validationCode":"if (constructorArgs.length != constructorArgTypes.length) {\n  throw new IllegalArgumentException(\"args.length=\" + constructorArgs.length\n    + \" types.length=\" + constructorArgTypes.length);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Build args and types from one List<Pair<Class<?>,Object>> so lengths stay aligned.","Add an equality assertion before calling setConstructorArguments.","Avoid varargs mixing for the two arrays."],"tags":["spring-aop","cglib","constructor","validation"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}