From 8336ca948eed1bd2d0f0df255d29d898ffbc3ba6 Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Thu, 26 Dec 2024 14:01:59 -0500 Subject: [PATCH] Use Objects.requireNonNull() --- .../commons/beanutils/BasicDynaBean.java | 59 +++++-------------- .../commons/beanutils/ResultSetDynaClass.java | 7 +-- .../commons/beanutils/RowSetDynaClass.java | 9 +-- .../converters/AbstractArrayConverter.java | 29 +++------ 4 files changed, 27 insertions(+), 77 deletions(-) diff --git a/src/main/java/org/apache/commons/beanutils/BasicDynaBean.java b/src/main/java/org/apache/commons/beanutils/BasicDynaBean.java index 031e5efec..92e929d66 100644 --- a/src/main/java/org/apache/commons/beanutils/BasicDynaBean.java +++ b/src/main/java/org/apache/commons/beanutils/BasicDynaBean.java @@ -22,6 +22,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; /** *

Minimal implementation of the DynaBean interface. Can be @@ -80,18 +81,12 @@ public BasicDynaBean(final DynaClass dynaClass) { */ @Override public boolean contains(final String name, final String key) { - final Object value = values.get(name); - if (value == null) { - throw new NullPointerException - ("No mapped value for '" + name + "(" + key + ")'"); - } + Objects.requireNonNull(value, () -> "No mapped value for '" + name + "(" + key + ")'"); if (value instanceof Map) { return ((Map) value).containsKey(key); } - throw new IllegalArgumentException - ("Non-mapped property for '" + name + "(" + key + ")'"); - + throw new IllegalArgumentException("Non-mapped property for '" + name + "(" + key + ")'"); } /** @@ -162,21 +157,15 @@ public Object get(final String name) { */ @Override public Object get(final String name, final int index) { - final Object value = values.get(name); - if (value == null) { - throw new NullPointerException - ("No indexed value for '" + name + "[" + index + "]'"); - } + Objects.requireNonNull(value, () -> "No indexed value for '" + name + "[" + index + "]'"); if (value.getClass().isArray()) { return Array.get(value, index); } if (value instanceof List) { return ((List) value).get(index); } - throw new IllegalArgumentException - ("Non-indexed property for '" + name + "[" + index + "]'"); - + throw new IllegalArgumentException("Non-indexed property for '" + name + "[" + index + "]'"); } /** @@ -193,18 +182,12 @@ public Object get(final String name, final int index) { */ @Override public Object get(final String name, final String key) { - final Object value = values.get(name); - if (value == null) { - throw new NullPointerException - ("No mapped value for '" + name + "(" + key + ")'"); - } + Objects.requireNonNull(value, () -> "No mapped value for '" + name + "(" + key + ")'"); if (value instanceof Map) { return ((Map) value).get(key); } - throw new IllegalArgumentException - ("Non-mapped property for '" + name + "(" + key + ")'"); - + throw new IllegalArgumentException("Non-mapped property for '" + name + "(" + key + ")'"); } /** @@ -297,18 +280,12 @@ protected boolean isAssignable(final Class dest, final Class source) { */ @Override public void remove(final String name, final String key) { - final Object value = values.get(name); - if (value == null) { - throw new NullPointerException - ("No mapped value for '" + name + "(" + key + ")'"); - } + Objects.requireNonNull(value, () -> "No mapped value for '" + name + "(" + key + ")'"); if (!(value instanceof Map)) { - throw new IllegalArgumentException - ("Non-mapped property for '" + name + "(" + key + ")'"); + throw new IllegalArgumentException("Non-mapped property for '" + name + "(" + key + ")'"); } ((Map) value).remove(key); - } /** @@ -330,10 +307,7 @@ public void remove(final String name, final String key) { public void set(final String name, final int index, final Object value) { final Object prop = values.get(name); - if (prop == null) { - throw new NullPointerException - ("No indexed value for '" + name + "[" + index + "]'"); - } + Objects.requireNonNull(prop, () -> "No indexed value for '" + name + "[" + index + "]'"); if (prop.getClass().isArray()) { Array.set(prop, index, value); } else if (prop instanceof List) { @@ -401,23 +375,18 @@ public void set(final String name, final Object value) { */ @Override public void set(final String name, final String key, final Object value) { - final Object prop = values.get(name); - if (prop == null) { - throw new NullPointerException - ("No mapped value for '" + name + "(" + key + ")'"); - } + Objects.requireNonNull(prop, () -> "No mapped value for '" + name + "(" + key + ")'"); if (!(prop instanceof Map)) { throw new IllegalArgumentException ("Non-mapped property for '" + name + "(" + key + ")'"); } - @SuppressWarnings("unchecked") - final // This is safe to cast because mapped properties are always // maps of types String -> Object - Map map = (Map) prop; + @SuppressWarnings("unchecked") + final Map map = (Map) prop; map.put(key, value); - } } + diff --git a/src/main/java/org/apache/commons/beanutils/ResultSetDynaClass.java b/src/main/java/org/apache/commons/beanutils/ResultSetDynaClass.java index 9ba2a2067..ca7e26926 100644 --- a/src/main/java/org/apache/commons/beanutils/ResultSetDynaClass.java +++ b/src/main/java/org/apache/commons/beanutils/ResultSetDynaClass.java @@ -20,6 +20,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.Iterator; +import java.util.Objects; /** *

Implements DynaClass for DynaBeans that wrap the @@ -153,11 +154,7 @@ public ResultSetDynaClass(final ResultSet resultSet, final boolean lowerCase) */ public ResultSetDynaClass(final ResultSet resultSet, final boolean lowerCase, final boolean useColumnLabel) throws SQLException { - - if (resultSet == null) { - throw new NullPointerException(); - } - this.resultSet = resultSet; + this.resultSet = Objects.requireNonNull(resultSet, "resultSet"); this.lowerCase = lowerCase; setUseColumnLabel(useColumnLabel); introspect(resultSet); diff --git a/src/main/java/org/apache/commons/beanutils/RowSetDynaClass.java b/src/main/java/org/apache/commons/beanutils/RowSetDynaClass.java index ecde68f57..b147508aa 100644 --- a/src/main/java/org/apache/commons/beanutils/RowSetDynaClass.java +++ b/src/main/java/org/apache/commons/beanutils/RowSetDynaClass.java @@ -21,6 +21,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** *

Implements {@link DynaClass} to create an in-memory collection @@ -198,17 +199,13 @@ public RowSetDynaClass(final ResultSet resultSet, final boolean lowerCase, final * @since 1.8.3 */ public RowSetDynaClass(final ResultSet resultSet, final boolean lowerCase, final int limit, final boolean useColumnLabel) - throws SQLException { - - if (resultSet == null) { - throw new NullPointerException(); - } + throws SQLException { + Objects.requireNonNull(resultSet, "resultSet"); this.lowerCase = lowerCase; this.limit = limit; setUseColumnLabel(useColumnLabel); introspect(resultSet); copy(resultSet); - } /** diff --git a/src/main/java/org/apache/commons/beanutils/converters/AbstractArrayConverter.java b/src/main/java/org/apache/commons/beanutils/converters/AbstractArrayConverter.java index 96dbe01fb..1ffb35720 100644 --- a/src/main/java/org/apache/commons/beanutils/converters/AbstractArrayConverter.java +++ b/src/main/java/org/apache/commons/beanutils/converters/AbstractArrayConverter.java @@ -22,6 +22,7 @@ import java.io.StringReader; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import org.apache.commons.beanutils.ConversionException; import org.apache.commons.beanutils.Converter; @@ -135,55 +136,41 @@ public AbstractArrayConverter(final Object defaultValue) { * is null */ protected List parseElements(String svalue) { - // Validate the passed argument - if (svalue == null) { - throw new NullPointerException(); - } - + Objects.requireNonNull(svalue, "svalue"); // Trim any matching '{' and '}' delimiters svalue = svalue.trim(); if (svalue.startsWith("{") && svalue.endsWith("}")) { svalue = svalue.substring(1, svalue.length() - 1); } - try { // Set up a StreamTokenizer on the characters in this String - final StreamTokenizer st = - new StreamTokenizer(new StringReader(svalue)); - st.whitespaceChars(',',','); // Commas are delimiters - st.ordinaryChars('0', '9'); // Needed to turn off numeric flag + final StreamTokenizer st = new StreamTokenizer(new StringReader(svalue)); + st.whitespaceChars(',', ','); // Commas are delimiters + st.ordinaryChars('0', '9'); // Needed to turn off numeric flag st.ordinaryChars('.', '.'); st.ordinaryChars('-', '-'); - st.wordChars('0', '9'); // Needed to make part of tokens + st.wordChars('0', '9'); // Needed to make part of tokens st.wordChars('.', '.'); st.wordChars('-', '-'); - // Split comma-delimited tokens into a List final ArrayList list = new ArrayList<>(); while (true) { final int ttype = st.nextToken(); - if (ttype == StreamTokenizer.TT_WORD || - ttype > 0) { + if (ttype == StreamTokenizer.TT_WORD || ttype > 0) { list.add(st.sval); } else if (ttype == StreamTokenizer.TT_EOF) { break; } else { - throw new ConversionException - ("Encountered token of type " + ttype); + throw new ConversionException("Encountered token of type " + ttype); } } - // Return the completed list return list; - } catch (final IOException e) { - throw new ConversionException(e); - } - } }