我正在尝试从对象集合(Tag)中读取值,并使用反射将它们映射到jaxb生成的对象。我有一个字段列表,它从主对象中筛选出所需的字段。
这是示例代码。我使用内部类Tag作为需要从中提取值的列表。列表字段列表充当过滤器。目标是使用反射将这些值复制到jaxb生成的对象中。我使用javabeanPropertyDescriptor来实现这一点。
现在,我遇到了为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("nnPrinting 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值。
任何建议都将不胜感激。
感谢
这就是您想要的吗?
if (pd.getReadMethod() != null && pd.getReadMethod().getReturnType().isAssignableFrom(List.class)) {
Method method = pd.getReadMethod();
List list = (List) method.invoke(mc);
list.add("Hello World!"); // change the passed String of course
}