You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Partha Paul (Jira)" <ji...@apache.org> on 2026/03/27 23:41:00 UTC

[jira] [Updated] (COLLECTIONS-886) CompositeSet.addComposited(Set) does not throw NullPointerException when null is passed, which contradicts current Javadoc comment

     [ https://issues.apache.org/jira/browse/COLLECTIONS-886?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Partha Paul updated COLLECTIONS-886:
------------------------------------
    Description: 
The Javadoc for {{CompositeSet.addComposited(Set)}} explicitly states that a {{NullPointerException}} should be thrown when the {{set}} parameter is {{{}null{}}}. However, the current implementation contains a null guard ( {{if set != null}} ) that silently ignores null input and doesn't throw an exception.
h5. Javadoc (Current) contains
{code:java}
@throws NullPointerException if {@code set} is null
{code}
h5. Implementation (Current)
{code:java}
public synchronized void addComposited(final Set<E> set) {
       if (set != null) {
       ...
       all.add(set);
    }
}
{code}
There also exist a test code {{org.apache.commons.collections4.set.CompositeSetTest.testAddComposited}} which adds {{null}} to the {{CompositeSet}} using this method.

h5. Steps to Reproduce
{code:java}
@Test
void testNullPointerExceptionOnAddCompositedWithNullSet() {
     CompositeSet<String> set = new CompositeSet<>(new HashSet<>());
     assertThrows(NullPointerException.class, () -> set.addComposited(null));
}
{code}
The test above will fail because no {{NullPointerException}} is thrown.

Happy to submit pr by fixing the javadoc by removing the {{NullPointerException}} clause!

  was:
The Javadoc for {{CompositeSet.addComposited(Set)}} explicitly states that a {{NullPointerException}} should be thrown when the {{set}} parameter is {{{}null{}}}. However, the current implementation contains a null guard ( {{if set != null}} ) that silently ignores null input and doesn't throw an exception.
h5. Javadoc (Current) contains
{code:java}
@throws NullPointerException if {@code set} is null
{code}
h5. Implementation (Current)
{code:java}
public synchronized void addComposited(final Set<E> set) {
       if (set != null) {
       ...
       all.add(set);
    }
}
{code}

h5. Steps to Reproduce
{code:java}
@Test
void testNullPointerExceptionOnAddCompositedWithNullSet() {
     CompositeSet<String> set = new CompositeSet<>(new HashSet<>());
     assertThrows(NullPointerException.class, () -> set.addComposited(null));
}
{code}
The test above will fail because no {{NullPointerException}} is thrown.

Happy to submit pr by fixing the javadoc by removing the {{NullPointerException}} clause!


> CompositeSet.addComposited(Set) does not throw NullPointerException when null is passed, which contradicts current Javadoc comment
> ----------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: COLLECTIONS-886
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-886
>             Project: Commons Collections
>          Issue Type: Bug
>    Affects Versions: 4.5.0
>         Environment: commons-collections: 4.5.0 (and previous versions)
> java: 17.0.12
>            Reporter: Partha Paul
>            Priority: Major
>              Labels: docuentation, documentation-update, implementation
>
> The Javadoc for {{CompositeSet.addComposited(Set)}} explicitly states that a {{NullPointerException}} should be thrown when the {{set}} parameter is {{{}null{}}}. However, the current implementation contains a null guard ( {{if set != null}} ) that silently ignores null input and doesn't throw an exception.
> h5. Javadoc (Current) contains
> {code:java}
> @throws NullPointerException if {@code set} is null
> {code}
> h5. Implementation (Current)
> {code:java}
> public synchronized void addComposited(final Set<E> set) {
>        if (set != null) {
>        ...
>        all.add(set);
>     }
> }
> {code}
> There also exist a test code {{org.apache.commons.collections4.set.CompositeSetTest.testAddComposited}} which adds {{null}} to the {{CompositeSet}} using this method.
> h5. Steps to Reproduce
> {code:java}
> @Test
> void testNullPointerExceptionOnAddCompositedWithNullSet() {
>      CompositeSet<String> set = new CompositeSet<>(new HashSet<>());
>      assertThrows(NullPointerException.class, () -> set.addComposited(null));
> }
> {code}
> The test above will fail because no {{NullPointerException}} is thrown.
> Happy to submit pr by fixing the javadoc by removing the {{NullPointerException}} clause!



--
This message was sent by Atlassian Jira
(v8.20.10#820010)