[
https://issues.apache.org/jira/browse/LANG-1804?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Zhongxin Yan updated LANG-1804:
-------------------------------
Description:
{code:java}
// source code
public static CharSet getInstance(final String... setStrs) {
if (setStrs == null) {
return null; // error
}
if (setStrs.length == 1) {
final CharSet common = COMMON.get(setStrs[0]);
if (common != null) {
return common;
}
}
return new CharSet(setStrs);
} {code}
The {{getInstance}} method of {{CharSet}} currently returns {{null}} when the
input parameter {{setStrs}} is {{{}null{}}}. This behavior contradicts the
documentation, which states that a {{null}} input should return the {{EMPTY}}
constant (an empty character set). Returning {{null}} can lead to
{{NullPointerException}} in calling code that expects a valid {{CharSet}}
instance even for empty inputs. [github
PR|https://github.com/apache/commons-lang/pull/1530]
The Javadoc and source code are as follows:
!image-2025-12-17-20-14-35-234.png|width=441,height=259!
{code:java}
// Test NullPointerException
@Test
void testGetInstance_Null_Fixed_ReturnsEmpty_NoNPE() {
CharSet charSet = CharSet.getInstance(null);
assertThrows(NullPointerException.class,()->charSet.contains('['));
} {code}
was:
{code:java}
// source code
public static CharSet getInstance(final String... setStrs) {
if (setStrs == null) {
return null; // error
}
if (setStrs.length == 1) {
final CharSet common = COMMON.get(setStrs[0]);
if (common != null) {
return common;
}
}
return new CharSet(setStrs);
} {code}
The {{getInstance}} method of {{CharSet}} currently returns {{null}} when the
input parameter {{setStrs}} is {{{}null{}}}. This behavior contradicts the
documentation, which states that a {{null}} input should return the {{EMPTY}}
constant (an empty character set). Returning {{null}} can lead to
{{NullPointerException}} in calling code that expects a valid {{CharSet}}
instance even for empty inputs.
The Javadoc and source code are as follows:
!image-2025-12-17-20-14-35-234.png|width=441,height=259!
{code:java}
// Test NullPointerException
@Test
void testGetInstance_Null_Fixed_ReturnsEmpty_NoNPE() {
CharSet charSet = CharSet.getInstance(null);
assertThrows(NullPointerException.class,()->charSet.contains('['));
} {code}
> Fix CharSet#getInstance returns null instead of EMPTY when input setStrs is
> null
> --------------------------------------------------------------------------------
>
> Key: LANG-1804
> URL: https://issues.apache.org/jira/browse/LANG-1804
> Project: Commons Lang
> Issue Type: Bug
> Components: lang.*
> Affects Versions: 3.20.0
> Reporter: Zhongxin Yan
> Priority: Major
> Attachments: image-2025-12-17-20-03-49-458.png,
> image-2025-12-17-20-12-40-410.png, image-2025-12-17-20-13-49-838.png,
> image-2025-12-17-20-14-35-234.png
>
>
>
> {code:java}
> // source code
> public static CharSet getInstance(final String... setStrs) {
> if (setStrs == null) {
> return null; // error
> }
> if (setStrs.length == 1) {
> final CharSet common = COMMON.get(setStrs[0]);
> if (common != null) {
> return common;
> }
> }
> return new CharSet(setStrs);
> } {code}
> The {{getInstance}} method of {{CharSet}} currently returns {{null}} when the
> input parameter {{setStrs}} is {{{}null{}}}. This behavior contradicts the
> documentation, which states that a {{null}} input should return the {{EMPTY}}
> constant (an empty character set). Returning {{null}} can lead to
> {{NullPointerException}} in calling code that expects a valid {{CharSet}}
> instance even for empty inputs. [github
> PR|https://github.com/apache/commons-lang/pull/1530]
> The Javadoc and source code are as follows:
> !image-2025-12-17-20-14-35-234.png|width=441,height=259!
>
> {code:java}
> // Test NullPointerException
> @Test
> void testGetInstance_Null_Fixed_ReturnsEmpty_NoNPE() {
> CharSet charSet = CharSet.getInstance(null);
> assertThrows(NullPointerException.class,()->charSet.contains('['));
> } {code}
>
>
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)