clones, generics and unchecked cast warnings

Discussion in 'Java' started by rwfields@yahoo.com, Apr 23, 2006.

  1. Guest

    Hello,

    Here is the code:

    import java.util.Stack;

    public class Clone implements Cloneable {

    Stack<Object> stack = new Stack<Object>();

    public Clone() {
    }

    public Object clone() throws CloneNotSupportedException {
    Clone rv = (Clone) super.clone();
    rv.stack = (Stack<Object>) stack.clone();
    return rv;
    }

    public static void main (String[] args) throws
    CloneNotSupportedException {
    System.out.println("hello world!");
    Clone a = new Clone();
    Clone b = (Clone) a.clone();
    }
    }

    It seems like the explicit cast should prevent the warning, but it does
    not. Without the explicit cast, the code generates a compile error. I
    can live with warnings, but it sure seems like what I am trying to do
    is valid. Did the language designers (1.5) miss this on accident, or
    is there something simple that I am missing? Does anybody have any
    thoughts?

    Thanks,
    Randall
     
    , Apr 23, 2006
    #1
    1. Advertising

  2. hiwa Guest

    Since Java generics is based on the 'erasure' mechanism,
    this cast and getting a warning is always unavoidable.

    Sun gives us a small solace called SuppressWarnings
    annotation.

    For your (Clone) casting for clone() return value, I personally
    think Java cloning has failed to keep step with generics and
    it is a major bug.
     
    hiwa, Apr 23, 2006
    #2
    1. Advertising

  3. Piotr Kobzda Guest

    wrote:

    > It seems like the explicit cast should prevent the warning, but it does
    > not. Without the explicit cast, the code generates a compile error. I
    > can live with warnings, but it sure seems like what I am trying to do
    > is valid. Did the language designers (1.5) miss this on accident, or
    > is there something simple that I am missing? Does anybody have any
    > thoughts?


    Covariant return types introduced in Java 5 allows you to write a method
    whose return type is a subclass of the type returned by the method with
    the same signature in the superclass.

    Thus your clone() method would be declared as:

    public Clone clone() { ...

    Which allows for usage like:

    Clone a = new Clone();
    Clone b = a.clone();


    BTW -- Declaring CloneNotSupportedException with a custom clone() method
    when a class implements Cloneable is impractical /will never happen for
    such a class/, and should be handled in that method.


    Regards,
    piotr
     
    Piotr Kobzda, Apr 23, 2006
    #3
  4. hiwa Guest

    > Covariant return types introduced in Java 5 allows you to write a method
    > whose return type is a subclass of the type returned by the method with
    > the same signature in the superclass.
    >
    > Thus your clone() method would be declared as:
    >
    > public Clone clone() { ...
    >
    > Which allows for usage like:
    >
    > Clone a = new Clone();
    > Clone b = a.clone();

    It works. Thanks. But I can't find any article(s) on
    covariant return type in the JDK 1.5 standard
    documentation set. I've found this article on
    bugdatabase:
    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6349937
     
    hiwa, Apr 23, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Buroff
    Replies:
    5
    Views:
    5,511
    Alan Moore
    Sep 28, 2004
  2. Mark Maloof
    Replies:
    6
    Views:
    919
  3. Eric I.
    Replies:
    6
    Views:
    674
    Eric I.
    Aug 15, 2008
  4. RVic
    Replies:
    19
    Views:
    1,399
  5. Mayeul
    Replies:
    2
    Views:
    3,387
Loading...

Share This Page