Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaking this in constructor

The Controller class is a singleton, which seems to be a special case allowing for safely passing this to Controller.

Netbeans gives

Configure "passing suspicious parameters in the constructor" hint

for controller.addObserver(this); which makes me ask what the better technique would be, although I gather it's not a good approach.

package net.bounceme.dur.usenet.swing;

import java.util.Observable;
import java.util.Observer;
import java.util.logging.Logger;
import javax.mail.Folder;
import javax.swing.ListModel;
import net.bounceme.dur.usenet.controller.Controller;
import net.bounceme.dur.usenet.controller.MessageBean;
import net.bounceme.dur.usenet.controller.MessagesDefaultListModel;

public class MessageSelect extends javax.swing.JPanel implements Observer {

    private static final Logger LOG = Logger.getLogger(MessageSelect.class.getName());
    private Controller controller = Controller.getInstance();
    private ListModel messages = new MessagesDefaultListModel();
    private MessageBean messageBean = new MessageBean();

    @SuppressWarnings("unchecked")
    public MessageSelect() {
        controller.addObserver(this);
        initComponents();
        messagesJList.setPrototypeCellValue("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    }
like image 696
Thufir Avatar asked Jul 12 '26 14:07

Thufir


1 Answers

You are passing this to an external class (Controller) when the object hasn't been fully constructed. Controller could then reference your object while its construction hasn't finished.

Most people work around this by using a factory method which creates the object first, then passes this externally.

// private to force clients to use the static factory method
private MessageSelect() {
  initComponents();
  messagesJList.setPrototypeCellValue("xxx");
}

public static MessageSelect createInstance() {
  MessageSelect instance = new MessageSelect();
  instance.controller.addObserver(instance);
  return instance;
}

Take a look at this excellent Brian Goetz article on safe object construction.

like image 76
Steve Kuo Avatar answered Jul 15 '26 05:07

Steve Kuo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!