1

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...

Oswald
31.8k3 gold badges46 silver badges72 bronze badges
asked Apr 11, 2011 at 18:28
3
  • *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> Commented 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. Commented Apr 11, 2011 at 18:37
  • possible duplicate of 3000 other questions :-D Commented Apr 11, 2011 at 18:38

4 Answers 4

1

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. ;)

answered Apr 11, 2011 at 18:32
Sign up to request clarification or add additional context in comments.

2 Comments

the source and destination objects may be of diffrent class
Then you are not just copying, but you can use the second example to return a different type.
0

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.

answered Apr 11, 2011 at 18:33

Comments

0

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.

answered Apr 11, 2011 at 18:40

2 Comments

the problem is the source may or may not be Cloneable .. more code has to be added in tht function
Even then, it's just an 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.
0

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.

Java is Pass-by-Value, Dammit!

answered Apr 11, 2011 at 18:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.