In business
org.seasar.framework.beans.util.Beans#copy
I sometimes use.
It's convenient, but I don't use it often, so I'm worried about the following two points each time I use it.
--Does an error occur if there is a property that exists in the copy source but not in the copy destination? --Is it an error if there is a property in the copy destination that is not in the copy source?
Javadoc looks like this about the above I didn't write it, so I tested it.
--Neither will result in an error. --Properties that are not in the source but in the destination are not affected (the pre-assigned values remain).
import org.seasar.framework.beans.util.Beans;
public class BeansTest {
/**Copy source class*/
public static class Src {
public String name;
public String hobby; //Properties not in Dest
}
/**Copy destination class*/
public static class Dest {
public String name;
public String address; //Properties not in Src
@Override
public String toString() {
return String.format("name=%s, address=%s", name, address);
}
}
public static void main(String[] args) {
Src src = new Src();
src.name = "Kozure Hanako";
src.hobby = "bicycle";
Dest dest = new Dest();
dest.address = "Chiyoda ward, Tokyo";
//Copy execution
Beans.copy(src, dest).execute();
System.out.println(dest); // -> name=Kozure Hanako, address=Chiyoda ward, Tokyo
}
}
Recommended Posts