[Java:S2384] Don't complain about unmodifiable collections

If an instance is knowingly unmodifiable, S2384 should not complain when returned in a getter.

Of course it is not always possible to tell, whether the instance is actually unmodifiable. But if done the standard way, it should be reflected.

public class A
{
    private final List<String> items1 = new ArrayList<>();
    private final List<String> items1Unmod = Collections.unmodifiableList( items1 );
    private List<String> items2 = null;
    private List<String> items2Unmod = null;
    
    public void addItem1( String item )
    {
        items1.add( item );
    }
    
    public void addItem2( String item )
    {
        if ( items2 == null )
        {
            items2 = new ArrayList<>();
            items2Unmod = Collections.unmodifiableList( items2 );
        }
        
        items2.add( item );
    }
    
    public List< String > getItems1()
    {
        return items1Unmod; // FP
    }
    
    public List< String > getItems2()
    {
        return items2Unmod; // FP
    }
}

Hi Marvin,

Thanks for your feedback. Created a ticket.

Regards,
Margarita

Hi Margarita,

nice! Thanks.

Cheers
Marvin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.