Skip to content

Commit 76ec155

Browse files
committed
Fix edge-case NullPointerException in
org.apache.commons.lang3.EnumUtils.getEnum(Class, String, E) - Javadoc - Fix some formatting
1 parent 834e36a commit 76ec155

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ The <action> type attribute can be add,update,fix,remove.
9999
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix edge-case NullPointerException in org.apache.commons.lang3.SystemUtils.isJavaVersionAtLeast(JavaVersion).</action>
100100
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix edge-case NullPointerException in org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost(JavaVersion).</action>
101101
<action type="fix" dev="ggregory" due-to="Gary Gregory">Return the default enum if a SecurityException is caught in getEnumSystemProperty().</action>
102+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix edge-case NullPointerException in org.apache.commons.lang3.EnumUtils.getEnum(Class, String, E).</action>
102103
<!-- ADD -->
103104
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Strings and refactor StringUtils.</action>
104105
<action issue="LANG-1747" type="add" dev="ggregory" due-to="Oliver B. Fischer, Gary Gregory">Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier).</action>

src/main/java/org/apache/commons/lang3/EnumUtils.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,10 @@ public static <E extends Enum<E>> long[] generateBitVectors(final Class<E> enumC
192192
* <p>This method differs from {@link Enum#valueOf} in that it does not throw an exception
193193
* for an invalid enum name.</p>
194194
*
195-
* @param <E> the type of the enumeration
196-
* @param enumClass the class of the enum to query, not null
197-
* @param enumName the enum name, null returns null
198-
* @return the enum, null if not found
195+
* @param <E> the type of the enumeration.
196+
* @param enumClass the class of the enum to query, not null.
197+
* @param enumName the enum name, null returns null.
198+
* @return the enum, null if not found.
199199
*/
200200
public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName) {
201201
return getEnum(enumClass, enumName, null);
@@ -207,20 +207,20 @@ public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final Stri
207207
* <p>This method differs from {@link Enum#valueOf} in that it does not throw an exception
208208
* for an invalid enum name.</p>
209209
*
210-
* @param <E> the type of the enumeration
211-
* @param enumClass the class of the enum to query, not null
212-
* @param enumName the enum name, null returns default enum
213-
* @param defaultEnum the default enum
214-
* @return the enum, default enum if not found
210+
* @param <E> the type of the enumeration.
211+
* @param enumClass the class of the enum to query, null returns default enum.
212+
* @param enumName the enum name, null returns default enum.
213+
* @param defaultEnum the default enum.
214+
* @return the enum, default enum if not found.
215215
* @since 3.10
216216
*/
217217
public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName, final E defaultEnum) {
218-
if (enumName == null) {
218+
if (enumClass == null || enumName == null) {
219219
return defaultEnum;
220220
}
221221
try {
222222
return Enum.valueOf(enumClass, enumName);
223-
} catch (final IllegalArgumentException ex) {
223+
} catch (final IllegalArgumentException e) {
224224
return defaultEnum;
225225
}
226226
}
@@ -320,10 +320,8 @@ public static <E extends Enum<E>, K> Map<K, E> getEnumMap(final Class<E> enumCla
320320
* @return the enum, default enum if not found.
321321
* @since 3.13.0
322322
*/
323-
public static <E extends Enum<E>> E getEnumSystemProperty(final Class<E> enumClass, final String propName,
324-
final E defaultEnum) {
325-
return enumClass == null || propName == null ? defaultEnum
326-
: getEnum(enumClass, SystemProperties.getProperty(propName), defaultEnum);
323+
public static <E extends Enum<E>> E getEnumSystemProperty(final Class<E> enumClass, final String propName, final E defaultEnum) {
324+
return getEnum(enumClass, SystemProperties.getProperty(propName), defaultEnum);
327325
}
328326

329327
/**
@@ -365,11 +363,11 @@ public static <E extends Enum<E>> E getFirstEnum(final Class<E> enumClass, final
365363
*/
366364
public static <E extends Enum<E>> E getFirstEnumIgnoreCase(final Class<E> enumClass, final String enumName, final Function<E, String> stringFunction,
367365
final E defaultEnum) {
368-
if (enumName == null || !enumClass.isEnum()) {
369-
return defaultEnum;
370-
}
371-
return stream(enumClass).filter(e -> enumName.equalsIgnoreCase(stringFunction.apply(e))).findFirst().orElse(defaultEnum);
366+
if (enumName == null || !enumClass.isEnum()) {
367+
return defaultEnum;
372368
}
369+
return stream(enumClass).filter(e -> enumName.equalsIgnoreCase(stringFunction.apply(e))).findFirst().orElse(defaultEnum);
370+
}
373371

374372
private static <E extends Enum<E>> boolean isEnum(final Class<E> enumClass) {
375373
return enumClass != null && !enumClass.isEnum();
@@ -378,13 +376,14 @@ private static <E extends Enum<E>> boolean isEnum(final Class<E> enumClass) {
378376
/**
379377
* Checks if the specified name is a valid enum for the class.
380378
*
381-
* <p>This method differs from {@link Enum#valueOf} in that it checks if the name is
382-
* a valid enum without needing to catch the exception.</p>
379+
* <p>
380+
* This method differs from {@link Enum#valueOf} in that it checks if the name is a valid enum without needing to catch the exception.
381+
* </p>
383382
*
384-
* @param <E> the type of the enumeration
385-
* @param enumClass the class of the enum to query, not null
386-
* @param enumName the enum name, null returns false
387-
* @return true if the enum name is valid, otherwise false
383+
* @param <E> the type of the enumeration.
384+
* @param enumClass the class of the enum to query, null returns false.
385+
* @param enumName the enum name, null returns false.
386+
* @return true if the enum name is valid, otherwise false.
388387
*/
389388
public static <E extends Enum<E>> boolean isValidEnum(final Class<E> enumClass, final String enumName) {
390389
return getEnum(enumClass, enumName) != null;
@@ -393,14 +392,15 @@ public static <E extends Enum<E>> boolean isValidEnum(final Class<E> enumClass,
393392
/**
394393
* Checks if the specified name is a valid enum for the class.
395394
*
396-
* <p>This method differs from {@link Enum#valueOf} in that it checks if the name is
397-
* a valid enum without needing to catch the exception
398-
* and performs case insensitive matching of the name.</p>
395+
* <p>
396+
* This method differs from {@link Enum#valueOf} in that it checks if the name is a valid enum without needing to catch the exception and performs case
397+
* insensitive matching of the name.
398+
* </p>
399399
*
400-
* @param <E> the type of the enumeration
401-
* @param enumClass the class of the enum to query, not null
402-
* @param enumName the enum name, null returns false
403-
* @return true if the enum name is valid, otherwise false
400+
* @param <E> the type of the enumeration.
401+
* @param enumClass the class of the enum to query, null returns false.
402+
* @param enumName the enum name, null returns false.
403+
* @return true if the enum name is valid, otherwise false.
404404
* @since 3.8
405405
*/
406406
public static <E extends Enum<E>> boolean isValidEnumIgnoreCase(final Class<E> enumClass, final String enumName) {

src/test/java/org/apache/commons/lang3/EnumUtilsTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ void testGetEnum_defaultEnum() {
281281
assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, null, Traffic.GREEN));
282282
assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, null, Traffic.RED));
283283
assertNull(EnumUtils.getEnum(Traffic.class, "PURPLE", null));
284+
assertEquals(Traffic.AMBER, EnumUtils.getEnum(null, "RED", Traffic.AMBER));
284285
}
285286

286287
/**
@@ -296,7 +297,7 @@ void testGetEnum_nonEnumClass() {
296297

297298
@Test
298299
void testGetEnum_nullClass() {
299-
assertNullPointerException(() -> EnumUtils.getEnum((Class<Traffic>) null, "PURPLE"));
300+
assertNull(EnumUtils.getEnum((Class<Traffic>) null, "PURPLE"));
300301
}
301302

302303
@Test
@@ -467,7 +468,7 @@ void testIsValidEnum() {
467468

468469
@Test
469470
void testIsValidEnum_nullClass() {
470-
assertNullPointerException(() -> EnumUtils.isValidEnum(null, "PURPLE"));
471+
assertFalse(EnumUtils.isValidEnum(null, "PURPLE"));
471472
}
472473

473474
@Test

0 commit comments

Comments
 (0)