Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The constructor for LicenseManager initializes licenseMap with a demo license key which is meant to be kept secret. The license key is hardcoded hard-coded for illustrative purposes and should ideally be read from an external configuration file that stores its encrypted version; see MSC66-JG. Store passwords using a hash function for more information. The LicenseType class provides overridden implementations of equals() and hashCode() methods.

...

This noncompliant code example consists of a Widget class and a LayoutManager class containing a set of widgets;:

Code Block
bgColor#ffcccc
public class Widget {
    private int noOfComponents;
    public Widget(int noOfComponents) {
        this.noOfComponents = noOfComponents;
    }
    public int getNoOfComponents() {
        return noOfComponents;
    }
    public final void setNoOfComponents(int noOfComponents) {
        this.noOfComponents = noOfComponents;
    }
    public boolean equals(Object o) {
        if (o == null || !(o instanceof Widget)) {
            return false;
        }
        Widget widget = (Widget) o;
        return this.noOfComponents == widget.getNoOfComponents();
    }
    @Override
    public int hashCode() {
        int res = 31;
        res = res * 17 + noOfComponents;
        return res;
    }
}
public class LayoutManager {
    private Set<Widget> layouts = new HashSet<Widget>();
    public void addWidget(Widget widget) {
        if (!layouts.contains(widget)) {
            layouts.add(widget);
        }
    }
    public int getLayoutSize() {
        return layouts.size();
    }
}

...

In this noncompliant code example, class Worker and its subclass SubWorker each contain a startThread()method intended to start a thread.:

Code Block
bgColor#ffcccc
public class Worker implements Runnable {
    Worker() { }
    public void startThread(String name) {
        new Thread(this, name).start();
    }
    @Override
    public void run() {
        System.out.println("Parent");
    }
}

public class SubWorker extends Worker {
    @Override
    public void startThread(String name) {
        super.startThread(name);
		new Thread(this, name).start();        
    }
    @Override
    public void run() {
        System.out.println("Child");
    }
}

...

This compliant solution modifies the SubWorker class and removes the call to super.startThread():

Code Block
bgColor#ccccff
public class SubWorker extends Worker {
    @Override
    public void startThread(String name) {
        new Thread(this, name).start();
    }
   // ...
}

...