Results 1 to 8 of 8
  1. #1
    dhafirnz is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default Strange Compilation Error About Generics

    Hi all,
    I'm getting a strange type casting compilation error for what seems to be a simple case.

    Have the following class:

    class GenericResult<T> implements Serializable {

    private List<Message> messages = new ArrayList<Message>();
    private T data;

    public T getData() {
    return data;
    }


    public void setData(T data) {
    this.data = data;
    }


    if I don't specify a type for the class above when creating it (which is valid with a warning) the compiler converts the List of <Messages> to a List of <Object> and I lose the type although the List has nothing to do with the Class type parameter!

    So, if I go like this:

    GenericResult myResult = new GenericResult();
    for(Message msg: myResult.getMessages() ) {
    ...
    }

    This will create compilation error on the for loop saying it cannot convert from Object to Message, although the list is defined as Message type!

    If I specify any dummy type when creating the class above (say like new GenericResult<Date>()) then the compiler will be happy!

    Note that any other non-List property will maintain its type regardless, it seems that the Collection has this problem?

    Can someone explain please?

    Thanks,
    Last edited by dhafirnz; 11-15-2010 at 11:46 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    Curious.
    I'm sure Jos can explain why...

    (Tolls waits patiently)

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Show us the code for your getMessages method; does it return something of type List<Message>?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    dhafirnz is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Hi,
    Yes, the getter/setter all typed.



    public List<Message> getMessages() {
    return messages;
    }

    public void setMessages(List<Message> messages) {
    this.messages = messages;
    }


    Thanks for any help in solving this mystery. I am using Java 1.6 by the way.
    Last edited by dhafirnz; 11-15-2010 at 09:23 PM.

  5. #5
    dhafirnz is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default just guessing..

    My guess is that because a type was not bound to the generic type, the compiler downgrades the byte code to a pre Java 5 version (like 1.4), and hence a typed List will loose its type!

    Very confusing! Why would it do that?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by dhafirnz View Post
    My guess is that because a type was not bound to the generic type, the compiler downgrades the byte code to a pre Java 5 version (like 1.4), and hence a typed List will loose its type!

    Very confusing! Why would it do that?
    Legacy support; read paragraph 4.8 Raw Types in the JLS; any member of a parameterized type that is also parameterized (no matter if the types are the same as the parameter type of the class you're defining) will be 'erased' to its raw type to avoid 'rare' types. Personally I find it a bit rude but I can see the problems that arise if the erasure isn't complete. They also sort of warn that raw types may not be supported in the future. Interesting, I never thought about it until I read your example; thanks for that ...

    kind regards,

    Jos

    ps. Both javacc and jide (the Eclipse compiler) do this; I don't know about other compilers.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    "Personally I find it a bit rude"
    :)

    That made me chuckle.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    "Personally I find it a bit rude"
    :)

    That made me chuckle.
    I wanted to write: "such a shitty solution can only have come from the brains of a no balls moron"; but this is a family forum and kids can read it so I didn't write it ... ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. bean compilation error
    By technical_helps@yahoo.com in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 07-30-2009, 12:21 AM
  2. java.lang.Error: Unresolved compilation problems
    By jon80 in forum New To Java
    Replies: 0
    Last Post: 06-07-2009, 11:04 PM
  3. JAVA compilation error in UNIX
    By satish kumar in forum Advanced Java
    Replies: 9
    Last Post: 08-08-2008, 08:36 AM
  4. compilation error(version problem?)
    By Ms.Ranjan in forum New To Java
    Replies: 3
    Last Post: 07-11-2008, 05:31 PM
  5. compilation error with Jcreator
    By Heather in forum JCreator
    Replies: 2
    Last Post: 06-30-2007, 05:12 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •