This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 8876abff1 Refactor ExceptionUtils to share throwable search logic
8876abff1 is described below

commit 8876abff15187b992a540dbadb6ad38f5be0ec8a
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Oct 25 10:01:05 2026 -0400

    Refactor ExceptionUtils to share throwable search logic
    
    Extract a common search helper for indexOf() and throwableOf(),
    preserving null handling, starting indexes, and exact or subclass
    matching. Simplify isChecked() by delegating to isUnchecked().
---
 .../commons/lang3/exception/ExceptionUtils.java    | 62 ++++++++--------------
 1 file changed, 21 insertions(+), 41 deletions(-)

diff --git 
a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java 
b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
index 566a44f50..af91e29b0 100644
--- a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
+++ b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
@@ -582,28 +582,26 @@ public static boolean hasCause(Throwable chain,
      * @param subclass  if {@code true}, compares with {@link 
Class#isAssignableFrom(Class)}, otherwise compares using references.
      * @return index of the {@code type} within throwables nested within the 
specified {@code throwable}.
      */
-    private static int indexOf(final Throwable throwable, final Class<? 
extends Throwable> type, int fromIndex, final boolean subclass) {
+    private static int indexOf(final Throwable throwable, final Class<? 
extends Throwable> type, final int fromIndex, final boolean subclass) {
         if (throwable == null || type == null) {
             return NOT_FOUND;
         }
-        if (fromIndex < 0) {
-            fromIndex = 0;
-        }
-        final Throwable[] throwables = getThrowables(throwable);
-        if (fromIndex >= throwables.length) {
-            return NOT_FOUND;
-        }
-        if (subclass) {
-            for (int i = fromIndex; i < throwables.length; i++) {
-                if (type.isAssignableFrom(throwables[i].getClass())) {
-                    return i;
-                }
-            }
-        } else {
-            for (int i = fromIndex; i < throwables.length; i++) {
-                if (type.equals(throwables[i].getClass())) {
-                    return i;
-                }
+        return indexOf(getThrowables(throwable), type, fromIndex, subclass);
+    }
+
+    /**
+     * Finds the first matching throwable in an array.
+     *
+     * @param throwables The throwable chain to search.
+     * @param type The type to search for.
+     * @param fromIndex The starting index, negative treated as zero.
+     * @param subclass Whether subclasses match.
+     * @return The index of the first match, or {@code -1} if none is found.
+     */
+    private static int indexOf(final Throwable[] throwables, final Class<? 
extends Throwable> type, final int fromIndex, final boolean subclass) {
+        for (int i = Math.max(0, fromIndex); i < throwables.length; i++) {
+            if (subclass ? type.isInstance(throwables[i]) : 
type.equals(throwables[i].getClass())) {
+                return i;
             }
         }
         return NOT_FOUND;
@@ -696,7 +694,7 @@ public static int indexOfType(final Throwable throwable, 
final Class<? extends T
      * @since 3.13.0
      */
     public static boolean isChecked(final Throwable throwable) {
-        return throwable != null && !(throwable instanceof Error) && 
!(throwable instanceof RuntimeException);
+        return throwable != null && !isUnchecked(throwable);
     }
 
     /**
@@ -962,31 +960,13 @@ public static Stream<Throwable> stream(final Throwable 
throwable) {
      * using references.
      * @return throwable of the {@code type} within throwables nested within 
the specified {@code throwable}.
      */
-    private static <T extends Throwable> T throwableOf(final Throwable 
throwable, final Class<T> type, int fromIndex, final boolean subclass) {
+    private static <T extends Throwable> T throwableOf(final Throwable 
throwable, final Class<T> type, final int fromIndex, final boolean subclass) {
         if (throwable == null || type == null) {
             return null;
         }
-        if (fromIndex < 0) {
-            fromIndex = 0;
-        }
         final Throwable[] throwables = getThrowables(throwable);
-        if (fromIndex >= throwables.length) {
-            return null;
-        }
-        if (subclass) {
-            for (int i = fromIndex; i < throwables.length; i++) {
-                if (type.isAssignableFrom(throwables[i].getClass())) {
-                    return type.cast(throwables[i]);
-                }
-            }
-        } else {
-            for (int i = fromIndex; i < throwables.length; i++) {
-                if (type.equals(throwables[i].getClass())) {
-                    return type.cast(throwables[i]);
-                }
-            }
-        }
-        return null;
+        final int index = indexOf(throwables, type, fromIndex, subclass);
+        return index == NOT_FOUND ? null : type.cast(throwables[index]);
     }
 
     /**

Reply via email to