All Downloads are FREE. Search and download functionalities are using the official Maven repository.

resources.report.rules.findbugs.WCMP_BAD_ASSIGNMENT.html Maven / Gradle / Ivy

Go to download

Sanity4J was created to simplify running multiple static code analysis tools on the Java projects. It provides a single entry point to run all the selected tools and produce a consolidated report, which presents all findings in an easily accessible manner.

The newest version!


WCMP: Bad Assignment (WCMP_BAD_ASSIGNMENT)


WCMP: Bad Assignment (WCMP_BAD_ASSIGNMENT)

A bad assignment to a WComponent instance field was detected.

Developers that are new to WComponents often create their own WComponents without understanding that a single instance of their component will be shared by many user sessions. For this "shared instance pattern" to work it is essential that an application's WComponent structure does not change after initial construction. If the structure does change, the application will almost surely fail in a multi-user environment.

Safety features to assist developers in sticking to the pattern include marking WComponent instance variables as private and final, and also calling the assertNotLocked() method before attempting to changing the internal state of a WComponent.

public class Example extends WPanel
{
    private WComponent label;
    private WComponent field;
    
    // Best - immutable field
    private final WButton button = new WButton("button");
   
    public Example()
    {
        // ok, guaranteed to not be updating a shared instance in a constructor
        label = new WLabel("Default");
        
        add(label);
        add(button);
    }

    public void setMyWComponent(MyOtherComponent otherComponent)
    {
        // Bad - should never alter another WComponent's internal state
        otherComponent.panel = this;
    }
    
    public void setLabel(WLabel label)
    {
        // Bad - unprotected access to a field.
        this.label = label;
    }
    
    public void setField(WInput field)
    {
        // Better. 
        
        // This will throw a StaticLockException if an attempt 
        // is made to update the shared instance after the initial set-up.
        assertNotLocked();
        
        this.field = field;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy