Skip to content

Commit c29b6f5

Browse files
committed
Consistent handling of null array for arguments
Issue: SPR-16075
1 parent 3890d4c commit c29b6f5

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

spring-core/src/main/java/org/springframework/util/MethodInvoker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class MethodInvoker {
5151
private String staticMethod;
5252

5353
@Nullable
54-
private Object[] arguments = new Object[0];
54+
private Object[] arguments;
5555

5656
/** The method we will call */
5757
@Nullable

spring-core/src/test/java/org/springframework/util/MethodInvokerTests.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,26 +49,36 @@ public void plainMethodInvoker() throws Exception {
4949
Integer i = (Integer) mi.invoke();
5050
assertEquals(1, i.intValue());
5151

52+
// defensive check: singleton, non-static should work with null array
53+
tc1 = new TestClass1();
54+
mi = new MethodInvoker();
55+
mi.setTargetObject(tc1);
56+
mi.setTargetMethod("method1");
57+
mi.setArguments((Object[]) null);
58+
mi.prepare();
59+
i = (Integer) mi.invoke();
60+
assertEquals(1, i.intValue());
61+
5262
// sanity check: check that argument count matching works
5363
mi = new MethodInvoker();
5464
mi.setTargetClass(TestClass1.class);
5565
mi.setTargetMethod("supertypes");
56-
mi.setArguments(new Object[] {new ArrayList<>(), new ArrayList<>(), "hello"});
66+
mi.setArguments(new ArrayList<>(), new ArrayList<>(), "hello");
5767
mi.prepare();
5868
assertEquals("hello", mi.invoke());
5969

6070
mi = new MethodInvoker();
6171
mi.setTargetClass(TestClass1.class);
6272
mi.setTargetMethod("supertypes2");
63-
mi.setArguments(new Object[] {new ArrayList<>(), new ArrayList<>(), "hello", "bogus"});
73+
mi.setArguments(new ArrayList<>(), new ArrayList<>(), "hello", "bogus");
6474
mi.prepare();
6575
assertEquals("hello", mi.invoke());
6676

6777
// Sanity check: check that argument conversion doesn't work with plain MethodInvoker
6878
mi = new MethodInvoker();
6979
mi.setTargetClass(TestClass1.class);
7080
mi.setTargetMethod("supertypes2");
71-
mi.setArguments(new Object[] {new ArrayList<>(), new ArrayList<>(), "hello", Boolean.TRUE});
81+
mi.setArguments(new ArrayList<>(), new ArrayList<>(), "hello", Boolean.TRUE);
7282

7383
exception.expect(NoSuchMethodException.class);
7484
mi.prepare();
@@ -79,7 +89,7 @@ public void stringWithMethodInvoker() throws Exception {
7989
MethodInvoker methodInvoker = new MethodInvoker();
8090
methodInvoker.setTargetObject(new Greeter());
8191
methodInvoker.setTargetMethod("greet");
82-
methodInvoker.setArguments(new Object[] {"no match"});
92+
methodInvoker.setArguments("no match");
8393

8494
exception.expect(NoSuchMethodException.class);
8595
methodInvoker.prepare();
@@ -90,7 +100,7 @@ public void purchaserWithMethodInvoker() throws Exception {
90100
MethodInvoker methodInvoker = new MethodInvoker();
91101
methodInvoker.setTargetObject(new Greeter());
92102
methodInvoker.setTargetMethod("greet");
93-
methodInvoker.setArguments(new Object[] {new Purchaser()});
103+
methodInvoker.setArguments(new Purchaser());
94104
methodInvoker.prepare();
95105
String greeting = (String) methodInvoker.invoke();
96106
assertEquals("purchaser: hello", greeting);
@@ -101,7 +111,7 @@ public void shopperWithMethodInvoker() throws Exception {
101111
MethodInvoker methodInvoker = new MethodInvoker();
102112
methodInvoker.setTargetObject(new Greeter());
103113
methodInvoker.setTargetMethod("greet");
104-
methodInvoker.setArguments(new Object[] {new Shopper()});
114+
methodInvoker.setArguments(new Shopper());
105115
methodInvoker.prepare();
106116
String greeting = (String) methodInvoker.invoke();
107117
assertEquals("purchaser: may I help you?", greeting);
@@ -112,7 +122,7 @@ public void salesmanWithMethodInvoker() throws Exception {
112122
MethodInvoker methodInvoker = new MethodInvoker();
113123
methodInvoker.setTargetObject(new Greeter());
114124
methodInvoker.setTargetMethod("greet");
115-
methodInvoker.setArguments(new Object[] {new Salesman()});
125+
methodInvoker.setArguments(new Salesman());
116126
methodInvoker.prepare();
117127
String greeting = (String) methodInvoker.invoke();
118128
assertEquals("greetable: how are sales?", greeting);
@@ -123,7 +133,7 @@ public void customerWithMethodInvoker() throws Exception {
123133
MethodInvoker methodInvoker = new MethodInvoker();
124134
methodInvoker.setTargetObject(new Greeter());
125135
methodInvoker.setTargetMethod("greet");
126-
methodInvoker.setArguments(new Object[] {new Customer()});
136+
methodInvoker.setArguments(new Customer());
127137
methodInvoker.prepare();
128138
String greeting = (String) methodInvoker.invoke();
129139
assertEquals("customer: good day", greeting);
@@ -134,7 +144,7 @@ public void regularWithMethodInvoker() throws Exception {
134144
MethodInvoker methodInvoker = new MethodInvoker();
135145
methodInvoker.setTargetObject(new Greeter());
136146
methodInvoker.setTargetMethod("greet");
137-
methodInvoker.setArguments(new Object[] {new Regular("Kotter")});
147+
methodInvoker.setArguments(new Regular("Kotter"));
138148
methodInvoker.prepare();
139149
String greeting = (String) methodInvoker.invoke();
140150
assertEquals("regular: welcome back Kotter", greeting);
@@ -145,7 +155,7 @@ public void vipWithMethodInvoker() throws Exception {
145155
MethodInvoker methodInvoker = new MethodInvoker();
146156
methodInvoker.setTargetObject(new Greeter());
147157
methodInvoker.setTargetMethod("greet");
148-
methodInvoker.setArguments(new Object[] {new VIP("Fonzie")});
158+
methodInvoker.setArguments(new VIP("Fonzie"));
149159
methodInvoker.prepare();
150160
String greeting = (String) methodInvoker.invoke();
151161
assertEquals("regular: whassup dude?", greeting);

0 commit comments

Comments
 (0)