Skip to content

Commit 6ffb043

Browse files
sslaviccbeams
authored andcommitted
Allow null params as advertised in Constants#toCode*
Even though the Javadoc for Constants#toCode and #toCodeForSuffix specifies that a null value for the 'namePrefix' and 'nameSuffix' parameters are respectively allowed, before this change passing a null to either would result in a NullPointerException. This change fixes constant name lookup for null values of these params as if an empty string had been passed instead. This way of handling a null value is consistent with the rest of Constants class API. Issue: SPR-8278
1 parent 95e99fe commit 6ffb043

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

spring-core/src/main/java/org/springframework/core/Constants.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -264,7 +264,7 @@ public Set<Object> getValuesForSuffix(String nameSuffix) {
264264
* @throws ConstantException if the value wasn't found
265265
*/
266266
public String toCode(Object value, String namePrefix) throws ConstantException {
267-
String prefixToUse = (namePrefix != null ? namePrefix.trim().toUpperCase(Locale.ENGLISH) : null);
267+
String prefixToUse = (namePrefix != null ? namePrefix.trim().toUpperCase(Locale.ENGLISH) : "");
268268
for (Map.Entry<String, Object> entry : this.fieldCache.entrySet()) {
269269
if (entry.getKey().startsWith(prefixToUse) && entry.getValue().equals(value)) {
270270
return entry.getKey();
@@ -295,7 +295,7 @@ public String toCodeForProperty(Object value, String propertyName) throws Consta
295295
* @throws ConstantException if the value wasn't found
296296
*/
297297
public String toCodeForSuffix(Object value, String nameSuffix) throws ConstantException {
298-
String suffixToUse = (nameSuffix != null ? nameSuffix.trim().toUpperCase(Locale.ENGLISH) : null);
298+
String suffixToUse = (nameSuffix != null ? nameSuffix.trim().toUpperCase(Locale.ENGLISH) : "");
299299
for (Map.Entry<String, Object> entry : this.fieldCache.entrySet()) {
300300
if (entry.getKey().endsWith(suffixToUse) && entry.getValue().equals(value)) {
301301
return entry.getKey();

spring-core/src/test/java/org/springframework/core/ConstantsTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -148,19 +148,28 @@ public void testToCode() {
148148
assertEquals(c.toCode(new Integer(0), "D"), "DOG");
149149
assertEquals(c.toCode(new Integer(0), "DO"), "DOG");
150150
assertEquals(c.toCode(new Integer(0), "DoG"), "DOG");
151+
assertEquals(c.toCode(new Integer(0), null), "DOG");
151152
assertEquals(c.toCode(new Integer(66), ""), "CAT");
152153
assertEquals(c.toCode(new Integer(66), "C"), "CAT");
153154
assertEquals(c.toCode(new Integer(66), "ca"), "CAT");
154155
assertEquals(c.toCode(new Integer(66), "cAt"), "CAT");
156+
assertEquals(c.toCode(new Integer(66), null), "CAT");
155157
assertEquals(c.toCode("", ""), "S1");
156158
assertEquals(c.toCode("", "s"), "S1");
157159
assertEquals(c.toCode("", "s1"), "S1");
160+
assertEquals(c.toCode("", null), "S1");
158161
try {
159162
c.toCode("bogus", "bogus");
160163
fail("Should have thrown ConstantException");
161164
}
162165
catch (ConstantException expected) {
163166
}
167+
try {
168+
c.toCode("bogus", null);
169+
fail("Should have thrown ConstantException");
170+
}
171+
catch (ConstantException expected) {
172+
}
164173

165174
assertEquals(c.toCodeForProperty(new Integer(1), "myProperty"), "MY_PROPERTY_NO");
166175
assertEquals(c.toCodeForProperty(new Integer(2), "myProperty"), "MY_PROPERTY_YES");
@@ -175,19 +184,28 @@ public void testToCode() {
175184
assertEquals(c.toCodeForSuffix(new Integer(0), "G"), "DOG");
176185
assertEquals(c.toCodeForSuffix(new Integer(0), "OG"), "DOG");
177186
assertEquals(c.toCodeForSuffix(new Integer(0), "DoG"), "DOG");
187+
assertEquals(c.toCodeForSuffix(new Integer(0), null), "DOG");
178188
assertEquals(c.toCodeForSuffix(new Integer(66), ""), "CAT");
179189
assertEquals(c.toCodeForSuffix(new Integer(66), "T"), "CAT");
180190
assertEquals(c.toCodeForSuffix(new Integer(66), "at"), "CAT");
181191
assertEquals(c.toCodeForSuffix(new Integer(66), "cAt"), "CAT");
192+
assertEquals(c.toCodeForSuffix(new Integer(66), null), "CAT");
182193
assertEquals(c.toCodeForSuffix("", ""), "S1");
183194
assertEquals(c.toCodeForSuffix("", "1"), "S1");
184195
assertEquals(c.toCodeForSuffix("", "s1"), "S1");
196+
assertEquals(c.toCodeForSuffix("", null), "S1");
185197
try {
186198
c.toCodeForSuffix("bogus", "bogus");
187199
fail("Should have thrown ConstantException");
188200
}
189201
catch (ConstantException expected) {
190202
}
203+
try {
204+
c.toCodeForSuffix("bogus", null);
205+
fail("Should have thrown ConstantException");
206+
}
207+
catch (ConstantException expected) {
208+
}
191209
}
192210

193211
public void testGetValuesWithNullPrefix() throws Exception {

0 commit comments

Comments
 (0)