found raw type, missing return arguments for gener

2019-08-05 15:06发布

问题:

I don't understand this warning:

found raw type: javax.swing.DefaultListModel
missing type arguements for generic class javax.swing.DefaultListModel

Netbeans seems to indicate that more information can be found from alt-enter but nothing comes up. The type should be ?

code:

package net.bounceme.dur.nntp.swing;

import java.util.logging.Logger;
import javax.swing.DefaultListModel;

public class MessagesListModel extends DefaultListModel {
    private static final long serialVersionUID = 1L;
    private static final Logger LOG = Logger.getLogger(MessagesListModel.class.getName());

    @Override
    @SuppressWarnings("unchecked")
    public void addElement(Object element) {
        super.addElement(element);
    }

}

回答1:

DefaultListModel is a generic type from Java 7.

You should do DefaultListModel<ClassName> instead of DefaultListModel.

Generally, this is more safe, since you specify what you should and what you shouldn't insert to the list. So if you made mistake, the compiler will arise an error.



回答2:

From Java 7 DefaultListModel is considered as Generic class. See DefaultListModel . And while extending it you are not providing generic type to the DefaultListModel that you are extending. That's why your IDE is warning you.
Either you could ignore this warning. or As alternative you provide some Type parameter to it like <Integer> <String> or anything.. for example.

public class MessagesListModel extends DefaultListModel<String>