A
Ads
I need to replace a object a user is using with another object of same type:
ie:
//Here myObjectA is something
myFunction(myObjectA);
//now myObjectA is completly different
public void myFunction(MyObject myObjectA){
//creates a new MyObject using function I don't have access to
somehow set myObjectA to this new object
}
For various reason, we can't simply returning the new object from myFunction
(ie doing myObjectA = myFunction(myObjectA))
So what i've come up with is copying all the fields from one object into the
other:
as a function inside MyObject (using reflection for maintenance reasons):
public void copyInto(MyObject obj){
Field [] fields = Main.class.getDeclaredFields();
try {
for (int i = 0; i < fields.length; i++) {
fields.set(this, fields.get(obj));
System.out.println(fields.getName());
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
This code seems to work, but does it have any flaws I havn't noticed yet?
Also, there is a field called class$packagename$MyObject , what is this
field?
Any help appreciated
Ads
ie:
//Here myObjectA is something
myFunction(myObjectA);
//now myObjectA is completly different
public void myFunction(MyObject myObjectA){
//creates a new MyObject using function I don't have access to
somehow set myObjectA to this new object
}
For various reason, we can't simply returning the new object from myFunction
(ie doing myObjectA = myFunction(myObjectA))
So what i've come up with is copying all the fields from one object into the
other:
as a function inside MyObject (using reflection for maintenance reasons):
public void copyInto(MyObject obj){
Field [] fields = Main.class.getDeclaredFields();
try {
for (int i = 0; i < fields.length; i++) {
fields.set(this, fields.get(obj));
System.out.println(fields.getName());
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
This code seems to work, but does it have any flaws I havn't noticed yet?
Also, there is a field called class$packagename$MyObject , what is this
field?
Any help appreciated
Ads