Fix MethodBasedEvaluationContext.lazyLoadArguments
This commit fix a potential `ArrayIndexOutOfBoundsException` if `lazyLoadArguments` is called with an empty variable argument. See gh-1070
This commit is contained in:
parent
dd65689e0a
commit
f075aac7f9
|
@ -89,7 +89,7 @@ public class MethodBasedEvaluationContext extends StandardEvaluationContext {
|
|||
String[] parameterNames = this.paramDiscoverer.getParameterNames(this.method);
|
||||
// save parameter names (if discovered)
|
||||
if (parameterNames != null) {
|
||||
for (int i = 0; i < parameterNames.length; i++) {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
setVariable(parameterNames[i], this.args[i]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,6 +62,42 @@ public class MethodBasedEvaluationContextTests {
|
|||
assertNull(context.lookupVariable("p0"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void varArgEmpty() {
|
||||
Method method = ReflectionUtils.findMethod(SampleMethods.class, "hello", Boolean.class, String[].class);
|
||||
MethodBasedEvaluationContext context = createEvaluationContext(method, new Object[] {null});
|
||||
|
||||
assertNull(context.lookupVariable("p0"));
|
||||
assertNull(context.lookupVariable("p1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void varArgNull() {
|
||||
Method method = ReflectionUtils.findMethod(SampleMethods.class, "hello", Boolean.class, String[].class);
|
||||
MethodBasedEvaluationContext context = createEvaluationContext(method, new Object[] {null, null});
|
||||
|
||||
assertNull(context.lookupVariable("p0"));
|
||||
assertNull(context.lookupVariable("p1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void varArgSingle() {
|
||||
Method method = ReflectionUtils.findMethod(SampleMethods.class, "hello", Boolean.class, String[].class);
|
||||
MethodBasedEvaluationContext context = createEvaluationContext(method, new Object[] {null, "hello"});
|
||||
|
||||
assertNull(context.lookupVariable("p0"));
|
||||
assertEquals("hello", context.lookupVariable("p1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void varArgMultiple() {
|
||||
Method method = ReflectionUtils.findMethod(SampleMethods.class, "hello", Boolean.class, String[].class);
|
||||
MethodBasedEvaluationContext context = createEvaluationContext(method, new Object[] {null, new String[]{"hello", "hi"}});
|
||||
|
||||
assertNull(context.lookupVariable("p0"));
|
||||
assertNotNull(context.lookupVariable("p1"));
|
||||
}
|
||||
|
||||
private MethodBasedEvaluationContext createEvaluationContext(Method method, Object[] args) {
|
||||
return new MethodBasedEvaluationContext(this, method, args, this.paramDiscover);
|
||||
}
|
||||
|
@ -73,6 +109,9 @@ public class MethodBasedEvaluationContextTests {
|
|||
private void hello(String foo, Boolean flag) {
|
||||
}
|
||||
|
||||
private void hello(Boolean flag, String ... vararg){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue