I am trying to write a copy method which can be used to copy a bean to bean by mapping the property names. if the source object is a cloneable object m cloning it and trying to return it to the calling method. but in calling method it change made in the called method is not reflecting
you might understand if u look at the below code
public class Main {
public static void main(String[] args) {
HashMap<Object, Object> source = new HashMap<Object, Object>();
source.put("test1", "test1");
source.put("test2", "test2");
source.put("test3", "test3");
HashMap<Object, Object> destination = new HashMap<Object, Object>();
Main m = new Main();
Main.beanCopy(source, destination);
System.out.println("caller - " + destination);
}
public static void beanCopy(Object source, Object destination) {
if (source.getClass() == destination.getClass()
&& Cloneable.class.isInstance(source)) {
Class<? extends Object> cl = source.getClass();
try {
Method method = cl.getDeclaredMethod("clone");
destination = method.invoke(source);
System.out.println("callee - " + destination);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
the output of this program is
callee - {test1=test1, test2=test2, test3=test3}
caller - {}
I hope someone could help me to resolve this problem.. Thanks in advance...
-
*turns to class* And this, guys, is why Java is not pass-by-refernce and I'll rip your guts out if you keep saying so. </pseudoWit>user395760– user3957602011年04月11日 18:34:33 +00:00Commented Apr 11, 2011 at 18:34
-
I'll personally hunt down and shoot anyone who says it is. Does anybody really say that? Java is 100% pass-by-value. Some of the things it passes by value are integers, some are references to objects, but they are all passed by value.dty– dty2011年04月11日 18:37:58 +00:00Commented Apr 11, 2011 at 18:37
-
possible duplicate of 3000 other questions :-Dsubsub– subsub2011年04月11日 18:38:21 +00:00Commented Apr 11, 2011 at 18:38
4 Answers 4
Write the method as a function.
Object destination = Main.beanCopy(source);
public static Object beanCopy(Object source) {
return destination;
}
The problem you have with your approach is that you have to create an instance of the desired type so you know what to copy. If you want to pass the desired type as well.
MyType destination = Main.beanCopy(source, MyType.class);
public static <T> T beanCopy(Object source, Class<T> clazz) {
T destination;
return destination;
}
Note: Java does NOT support pass by reference. ;)
2 Comments
You can't change what destination refers to outside the scope of your method.
You would need to return the new object from your method.
Comments
If, instead of writing that insanely obfuscated code, you did this:
public static void beanCopy(Cloneable source, Object destination) {
if (source.getClass() == destination.getClass() {
destination = source.clone();
System.out.println("callee - " + destination);
}
}
Then the problem might be clearer.
2 Comments
instanceof and a cast - none of the ridiculous Class.isInstance() and reflection stuff is needed. Not that I'm telling you the answer - I'm just pointing out that if the code were simplified, then the problem might be clearer.In beanCopy, the destination variable holds a pointer to an Object. By assigning
destination = method.invoke(source);
that variable now holds a pointer to a different object.