JDK 13 RFR of JDK-8220346: Refactor java.lang.Throwable to use Objects.requireNonNull

Joe Darcy joe.darcy at oracle.com
Fri Mar 8 11:08:57 UTC 2019


Hello,

The code in java.lang.Throwable has various explicit null checks that 
could be rewritten to use Objects.requireNonNull.

Please review the patch below which implements this refactoring.

Thanks,

-Joe

diff -r 274361bd6915 src/java.base/share/classes/java/lang/Throwable.java
--- a/src/java.base/share/classes/java/lang/Throwable.java    Thu Mar 07 
10:22:19 2019 +0100
+++ b/src/java.base/share/classes/java/lang/Throwable.java    Fri Mar 08 
02:06:42 2019 -0800
@@ -874,8 +874,7 @@
          // Validate argument
          StackTraceElement[] defensiveCopy = stackTrace.clone();
          for (int i = 0; i < defensiveCopy.length; i++) {
-            if (defensiveCopy[i] == null)
-                throw new NullPointerException("stackTrace[" + i + "]");
+            Objects.requireNonNull(defensiveCopy[i], "stackTrace[" + i 
+ "]");
          }

          synchronized (this) {
@@ -914,8 +913,7 @@
                  for (Throwable t : suppressedExceptions) {
                      // Enforce constraints on suppressed exceptions in
                      // case of corrupt or malicious stream.
-                    if (t == null)
-                        throw new NullPointerException(NULL_CAUSE_MESSAGE);
+                    Objects.requireNonNull(t, NULL_CAUSE_MESSAGE);
                      if (t == this)
                          throw new 
IllegalArgumentException(SELF_SUPPRESSION_MESSAGE);
                      suppressed.add(t);
@@ -942,8 +940,7 @@
                  stackTrace = null;
              } else { // Verify stack trace elements are non-null.
                  for(StackTraceElement ste : stackTrace) {
-                    if (ste == null)
-                        throw new NullPointerException("null 
StackTraceElement in serial stream. ");
+                    Objects.requireNonNull(ste, "null StackTraceElement 
in serial stream. ");
                  }
              }
          } else {
@@ -1034,8 +1031,7 @@
          if (exception == this)
              throw new 
IllegalArgumentException(SELF_SUPPRESSION_MESSAGE, exception);

-        if (exception == null)
-            throw new NullPointerException(NULL_CAUSE_MESSAGE);
+        Objects.requireNonNull(exception, NULL_CAUSE_MESSAGE);

          if (suppressedExceptions == null) // Suppressed exceptions not 
recorded
              return;



More information about the core-libs-dev mailing list