Skip to content

Commit 8336ca9

Browse files
committed
Use Objects.requireNonNull()
1 parent 13c9f98 commit 8336ca9

File tree

4 files changed

+27
-77
lines changed

4 files changed

+27
-77
lines changed

src/main/java/org/apache/commons/beanutils/BasicDynaBean.java

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.HashMap;
2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.Objects;
2526

2627
/**
2728
* <p>Minimal implementation of the <code>DynaBean</code> interface. Can be
@@ -80,18 +81,12 @@ public BasicDynaBean(final DynaClass dynaClass) {
8081
*/
8182
@Override
8283
public boolean contains(final String name, final String key) {
83-
8484
final Object value = values.get(name);
85-
if (value == null) {
86-
throw new NullPointerException
87-
("No mapped value for '" + name + "(" + key + ")'");
88-
}
85+
Objects.requireNonNull(value, () -> "No mapped value for '" + name + "(" + key + ")'");
8986
if (value instanceof Map) {
9087
return ((Map<?, ?>) value).containsKey(key);
9188
}
92-
throw new IllegalArgumentException
93-
("Non-mapped property for '" + name + "(" + key + ")'");
94-
89+
throw new IllegalArgumentException("Non-mapped property for '" + name + "(" + key + ")'");
9590
}
9691

9792
/**
@@ -162,21 +157,15 @@ public Object get(final String name) {
162157
*/
163158
@Override
164159
public Object get(final String name, final int index) {
165-
166160
final Object value = values.get(name);
167-
if (value == null) {
168-
throw new NullPointerException
169-
("No indexed value for '" + name + "[" + index + "]'");
170-
}
161+
Objects.requireNonNull(value, () -> "No indexed value for '" + name + "[" + index + "]'");
171162
if (value.getClass().isArray()) {
172163
return Array.get(value, index);
173164
}
174165
if (value instanceof List) {
175166
return ((List<?>) value).get(index);
176167
}
177-
throw new IllegalArgumentException
178-
("Non-indexed property for '" + name + "[" + index + "]'");
179-
168+
throw new IllegalArgumentException("Non-indexed property for '" + name + "[" + index + "]'");
180169
}
181170

182171
/**
@@ -193,18 +182,12 @@ public Object get(final String name, final int index) {
193182
*/
194183
@Override
195184
public Object get(final String name, final String key) {
196-
197185
final Object value = values.get(name);
198-
if (value == null) {
199-
throw new NullPointerException
200-
("No mapped value for '" + name + "(" + key + ")'");
201-
}
186+
Objects.requireNonNull(value, () -> "No mapped value for '" + name + "(" + key + ")'");
202187
if (value instanceof Map) {
203188
return ((Map<?, ?>) value).get(key);
204189
}
205-
throw new IllegalArgumentException
206-
("Non-mapped property for '" + name + "(" + key + ")'");
207-
190+
throw new IllegalArgumentException("Non-mapped property for '" + name + "(" + key + ")'");
208191
}
209192

210193
/**
@@ -297,18 +280,12 @@ protected boolean isAssignable(final Class<?> dest, final Class<?> source) {
297280
*/
298281
@Override
299282
public void remove(final String name, final String key) {
300-
301283
final Object value = values.get(name);
302-
if (value == null) {
303-
throw new NullPointerException
304-
("No mapped value for '" + name + "(" + key + ")'");
305-
}
284+
Objects.requireNonNull(value, () -> "No mapped value for '" + name + "(" + key + ")'");
306285
if (!(value instanceof Map)) {
307-
throw new IllegalArgumentException
308-
("Non-mapped property for '" + name + "(" + key + ")'");
286+
throw new IllegalArgumentException("Non-mapped property for '" + name + "(" + key + ")'");
309287
}
310288
((Map<?, ?>) value).remove(key);
311-
312289
}
313290

314291
/**
@@ -330,10 +307,7 @@ public void remove(final String name, final String key) {
330307
public void set(final String name, final int index, final Object value) {
331308

332309
final Object prop = values.get(name);
333-
if (prop == null) {
334-
throw new NullPointerException
335-
("No indexed value for '" + name + "[" + index + "]'");
336-
}
310+
Objects.requireNonNull(prop, () -> "No indexed value for '" + name + "[" + index + "]'");
337311
if (prop.getClass().isArray()) {
338312
Array.set(prop, index, value);
339313
} else if (prop instanceof List) {
@@ -401,23 +375,18 @@ public void set(final String name, final Object value) {
401375
*/
402376
@Override
403377
public void set(final String name, final String key, final Object value) {
404-
405378
final Object prop = values.get(name);
406-
if (prop == null) {
407-
throw new NullPointerException
408-
("No mapped value for '" + name + "(" + key + ")'");
409-
}
379+
Objects.requireNonNull(prop, () -> "No mapped value for '" + name + "(" + key + ")'");
410380
if (!(prop instanceof Map)) {
411381
throw new IllegalArgumentException
412382
("Non-mapped property for '" + name + "(" + key + ")'");
413383
}
414-
@SuppressWarnings("unchecked")
415-
final
416384
// This is safe to cast because mapped properties are always
417385
// maps of types String -> Object
418-
Map<String, Object> map = (Map<String, Object>) prop;
386+
@SuppressWarnings("unchecked")
387+
final Map<String, Object> map = (Map<String, Object>) prop;
419388
map.put(key, value);
420-
421389
}
422390

423391
}
392+

src/main/java/org/apache/commons/beanutils/ResultSetDynaClass.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.sql.ResultSet;
2121
import java.sql.SQLException;
2222
import java.util.Iterator;
23+
import java.util.Objects;
2324

2425
/**
2526
* <p>Implements <code>DynaClass</code> for DynaBeans that wrap the
@@ -153,11 +154,7 @@ public ResultSetDynaClass(final ResultSet resultSet, final boolean lowerCase)
153154
*/
154155
public ResultSetDynaClass(final ResultSet resultSet, final boolean lowerCase, final boolean useColumnLabel)
155156
throws SQLException {
156-
157-
if (resultSet == null) {
158-
throw new NullPointerException();
159-
}
160-
this.resultSet = resultSet;
157+
this.resultSet = Objects.requireNonNull(resultSet, "resultSet");
161158
this.lowerCase = lowerCase;
162159
setUseColumnLabel(useColumnLabel);
163160
introspect(resultSet);

src/main/java/org/apache/commons/beanutils/RowSetDynaClass.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.sql.SQLException;
2222
import java.util.ArrayList;
2323
import java.util.List;
24+
import java.util.Objects;
2425

2526
/**
2627
* <p>Implements {@link DynaClass} to create an in-memory collection
@@ -198,17 +199,13 @@ public RowSetDynaClass(final ResultSet resultSet, final boolean lowerCase, final
198199
* @since 1.8.3
199200
*/
200201
public RowSetDynaClass(final ResultSet resultSet, final boolean lowerCase, final int limit, final boolean useColumnLabel)
201-
throws SQLException {
202-
203-
if (resultSet == null) {
204-
throw new NullPointerException();
205-
}
202+
throws SQLException {
203+
Objects.requireNonNull(resultSet, "resultSet");
206204
this.lowerCase = lowerCase;
207205
this.limit = limit;
208206
setUseColumnLabel(useColumnLabel);
209207
introspect(resultSet);
210208
copy(resultSet);
211-
212209
}
213210

214211
/**

src/main/java/org/apache/commons/beanutils/converters/AbstractArrayConverter.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.StringReader;
2323
import java.util.ArrayList;
2424
import java.util.List;
25+
import java.util.Objects;
2526

2627
import org.apache.commons.beanutils.ConversionException;
2728
import org.apache.commons.beanutils.Converter;
@@ -135,55 +136,41 @@ public AbstractArrayConverter(final Object defaultValue) {
135136
* is <code>null</code>
136137
*/
137138
protected List<String> parseElements(String svalue) {
138-
139139
// Validate the passed argument
140-
if (svalue == null) {
141-
throw new NullPointerException();
142-
}
143-
140+
Objects.requireNonNull(svalue, "svalue");
144141
// Trim any matching '{' and '}' delimiters
145142
svalue = svalue.trim();
146143
if (svalue.startsWith("{") && svalue.endsWith("}")) {
147144
svalue = svalue.substring(1, svalue.length() - 1);
148145
}
149-
150146
try {
151147

152148
// Set up a StreamTokenizer on the characters in this String
153-
final StreamTokenizer st =
154-
new StreamTokenizer(new StringReader(svalue));
155-
st.whitespaceChars(',',','); // Commas are delimiters
156-
st.ordinaryChars('0', '9'); // Needed to turn off numeric flag
149+
final StreamTokenizer st = new StreamTokenizer(new StringReader(svalue));
150+
st.whitespaceChars(',', ','); // Commas are delimiters
151+
st.ordinaryChars('0', '9'); // Needed to turn off numeric flag
157152
st.ordinaryChars('.', '.');
158153
st.ordinaryChars('-', '-');
159-
st.wordChars('0', '9'); // Needed to make part of tokens
154+
st.wordChars('0', '9'); // Needed to make part of tokens
160155
st.wordChars('.', '.');
161156
st.wordChars('-', '-');
162-
163157
// Split comma-delimited tokens into a List
164158
final ArrayList<String> list = new ArrayList<>();
165159
while (true) {
166160
final int ttype = st.nextToken();
167-
if (ttype == StreamTokenizer.TT_WORD ||
168-
ttype > 0) {
161+
if (ttype == StreamTokenizer.TT_WORD || ttype > 0) {
169162
list.add(st.sval);
170163
} else if (ttype == StreamTokenizer.TT_EOF) {
171164
break;
172165
} else {
173-
throw new ConversionException
174-
("Encountered token of type " + ttype);
166+
throw new ConversionException("Encountered token of type " + ttype);
175167
}
176168
}
177-
178169
// Return the completed list
179170
return list;
180-
181171
} catch (final IOException e) {
182-
183172
throw new ConversionException(e);
184-
185173
}
186-
187174
}
188175

189176
}

0 commit comments

Comments
 (0)