Class Generics break completely seperate method

Posted by TheLQ on Stack Overflow See other posts from Stack Overflow or by TheLQ
Published on 2011-03-09T16:07:19Z Indexed on 2011/03/09 16:10 UTC
Read the original article Hit count: 242

Filed under:
|

I found a strange problem when I used class Generics Today: Setting some broke a completely separate method.

Here's a small example class that illustrates the problem. This code works just fine

public class Sandbox {
    public interface ListenerManagerTest {
        public Set<Listener> getListeners();
    }

    public void setListenerManager(ListenerManagerTest listenerManager) {
        for (Listener curListener : listenerManager.getListeners())
            return;
    }
}

Now as soon as I use class Generics, the getListeners() method returns Set<Object> instead of Set<Listener>

public class Sandbox {
    public interface ListenerManagerTest<E extends Object> {
        public Set<Listener> getListeners();
    }

    public void setListenerManager(ListenerManagerTest listenerManager) {
        for (Listener curListener : listenerManager.getListeners()) //Expected Listener, not Object
            return;
    }
}

What would cause this error? The ##java channel on Freenode said it was because of compile time candy and that I was using a raw type. But how would an raw class type break all generics in the class? And how would of worked before?

© Stack Overflow or respective owner

Related posts about java

Related posts about generics