我正在尝试从对象集合(标签)中读取值,并使用反射将它们映射到 jaxb 生成的对象。我有一个字段列表,它从主对象中过滤出所需的字段。
这是示例代码。我使用内部类 Tag 作为需要从中提取值的列表。List fieldList 充当过滤器。目标是使用反射将这些值复制到 jaxb 生成的对象。我正在使用 javabean PropertyDescriptor 来实现这一点。
现在,我遇到了为 jaxb 对象中的集合字段设置值的问题。对于 List 字段,jaxb 不提供 setter,而是您必须使用 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 值。
任何指针将不胜感激。
谢谢