L
Lethal Possum
Hello everyone,
Let's say I have a class B that extends a class A. Now I have an
instance of B and I need to "clone" it but only as an instance of A,
not B. I can't just cast my instance of B into A, I need a new
instance of A, and only A. I need that the new object's getClass()
method to return A.class.
Is there an easy way to do this? I'd prefer not to copy each field one
by one as there is many of them and maintenance would be difficult
(i.e. how do I make sure someone adding a field to class A will update
my method accordingly).
I know I could probably achieve this by reflection, iterating on every
field of class A, but some fields of A need to be copied in a specific
way. This is already done properly by the clone() method of A so I
would really like to leverage that code.
I am not sure my problem is very clear so I wrote a short piece of
code to demonstrate it. I also included all the solution that I
already know not to work:
== Start of code ==
public class A implements Cloneable {
private boolean _copy = false;
public boolean isCopy() {
return _copy;
}
public Object clone() throws CloneNotSupportedException {
A a = (A)super.clone();
a._cloned = true;
return a;
}
}
public class B extends A {
public A test1() throws Exception {
A a = (A)this;
return (A)a.clone();
}
public A test2() throws Exception {
A a = (A)clone();
return a;
}
public A test3() throws Exception {
A a = (A)super.clone();
return a;
}
public A test4() throws Exception {
A a = (A)clone();
return (A)a.clone();
}
}
public class test {
public static void main (String[] args) throws Exception {
B b = new B();
A a1 = b.test1();
System.out.println(a1.getClass().toString() + " copy=" +
a1.isCopy());
A a2 = b.test2();
System.out.println(a2.getClass().toString() + " copy=" +
a2.isCopy());
A a3 = b.test3();
System.out.println(a3.getClass().toString() + " copy=" +
a3.isCopy());
A a4 = b.test4();
System.out.println(a4.getClass().toString() + " copy=" +
a4.isCopy());
}
}
== End of code ==
All that I get is:
class B copy=true
When I would like to get:
class A copy=true
Any idea?
Thanks,
Tom
Let's say I have a class B that extends a class A. Now I have an
instance of B and I need to "clone" it but only as an instance of A,
not B. I can't just cast my instance of B into A, I need a new
instance of A, and only A. I need that the new object's getClass()
method to return A.class.
Is there an easy way to do this? I'd prefer not to copy each field one
by one as there is many of them and maintenance would be difficult
(i.e. how do I make sure someone adding a field to class A will update
my method accordingly).
I know I could probably achieve this by reflection, iterating on every
field of class A, but some fields of A need to be copied in a specific
way. This is already done properly by the clone() method of A so I
would really like to leverage that code.
I am not sure my problem is very clear so I wrote a short piece of
code to demonstrate it. I also included all the solution that I
already know not to work:
== Start of code ==
public class A implements Cloneable {
private boolean _copy = false;
public boolean isCopy() {
return _copy;
}
public Object clone() throws CloneNotSupportedException {
A a = (A)super.clone();
a._cloned = true;
return a;
}
}
public class B extends A {
public A test1() throws Exception {
A a = (A)this;
return (A)a.clone();
}
public A test2() throws Exception {
A a = (A)clone();
return a;
}
public A test3() throws Exception {
A a = (A)super.clone();
return a;
}
public A test4() throws Exception {
A a = (A)clone();
return (A)a.clone();
}
}
public class test {
public static void main (String[] args) throws Exception {
B b = new B();
A a1 = b.test1();
System.out.println(a1.getClass().toString() + " copy=" +
a1.isCopy());
A a2 = b.test2();
System.out.println(a2.getClass().toString() + " copy=" +
a2.isCopy());
A a3 = b.test3();
System.out.println(a3.getClass().toString() + " copy=" +
a3.isCopy());
A a4 = b.test4();
System.out.println(a4.getClass().toString() + " copy=" +
a4.isCopy());
}
}
== End of code ==
All that I get is:
class B copy=true
When I would like to get:
class A copy=true
Any idea?
Thanks,
Tom