Consistent handling of null array for arguments

Issue: SPR-16075
This commit is contained in:
Juergen Hoeller 2017-10-16 15:34:18 +02:00
parent 3890d4c9eb
commit c29b6f5b55
2 changed files with 22 additions and 12 deletions

View File

@ -51,7 +51,7 @@ public class MethodInvoker {
private String staticMethod; private String staticMethod;
@Nullable @Nullable
private Object[] arguments = new Object[0]; private Object[] arguments;
/** The method we will call */ /** The method we will call */
@Nullable @Nullable

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -49,18 +49,28 @@ public class MethodInvokerTests {
Integer i = (Integer) mi.invoke(); Integer i = (Integer) mi.invoke();
assertEquals(1, i.intValue()); assertEquals(1, i.intValue());
// defensive check: singleton, non-static should work with null array
tc1 = new TestClass1();
mi = new MethodInvoker();
mi.setTargetObject(tc1);
mi.setTargetMethod("method1");
mi.setArguments((Object[]) null);
mi.prepare();
i = (Integer) mi.invoke();
assertEquals(1, i.intValue());
// sanity check: check that argument count matching works // sanity check: check that argument count matching works
mi = new MethodInvoker(); mi = new MethodInvoker();
mi.setTargetClass(TestClass1.class); mi.setTargetClass(TestClass1.class);
mi.setTargetMethod("supertypes"); mi.setTargetMethod("supertypes");
mi.setArguments(new Object[] {new ArrayList<>(), new ArrayList<>(), "hello"}); mi.setArguments(new ArrayList<>(), new ArrayList<>(), "hello");
mi.prepare(); mi.prepare();
assertEquals("hello", mi.invoke()); assertEquals("hello", mi.invoke());
mi = new MethodInvoker(); mi = new MethodInvoker();
mi.setTargetClass(TestClass1.class); mi.setTargetClass(TestClass1.class);
mi.setTargetMethod("supertypes2"); mi.setTargetMethod("supertypes2");
mi.setArguments(new Object[] {new ArrayList<>(), new ArrayList<>(), "hello", "bogus"}); mi.setArguments(new ArrayList<>(), new ArrayList<>(), "hello", "bogus");
mi.prepare(); mi.prepare();
assertEquals("hello", mi.invoke()); assertEquals("hello", mi.invoke());
@ -68,7 +78,7 @@ public class MethodInvokerTests {
mi = new MethodInvoker(); mi = new MethodInvoker();
mi.setTargetClass(TestClass1.class); mi.setTargetClass(TestClass1.class);
mi.setTargetMethod("supertypes2"); mi.setTargetMethod("supertypes2");
mi.setArguments(new Object[] {new ArrayList<>(), new ArrayList<>(), "hello", Boolean.TRUE}); mi.setArguments(new ArrayList<>(), new ArrayList<>(), "hello", Boolean.TRUE);
exception.expect(NoSuchMethodException.class); exception.expect(NoSuchMethodException.class);
mi.prepare(); mi.prepare();
@ -79,7 +89,7 @@ public class MethodInvokerTests {
MethodInvoker methodInvoker = new MethodInvoker(); MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(new Greeter()); methodInvoker.setTargetObject(new Greeter());
methodInvoker.setTargetMethod("greet"); methodInvoker.setTargetMethod("greet");
methodInvoker.setArguments(new Object[] {"no match"}); methodInvoker.setArguments("no match");
exception.expect(NoSuchMethodException.class); exception.expect(NoSuchMethodException.class);
methodInvoker.prepare(); methodInvoker.prepare();
@ -90,7 +100,7 @@ public class MethodInvokerTests {
MethodInvoker methodInvoker = new MethodInvoker(); MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(new Greeter()); methodInvoker.setTargetObject(new Greeter());
methodInvoker.setTargetMethod("greet"); methodInvoker.setTargetMethod("greet");
methodInvoker.setArguments(new Object[] {new Purchaser()}); methodInvoker.setArguments(new Purchaser());
methodInvoker.prepare(); methodInvoker.prepare();
String greeting = (String) methodInvoker.invoke(); String greeting = (String) methodInvoker.invoke();
assertEquals("purchaser: hello", greeting); assertEquals("purchaser: hello", greeting);
@ -101,7 +111,7 @@ public class MethodInvokerTests {
MethodInvoker methodInvoker = new MethodInvoker(); MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(new Greeter()); methodInvoker.setTargetObject(new Greeter());
methodInvoker.setTargetMethod("greet"); methodInvoker.setTargetMethod("greet");
methodInvoker.setArguments(new Object[] {new Shopper()}); methodInvoker.setArguments(new Shopper());
methodInvoker.prepare(); methodInvoker.prepare();
String greeting = (String) methodInvoker.invoke(); String greeting = (String) methodInvoker.invoke();
assertEquals("purchaser: may I help you?", greeting); assertEquals("purchaser: may I help you?", greeting);
@ -112,7 +122,7 @@ public class MethodInvokerTests {
MethodInvoker methodInvoker = new MethodInvoker(); MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(new Greeter()); methodInvoker.setTargetObject(new Greeter());
methodInvoker.setTargetMethod("greet"); methodInvoker.setTargetMethod("greet");
methodInvoker.setArguments(new Object[] {new Salesman()}); methodInvoker.setArguments(new Salesman());
methodInvoker.prepare(); methodInvoker.prepare();
String greeting = (String) methodInvoker.invoke(); String greeting = (String) methodInvoker.invoke();
assertEquals("greetable: how are sales?", greeting); assertEquals("greetable: how are sales?", greeting);
@ -123,7 +133,7 @@ public class MethodInvokerTests {
MethodInvoker methodInvoker = new MethodInvoker(); MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(new Greeter()); methodInvoker.setTargetObject(new Greeter());
methodInvoker.setTargetMethod("greet"); methodInvoker.setTargetMethod("greet");
methodInvoker.setArguments(new Object[] {new Customer()}); methodInvoker.setArguments(new Customer());
methodInvoker.prepare(); methodInvoker.prepare();
String greeting = (String) methodInvoker.invoke(); String greeting = (String) methodInvoker.invoke();
assertEquals("customer: good day", greeting); assertEquals("customer: good day", greeting);
@ -134,7 +144,7 @@ public class MethodInvokerTests {
MethodInvoker methodInvoker = new MethodInvoker(); MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(new Greeter()); methodInvoker.setTargetObject(new Greeter());
methodInvoker.setTargetMethod("greet"); methodInvoker.setTargetMethod("greet");
methodInvoker.setArguments(new Object[] {new Regular("Kotter")}); methodInvoker.setArguments(new Regular("Kotter"));
methodInvoker.prepare(); methodInvoker.prepare();
String greeting = (String) methodInvoker.invoke(); String greeting = (String) methodInvoker.invoke();
assertEquals("regular: welcome back Kotter", greeting); assertEquals("regular: welcome back Kotter", greeting);
@ -145,7 +155,7 @@ public class MethodInvokerTests {
MethodInvoker methodInvoker = new MethodInvoker(); MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetObject(new Greeter()); methodInvoker.setTargetObject(new Greeter());
methodInvoker.setTargetMethod("greet"); methodInvoker.setTargetMethod("greet");
methodInvoker.setArguments(new Object[] {new VIP("Fonzie")}); methodInvoker.setArguments(new VIP("Fonzie"));
methodInvoker.prepare(); methodInvoker.prepare();
String greeting = (String) methodInvoker.invoke(); String greeting = (String) methodInvoker.invoke();
assertEquals("regular: whassup dude?", greeting); assertEquals("regular: whassup dude?", greeting);