Skip to content

Commit 03be809

Browse files
committed
Consistent handling of null array for arguments
Issue: SPR-16075 (cherry picked from commit c29b6f5)
1 parent 4daf685 commit 03be809

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

spring-beans/src/test/java/org/springframework/beans/factory/config/MethodInvokingFactoryBeanTests.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 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.
@@ -140,15 +140,15 @@ public void testGetObjectType() throws Exception {
140140
mcfb.setTargetMethod("voidRetvalMethod");
141141
mcfb.afterPropertiesSet();
142142
Class<?> objType = mcfb.getObjectType();
143-
assertTrue(objType.equals(void.class));
143+
assertSame(objType, void.class);
144144

145145
// verify that we can call a method with args that are subtypes of the
146146
// target method arg types
147147
TestClass1._staticField1 = 0;
148148
mcfb = new MethodInvokingFactoryBean();
149149
mcfb.setTargetClass(TestClass1.class);
150150
mcfb.setTargetMethod("supertypes");
151-
mcfb.setArguments(new Object[] {new ArrayList<Object>(), new ArrayList<Object>(), "hello"});
151+
mcfb.setArguments(new ArrayList<>(), new ArrayList<Object>(), "hello");
152152
mcfb.afterPropertiesSet();
153153
mcfb.getObjectType();
154154

@@ -157,7 +157,7 @@ public void testGetObjectType() throws Exception {
157157
mcfb.registerCustomEditor(String.class, new StringTrimmerEditor(false));
158158
mcfb.setTargetClass(TestClass1.class);
159159
mcfb.setTargetMethod("supertypes");
160-
mcfb.setArguments(new Object[] {"1", new Object()});
160+
mcfb.setArguments("1", new Object());
161161
try {
162162
mcfb.afterPropertiesSet();
163163
fail("Should have thrown NoSuchMethodException");
@@ -225,7 +225,7 @@ public void testGetObject() throws Exception {
225225
mcfb = new MethodInvokingFactoryBean();
226226
mcfb.setTargetClass(TestClass1.class);
227227
mcfb.setTargetMethod("supertypes");
228-
mcfb.setArguments(new Object[] {new ArrayList<Object>(), new ArrayList<Object>(), "hello"});
228+
mcfb.setArguments(new ArrayList<>(), new ArrayList<Object>(), "hello");
229229
// should pass
230230
mcfb.afterPropertiesSet();
231231
}
@@ -235,7 +235,7 @@ public void testArgumentConversion() throws Exception {
235235
MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean();
236236
mcfb.setTargetClass(TestClass1.class);
237237
mcfb.setTargetMethod("supertypes");
238-
mcfb.setArguments(new Object[] {new ArrayList<Object>(), new ArrayList<Object>(), "hello", "bogus"});
238+
mcfb.setArguments(new ArrayList<>(), new ArrayList<Object>(), "hello", "bogus");
239239
try {
240240
mcfb.afterPropertiesSet();
241241
fail("Matched method with wrong number of args");
@@ -247,7 +247,7 @@ public void testArgumentConversion() throws Exception {
247247
mcfb = new MethodInvokingFactoryBean();
248248
mcfb.setTargetClass(TestClass1.class);
249249
mcfb.setTargetMethod("supertypes");
250-
mcfb.setArguments(new Object[] {1, new Object()});
250+
mcfb.setArguments(1, new Object());
251251
try {
252252
mcfb.afterPropertiesSet();
253253
mcfb.getObject();
@@ -260,14 +260,14 @@ public void testArgumentConversion() throws Exception {
260260
mcfb = new MethodInvokingFactoryBean();
261261
mcfb.setTargetClass(TestClass1.class);
262262
mcfb.setTargetMethod("supertypes2");
263-
mcfb.setArguments(new Object[] {new ArrayList<Object>(), new ArrayList<Object>(), "hello", "bogus"});
263+
mcfb.setArguments(new ArrayList<>(), new ArrayList<Object>(), "hello", "bogus");
264264
mcfb.afterPropertiesSet();
265265
assertEquals("hello", mcfb.getObject());
266266

267267
mcfb = new MethodInvokingFactoryBean();
268268
mcfb.setTargetClass(TestClass1.class);
269269
mcfb.setTargetMethod("supertypes2");
270-
mcfb.setArguments(new Object[] {new ArrayList<Object>(), new ArrayList<Object>(), new Object()});
270+
mcfb.setArguments(new ArrayList<>(), new ArrayList<Object>(), new Object());
271271
try {
272272
mcfb.afterPropertiesSet();
273273
fail("Matched method when shouldn't have matched");
@@ -292,14 +292,14 @@ public void testInvokeWithIntArgument() throws Exception {
292292
ArgumentConvertingMethodInvoker methodInvoker = new ArgumentConvertingMethodInvoker();
293293
methodInvoker.setTargetClass(TestClass1.class);
294294
methodInvoker.setTargetMethod("intArgument");
295-
methodInvoker.setArguments(new Object[] {5});
295+
methodInvoker.setArguments(5);
296296
methodInvoker.prepare();
297297
methodInvoker.invoke();
298298

299299
methodInvoker = new ArgumentConvertingMethodInvoker();
300300
methodInvoker.setTargetClass(TestClass1.class);
301301
methodInvoker.setTargetMethod("intArgument");
302-
methodInvoker.setArguments(new Object[] {"5"});
302+
methodInvoker.setArguments(5);
303303
methodInvoker.prepare();
304304
methodInvoker.invoke();
305305
}
@@ -309,37 +309,37 @@ public void testInvokeWithIntArguments() throws Exception {
309309
MethodInvokingBean methodInvoker = new MethodInvokingBean();
310310
methodInvoker.setTargetClass(TestClass1.class);
311311
methodInvoker.setTargetMethod("intArguments");
312-
methodInvoker.setArguments(new Object[]{new Integer[] {5, 10}});
312+
methodInvoker.setArguments(new Object[] {new Integer[] {5, 10}});
313313
methodInvoker.afterPropertiesSet();
314314

315315
methodInvoker = new MethodInvokingBean();
316316
methodInvoker.setTargetClass(TestClass1.class);
317317
methodInvoker.setTargetMethod("intArguments");
318-
methodInvoker.setArguments(new Object[]{new String[]{"5", "10"}});
318+
methodInvoker.setArguments(new Object[] {new String[] {"5", "10"}});
319319
methodInvoker.afterPropertiesSet();
320320

321321
methodInvoker = new MethodInvokingBean();
322322
methodInvoker.setTargetClass(TestClass1.class);
323323
methodInvoker.setTargetMethod("intArguments");
324-
methodInvoker.setArguments(new Object[]{new Integer[] {5, 10}});
324+
methodInvoker.setArguments(new Object[] {new Integer[] {5, 10}});
325325
methodInvoker.afterPropertiesSet();
326326

327327
methodInvoker = new MethodInvokingBean();
328328
methodInvoker.setTargetClass(TestClass1.class);
329329
methodInvoker.setTargetMethod("intArguments");
330-
methodInvoker.setArguments(new String[]{"5", "10"});
330+
methodInvoker.setArguments("5", "10");
331331
methodInvoker.afterPropertiesSet();
332332

333333
methodInvoker = new MethodInvokingBean();
334334
methodInvoker.setTargetClass(TestClass1.class);
335335
methodInvoker.setTargetMethod("intArguments");
336-
methodInvoker.setArguments(new Object[]{new Integer[] {5, 10}});
336+
methodInvoker.setArguments(new Object[] {new Integer[] {5, 10}});
337337
methodInvoker.afterPropertiesSet();
338338

339339
methodInvoker = new MethodInvokingBean();
340340
methodInvoker.setTargetClass(TestClass1.class);
341341
methodInvoker.setTargetMethod("intArguments");
342-
methodInvoker.setArguments(new Object[]{"5", "10"});
342+
methodInvoker.setArguments("5", "10");
343343
methodInvoker.afterPropertiesSet();
344344
}
345345

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class MethodInvoker {
4444

4545
private String staticMethod;
4646

47-
private Object[] arguments = new Object[0];
47+
private Object[] arguments;
4848

4949
/** The method we will call */
5050
private Method methodObject;
@@ -130,7 +130,7 @@ public void setArguments(Object... arguments) {
130130
* Return the arguments for the method invocation.
131131
*/
132132
public Object[] getArguments() {
133-
return this.arguments;
133+
return (this.arguments != null ? this.arguments : new Object[0]);
134134
}
135135

136136

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

+21-11
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)