RFR: 8273329: Remove redundant null check from String.getBytes(String charsetName)
Sergey Bylokhov
serb at openjdk.java.net
Fri Sep 3 19:28:04 UTC 2021
On Fri, 3 Sep 2021 17:37:08 GMT, Sergey Bylokhov <serb 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);
>> }
>> }
>
> In such cases when the specific exception throwing is removed from the method because it can be produced by some other used method, the test might be useful. So if the code in the method will be changed, or the usage of other method will be removed the exception still be thrown. Probably such test exists already, then just point to it here.
> @mrserb you are right, there's such test, see `/test/jdk/java/lang/String/Exceptions.getBytes()`, line 384.
Thank you for confirmation, looks fine.
-------------
PR: https://git.openjdk.java.net/jdk/pull/5361
More information about the core-libs-dev
mailing list