RFR: 8273329: Remove redundant null check from String.getBytes(String charsetName)
Roger Riggs
rriggs at openjdk.java.net
Fri Sep 3 14:07:25 UTC 2021
On Fri, 3 Sep 2021 13:22:54 GMT, Сергей Цыпанов <github.com+10835776+stsypanov at openjdk.org> wrote:
> Current implementation looks like this:
>
> public byte[] getBytes(String charsetName)
> throws UnsupportedEncodingException {
> if (charsetName == null) throw new NullPointerException();
> return encode(lookupCharset(charsetName), coder(), value);
> }
>
> Null check seems to be redundant here because the same check of `charsetName` is done within `String.lookupCharset(String)`:
>
> private static Charset lookupCharset(String csn) throws UnsupportedEncodingException {
> Objects.requireNonNull(csn);
> try {
> return Charset.forName(csn);
> } catch (UnsupportedCharsetException | IllegalCharsetNameException x) {
> throw new UnsupportedEncodingException(csn);
> }
> }
Redundant null checks get collapsed by HotSpot, so not a performance improvement.
Having null checks at public entry points also shows a stack trace that is more specific about where the null came from. So not much value in changing this.
-------------
Marked as reviewed by rriggs (Reviewer).
PR: https://git.openjdk.java.net/jdk/pull/5361
More information about the core-libs-dev
mailing list