Skip to content
This repository was archived by the owner on Oct 11, 2024. It is now read-only.

Commit 0e8f5d8

Browse files
committed
Merge pull request spring-projects#57 from sslavic/SPR-8278
* SPR-8278: Fix compiler warnings in Constants/ConstantsTests Allow null params as advertised in Constants#toCode*
2 parents 95e99fe + 1f4b33c commit 0e8f5d8

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

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

+5-5
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.
@@ -58,7 +58,7 @@ public class Constants {
5858
* @param clazz the class to analyze
5959
* @throws IllegalArgumentException if the supplied <code>clazz</code> is <code>null</code>
6060
*/
61-
public Constants(Class clazz) {
61+
public Constants(Class<?> clazz) {
6262
Assert.notNull(clazz);
6363
this.className = clazz.getName();
6464
Field[] fields = clazz.getFields();
@@ -189,7 +189,7 @@ public Set<String> getNamesForProperty(String propertyName) {
189189
* @param nameSuffix suffix of the constant names to search (may be <code>null</code>)
190190
* @return the set of constant names
191191
*/
192-
public Set getNamesForSuffix(String nameSuffix) {
192+
public Set<String> getNamesForSuffix(String nameSuffix) {
193193
String suffixToUse = (nameSuffix != null ? nameSuffix.trim().toUpperCase(Locale.ENGLISH) : "");
194194
Set<String> names = new HashSet<String>();
195195
for (String code : this.fieldCache.keySet()) {
@@ -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

+31-12
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.
@@ -57,7 +57,7 @@ public void testConstants() {
5757
public void testGetNames() {
5858
Constants c = new Constants(A.class);
5959

60-
Set names = c.getNames("");
60+
Set<?> names = c.getNames("");
6161
assertEquals(c.getSize(), names.size());
6262
assertTrue(names.contains("DOG"));
6363
assertTrue(names.contains("CAT"));
@@ -75,7 +75,7 @@ public void testGetNames() {
7575
public void testGetValues() {
7676
Constants c = new Constants(A.class);
7777

78-
Set values = c.getValues("");
78+
Set<?> values = c.getValues("");
7979
assertEquals(7, values.size());
8080
assertTrue(values.contains(new Integer(0)));
8181
assertTrue(values.contains(new Integer(66)));
@@ -102,7 +102,7 @@ public void testGetValuesInTurkey() {
102102
try {
103103
Constants c = new Constants(A.class);
104104

105-
Set values = c.getValues("");
105+
Set<?> values = c.getValues("");
106106
assertEquals(7, values.size());
107107
assertTrue(values.contains(new Integer(0)));
108108
assertTrue(values.contains(new Integer(66)));
@@ -130,12 +130,12 @@ public void testGetValuesInTurkey() {
130130
public void testSuffixAccess() {
131131
Constants c = new Constants(A.class);
132132

133-
Set names = c.getNamesForSuffix("_PROPERTY");
133+
Set<?> names = c.getNamesForSuffix("_PROPERTY");
134134
assertEquals(2, names.size());
135135
assertTrue(names.contains("NO_PROPERTY"));
136136
assertTrue(names.contains("YES_PROPERTY"));
137137

138-
Set values = c.getValuesForSuffix("_PROPERTY");
138+
Set<?> values = c.getValuesForSuffix("_PROPERTY");
139139
assertEquals(2, values.size());
140140
assertTrue(values.contains(new Integer(3)));
141141
assertTrue(values.contains(new Integer(4)));
@@ -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,43 +184,52 @@ 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 {
194212
Constants c = new Constants(A.class);
195-
Set values = c.getValues(null);
213+
Set<?> values = c.getValues(null);
196214
assertEquals("Must have returned *all* public static final values", 7, values.size());
197215
}
198216

199217
public void testGetValuesWithEmptyStringPrefix() throws Exception {
200218
Constants c = new Constants(A.class);
201-
Set values = c.getValues("");
219+
Set<Object> values = c.getValues("");
202220
assertEquals("Must have returned *all* public static final values", 7, values.size());
203221
}
204222

205223
public void testGetValuesWithWhitespacedStringPrefix() throws Exception {
206224
Constants c = new Constants(A.class);
207-
Set values = c.getValues(" ");
225+
Set<?> values = c.getValues(" ");
208226
assertEquals("Must have returned *all* public static final values", 7, values.size());
209227
}
210228

211229
public void testWithClassThatExposesNoConstants() throws Exception {
212230
Constants c = new Constants(NoConstants.class);
213231
assertEquals(0, c.getSize());
214-
final Set values = c.getValues("");
232+
final Set<?> values = c.getValues("");
215233
assertNotNull(values);
216234
assertEquals(0, values.size());
217235
}
@@ -227,10 +245,11 @@ public void testCtorWithNullClass() throws Exception {
227245

228246
private static final class NoConstants {
229247
}
230-
231248

249+
250+
@SuppressWarnings("unused")
232251
private static final class A {
233-
252+
234253
public static final int DOG = 0;
235254
public static final int CAT = 66;
236255
public static final String S1 = "";

0 commit comments

Comments
 (0)