{"record":{"id":"fde95906c71a2402","repo":"spring-projects/spring-framework","slug":"number-of-constructorargs-must-match-number","errorCode":null,"errorMessage":"Number of 'constructorArgs' ({}) must match number of 'constructorArgTypes' ({})","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/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java#L133-L169","documentation":"setConstructorArguments() (line 150) requires the values array and the types array to have the same length, because each arg must be paired with exactly one declared parameter type for constructor resolution. A length mismatch means the inputs cannot describe a single constructor invocation.","triggerScenarios":"Passing constructorArgs and constructorArgTypes of different lengths, e.g. 3 values but 2 types.","commonSituations":"Appending an argument but forgetting to add its type; copying arrays of unequal size; off-by-one when building the two arrays in a loop.","solutions":["Make the two arrays the same length, pairing every value with its type","Build them together: for each (value,type) pair push to both arrays in one loop","Add an assertion: if (args.length != types.length) throw before calling the API"],"exampleFix":"// before\nproxy.setConstructorArguments(new Object[]{1, \"x\"}, new Class[]{int.class});\n\n// after\nproxy.setConstructorArguments(new Object[]{1, \"x\"}, new Class[]{int.class, String.class});","handlingStrategy":"validation","validationCode":"if (constructorArgs.length != constructorArgTypes.length) {\n  throw new IllegalArgumentException(\"constructorArgs/constructorArgTypes length mismatch\");\n}","typeGuard":"private static boolean constructorArgsConsistent(Object[] args, Class<?>[] types) {\n  return args != null && types != null && args.length == types.length;\n}","tryCatchPattern":null,"preventionTips":["Build args and types in a single loop so they cannot diverge","Add an assert/Objects.checkIndex-style guard before calling the API"],"tags":["spring-aop","cglib","constructor","config"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}