John Dalsgaard
John Dalsgaard

Reputation: 2807

Java and "Type Safety..."

I have a quick question about type safety

I have this method and a class variable defined:

private List<SelectItem> listBait = null;


public List<SelectItem> getListBait() {
    // Cache to avoid resorting and rebuilding list numerous times
    if (null == listBait) {
        listBait = ConfigurationBean.getCurrentInstance().getMeta().getListBait();
    }
    return listBait;
}

Now, I get a warning on the assignment in the if. It says Type safety: The expression of type List needs unchecked conversion to conform to List<SelectItem>.

First the getCurrentInstance method in ConfigurationBean is:

public static ConfigurationBean getCurrentInstance() {
    // This is a neat way to get a handle on the instance of this bean in the application scope from other Java code...
    FacesContext context = Util.getFacesContext();
    ConfigurationBean bean = (ConfigurationBean) context.getApplication().getVariableResolver().resolveVariable(context, "Configuration");
    return bean;
}

... and the getMeta method (and instance variable) is:

private final MetaDataBean meta = new MetaDataBean();

public MetaDataBean getMeta() {
    return meta;
}

The getListBait() method from the MetaDataBean looks like this:

public List<SelectItem> getListBait() {
    List<SelectItem> options = new ArrayList<SelectItem>();
    for (Bait bait : getAllBaits()) {
        if (!bait.isInActive()) {
            options.add(new SelectItem(bait.getKey(), bait.getName()));
        }
    }
    return options;
}

So from what I understand it should not give the warning in question...? Anyone, who can explain this to me - the suggested solutions don't seem to solve the problem (apart from the @SuppressWarning ;-) ).

This is on Java 1.6.

Thanks in advance!

Edit
... and going through this edit actually solved it!

What had happened was that probably some friendly advice from Eclipse "helped" me define the MetaDataBean like this:

public class MetaDataBean<whyFish> extends BaseBean implements Serializable {
:
:

... and that does not make sense. I can't tell when that little "" has been added to the declaration - but removing that made all the warnings disappear :-)

Thanks a lot!! - now I still trust a little in what Java I know so far ;-)

/John

Upvotes: 0

Views: 185

Answers (2)

Radiodef
Radiodef

Reputation: 37845

Since a raw type is the erasure of that type (4.8), a raw MetaDataBean returns a raw List from getListBait.

The solution is either to remove the generic type parameter from MetaDataBean or to not use a raw type, depending on whether the parameter is actually necessary.

Upvotes: 1

John Dalsgaard
John Dalsgaard

Reputation: 2807

As per the last edit. A generics had been added to one of the class definitions - probably by the friendly help of Eclipse (and obviously me accepting it without knowing exactly what I did!).

The excellent comments for further info helped find the wrong declaration :-)

Upvotes: 0

Related Questions