オブジェクトのコレクション (タグ) から値を読み取り、リフレクションを使用して jaxb 生成オブジェクトにマップしようとしています。メイン オブジェクトから必要なフィールドをフィルタリングするフィールド リストがあります。
これがサンプルコードです。値を抽出する必要があるリストとして、内部クラス Tag を使用しています。List fieldList はフィルターとして機能します。目標は、リフレクションを使用してこれらの値を jaxb 生成オブジェクトにコピーすることです。これを実現するために、javabean PropertyDescriptor を使用しています。
現在、jaxb オブジェクトのコレクション フィールドに値を設定するという問題に悩まされています。List フィールドの場合、jaxb はセッターを提供しないため、getList().add(object) を使用する必要があります。どうすればこれを達成できますか?
public class ReflectionTest2 {
/**
* @param args
*/
public static void main(String[] args) {
try{
ReflectionTest2 testObj = new ReflectionTest2();
List<Tag> tagList = new ArrayList<Tag>();
createTag(tagList, testObj);
List<String> fieldList = new ArrayList<String>();
fieldList.add("ADSKContentGroup");
fieldList.add("title");
CaasContextObject object = new CaasContextObject();
for(Tag each : tagList){
innerLoop:
for(String field : fieldList){
if(each.name.equals(field)){
for (PropertyDescriptor pd : Introspector.getBeanInfo(
CaasContextObject.class).getPropertyDescriptors()) {
if(pd.getWriteMethod() != null && !"class".equals(pd.getName())){
if(pd.getName().equals(each.name)){
if(pd.getPropertyType().isAssignableFrom(java.util.List.class)){
// handle list
}else{
pd.getWriteMethod().invoke(object, each.value);
}
break innerLoop;
}
}
}
}
}
}
// Read object
System.out.println("\n\nPrinting Title -->"+object.getTitle());
for(String cg : object.getADSKContentGroup()){
System.out.println(cg);
}
}catch (Exception e) {
e.printStackTrace();
}
}
private static void createTag(List<Tag> tagList, ReflectionTest2 obj){
Tag tag1 = obj.new Tag();
tag1.setName("ADSKContentGroup");
tag1.setValue("Documentation");
tagList.add(tag1);
Tag tag2 = obj.new Tag();
tag2.setName("ADSKContentGroup");
tag2.setValue("User Guide");
tagList.add(tag2);
Tag tag3 = obj.new Tag();
tag3.setName("title");
tag3.setValue("Test Title");
tagList.add(tag3);
}
public class Tag {
protected String name;
protected String value;
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
Jaxb 生成オブジェクト
public class CaasContextObject{
@XmlElement(required = true)
protected String title;
@XmlElement(name = "ADSKContentGroup")
protected List<String> adskContentGroup;
public String getTitle() {
return title;
}
public void setTitle(String value) {
this.title = value;
}
/**
* Gets the value of the adskContentGroup property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the adskContentGroup property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getADSKContentGroup().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link String }
*
*
*/
public List<String> getADSKContentGroup() {
if (adskContentGroup == null) {
adskContentGroup = new ArrayList<String>();
}
return this.adskContentGroup;
}
}
ご覧のとおり、問題は adskContentGroup 値の設定にあります。
任意のポインタをいただければ幸いです。
ありがとう