我正在尝试编写一个小库,它将通过反射将Java对象转换为XML。我已经得到了它的大部分工作,但遇到了一个错误,当试图通过数组迭代。
以下是我用于测试的域对象:
在Company.java:import java.util.List;
import com.google.common.collect.Lists;
public class Company
{
public Employee employeeArray[];
public Employee[] getEmployeeArray()
{
return employeeArray;
}
public void setEmployeeArray(Employee[] employeeArray)
{
this.employeeArray = employeeArray;
}
}
在Employee.java: public class Employee
{
public String firstName;
public String lastName;
public Employee() {}
public Employee(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
}
库的核心(ObjectXMLWriter.java):
import java.io.StringWriter;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.List;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.falcondev.web.DOMFactory;
import com.google.common.collect.Lists;
public class ObjectXMLWriter
{
private static final Logger logger = Logger.getLogger(ObjectXMLWriter.class);
private String fileLocation;
private Object object;
private boolean shouldOverride;
public ObjectXMLWriter(String fileLocation, Object object) {
this.fileLocation = fileLocation;
this.object = object;
this.shouldOverride = false;
}
public ObjectXMLWriter(String fileLocation, Object object, boolean shouldOverride) {
this(fileLocation, object);
this.shouldOverride = shouldOverride;
}
public String getFileLocation()
{
return fileLocation;
}
public void setFileLocation(String fileLocation)
{
this.fileLocation = fileLocation;
}
public Object getObject()
{
return object;
}
public void setObject(Object object)
{
this.object = object;
}
public boolean isShouldOverride()
{
return shouldOverride;
}
public void setShouldOverride(boolean shouldOverride)
{
this.shouldOverride = shouldOverride;
}
public boolean saveObject() throws Exception {
boolean saveSuccessful = false;
Document document = createDocument();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
StringWriter stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);
DOMSource source = new DOMSource(document);
transformer.transform(source, result);
String xmlString = stringWriter.toString();
//print XML
System.out.println("Here's the xml:nn" + xmlString);
//TODO save XML file
return saveSuccessful;
}
private Node createNode(Document document, Object object) throws Exception {
Node node = document.createElement(getObjectClassName(object));
logger.trace("NODE: " + node);
if(node != null) {
//create children nodes from object fields
List<Field> fields = Lists.newArrayList(object.getClass().getFields());
for(Field field: fields) {
Object obj = field.get(object);
logger.trace("OBJECT: " + obj);
if(obj == null || !checkInstantiability(obj) || field.getType().isAssignableFrom(List.class) || field.getType().isArray()) {
logger.debug("ATTEMPTING TO CREATE NODE FOR FIELD: " + field.getName());
logger.debug("FIELD TYPE: " + field.getType());
//TODO add types as needed
String fieldValue = "";
if(List.class.isAssignableFrom(field.getType())) { //TODO check if object is iterable instead
//TODO figure out how to iterate through iterable's
}
else if(field.getType().isArray()) {
Object array = field.get(obj); //Fails here
int length = Array.getLength(array);
for (int i = 0; i < length; i++) {
System.out.println(Array.get(array, i));
node.appendChild(createNode(document, Array.get(array, i)));
}
}
else if(field.getType() == Class.class) {
fieldValue = obj.toString().replaceFirst("class ", "");
}
else {
fieldValue = obj.toString();
}
logger.debug("FIELD OBJECT VALUE: '" + fieldValue + "'");
//TODO check for annotation to choose whether to create element or attribute
Element element = document.createElement(field.getName());
element.setTextContent(fieldValue);
node.appendChild(element);
}
else {
logger.debug("ATTEMPTING TO CREATE OBJECT NODE FOR FIELD: " + field.getName());
node.appendChild(createNode(document, obj));
}
}
}
return node;
}
private Document createDocument() throws Exception {
Document document = DOMFactory.create();
if(checkInstantiability(object)) {
Node rootNode = createNode(document, object);
document.appendChild(rootNode);
}
else {
logger.error("CANNOT SAVE UNINSTANTIABLE OBJECT. DOCUMENT MUST HAVE A ROOT NODE");
}
return document;
}
private String getObjectClassName(Object object) {
return object.getClass().getSimpleName().toLowerCase();
}
private boolean checkInstantiability(Object object) {
try
{
object.getClass().newInstance();
}
catch (InstantiationException exception)
{
return false;
}
catch (IllegalAccessException exception)
{
return false;
}
return true;
}
}
测试驱动程序(Test.java):
public class Test
{
public static void main(String[] args) throws Exception
{
Employee[] employees = new Employee[3];
employees[0] = new Employee("Tom", "C");
employees[1] = new Employee("Paul", "E");
employees[2] = new Employee("George", "A");
Company company = new Company();
company.setEmployeeArray(employees);
new ObjectXMLWriter("resources/test2.xml", company).saveObject();
}
}
当运行这段代码时,我得到的错误如下:
Exception in thread "main" java.lang.IllegalArgumentException: Can not set [Lcom.falcondev.orm.test.Employee; field com.falcondev.orm.test.Company.employeeArray to [Lcom.falcondev.orm.test.Employee;
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:37)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:18)
at java.lang.reflect.Field.get(Field.java:358)
at com.falcondev.orm.ObjectXMLWriter.createNode(ObjectXMLWriter.java:116)
at com.falcondev.orm.ObjectXMLWriter.createDocument(ObjectXMLWriter.java:149)
at com.falcondev.orm.ObjectXMLWriter.saveObject(ObjectXMLWriter.java:74)
at com.falcondev.orm.test.Test.main(Test.java:37)
我知道这是相当多的代码张贴。我尝试了下面的例子,如这篇文章(http://stackoverflow.com/questions/2200399/iterating-over-arrays-by-reflection/2200493#2200493)所示,它工作得很好,所以从简单的例子和我在上面的代码中所做的一定有一些微妙的区别。
public class Test
{
public static void main(String[] args) throws Exception
{
Employee[] employees = new Employee[3];
employees[0] = new Employee("Tom", "C");
employees[1] = new Employee("Paul", "E");
employees[2] = new Employee("George", "A");
Company company = new Company();
company.setEmployeeArray(employees);
Field field = company.getClass().getField("employeeArray");
if (field.getType().isArray()) {
Object array = field.get(company);
int length = Array.getLength(array);
for (int i = 0; i < length; i++) {
System.out.println(Array.get(array, i));
}
}
}
}
至于技术信息,本例中我使用的是:windows 7、eclipse 3.7、jdk 1.6.0_26、log4j 1.2.16、apache commons-lang3-3.0.1和google guava 10.0
在过去的几个星期里,我一直在努力使这个工作,所以任何帮助都会非常感激。
编辑:为将来参考和其他人使用,修复该问题的相关代码如下:
boolean shouldSaveFieldValue = true;
if(... || field.getType() == String.class) {
..
else if(field.getType().isArray()) {
Object array = field.get(object);
...
shouldSaveFieldValue = false;
}
if(shouldSaveFieldValue) {
...
Element element = document.createElement(field.getName());
element.setTextContent(fieldValue);
node.appendChild(element);
}
}
首先,我支持使用库而不是自己编写库的建议。我在使用JAXB方面非常幸运,它不需要第三方jar。对于你的问题,我认为你只是混淆了变量名称"object"one_answers"obj":
Company object = a Company
Field field = [ Company.Employee[] employeeArray ]
Employee[] obj = object.employeeArray
field.get(obj)不能求值,因为参数不是Company类型。以下是相关的代码:
private Node createNode(Document document, Object object) {
..
List<Field> fields = Lists.newArrayList(object.getClass().getFields());
for(Field field: fields) {
...
Object obj = field.get(object);
...
else if(field.getType().isArray()) {
Object array = field.get(obj); //Fails here
变化:
public Employee employeeArray[];
:
public transient Employee employeeArray[];
据我所知,数组是不可序列化的,我想这就是为什么你有一个员工列表和一个数组。因此,这告诉序列化程序在序列化对象时忽略雇员数组。