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

Martin Buchholz martinrb at google.com
Fri Mar 8 20:35:59 UTC 2019


On Fri, Mar 8, 2019 at 3:57 AM Tagir Valeev <amaembo at gmail.com> wrote:

> Hello!
>
> > 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
> > + "]");
>
> Won't it produce unnecessary garbage due to string concatenation on
> the common case when all frames are non-null?
>

I share Tagir's doubts.  I avoid this construction in performance sensitive
code.
Java doesn't offer syntactic abstraction (can I haz macros?) so the Java
Way is to write the explicit if.

Logging frameworks have the same trouble.
https://github.com/google/flogger

Perhaps hotspot's improved automatic NPE reporting makes the message detail
here obsolete?


More information about the core-libs-dev mailing list