F
f
Suppose I have
class Data1{
public String name;
public String address;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address = address;
}
}
class Data2{
public String alis;
public String port;
public String getAlis(){
return alis;
}
public void setAlis(String alis){
this.alis = alis;
}
public String getPort(){
return port;
}
public void setPort(String port){
this.port = port;
}
}
class Data3{
public String str1;
public String str1;
public String getStr1(){
return str1;
}
public void setStr1(String str1){
this.str1 = str1;
}
public String getStr2(){
return str2;
}
public void setStr2(String str2){
this.str2 = str2;
}
}
Data1 data1;
Data2 data2;
Data3 data3;
I want to set the attributes in Data3 by the attributes from Data1
or/and Data2. Thus, sometimes I want to do this:
data3.str1 = data2.alis;
data3.str2 = data1.name;
Sometimes I want to do this:
data3.str1 = data1.address;
data3.str2 = data1.name;
Sometimes I want to do this:
data3.str1 = data2.alis;
data3.str2 = data2.port;
....
There is a lot of possible transformations, but at run time, there is
only one used. If I specifiy the mapping in some way, for example, a
xml
<mapping>
<str1 source="data1" attribue="name">
<str2 source="data2" attribue="alis">
</mapping>
My program will read this xml at runtime and do the appropriate
transformation.
Thus, my code will be short. If there a way to do this?
I know I can do it with reflection, how is the perfermance? Any other
ways?
Thanks,
qq
class Data1{
public String name;
public String address;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address = address;
}
}
class Data2{
public String alis;
public String port;
public String getAlis(){
return alis;
}
public void setAlis(String alis){
this.alis = alis;
}
public String getPort(){
return port;
}
public void setPort(String port){
this.port = port;
}
}
class Data3{
public String str1;
public String str1;
public String getStr1(){
return str1;
}
public void setStr1(String str1){
this.str1 = str1;
}
public String getStr2(){
return str2;
}
public void setStr2(String str2){
this.str2 = str2;
}
}
Data1 data1;
Data2 data2;
Data3 data3;
I want to set the attributes in Data3 by the attributes from Data1
or/and Data2. Thus, sometimes I want to do this:
data3.str1 = data2.alis;
data3.str2 = data1.name;
Sometimes I want to do this:
data3.str1 = data1.address;
data3.str2 = data1.name;
Sometimes I want to do this:
data3.str1 = data2.alis;
data3.str2 = data2.port;
....
There is a lot of possible transformations, but at run time, there is
only one used. If I specifiy the mapping in some way, for example, a
xml
<mapping>
<str1 source="data1" attribue="name">
<str2 source="data2" attribue="alis">
</mapping>
My program will read this xml at runtime and do the appropriate
transformation.
Thus, my code will be short. If there a way to do this?
I know I can do it with reflection, how is the perfermance? Any other
ways?
Thanks,