commons-lang icon indicating copy to clipboard operation
commons-lang copied to clipboard

[LANG-1804] Fix CharSet#getInstance returns null instead of EMPTY when input setStrs is null

Open IcoreE opened this issue 1 month ago • 2 comments

// 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);
} 

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

// Test NullPointerException
    @Test
    void testGetInstance_Null_Fixed_ReturnsEmpty_NoNPE() {
        CharSet charSet = CharSet.getInstance(null);
        assertThrows(NullPointerException.class,()->charSet.contains('['));
    } 

IcoreE avatar Dec 17 '25 12:12 IcoreE

@IcoreE If you don't run mvn by itself before pushing, you'll never catch build errors like the ones that just occurred (see the instructions in the PR template).

garydgregory avatar Dec 17 '25 12:12 garydgregory

Jira: https://issues.apache.org/jira/browse/LANG-1804

garydgregory avatar Dec 17 '25 13:12 garydgregory