entry : aliases.entrySet()) {
+ String alias = entry.getValue();
+ String field = entry.getKey();
+ int idx = field.lastIndexOf('.');
+ if (idx != -1) {
+ String className = field.substring(0, idx);
+ Class clazz = ClassUtils.forName(className, classLoader);
+ String fieldName = field.substring(idx + 1);
+ this.getXStream().aliasField(alias, clazz, fieldName);
+ } else {
+ throw new IllegalArgumentException("Field name [" + field + "] does not contain '.'");
+ }
}
}
@@ -212,7 +253,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
/**
* Set the classes for which mappings will be read from class-level JDK 1.5+ annotation metadata.
- * @see com.thoughtworks.xstream.XStream#processAnnotations(Class)
+ * @see XStream#processAnnotations(Class)
*/
public void setAnnotatedClass(Class> annotatedClass) {
Assert.notNull(annotatedClass, "'annotatedClass' must not be null");
@@ -221,7 +262,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
/**
* Set annotated classes for which aliases will be read from class-level JDK 1.5+ annotation metadata.
- * @see com.thoughtworks.xstream.XStream#processAnnotations(Class[])
+ * @see XStream#processAnnotations(Class[])
*/
public void setAnnotatedClasses(Class>[] annotatedClasses) {
Assert.notEmpty(annotatedClasses, "'annotatedClasses' must not be empty");
@@ -256,6 +297,10 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
customizeXStream(getXStream());
}
+ public void setBeanClassLoader(ClassLoader classLoader) {
+ this.classLoader = classLoader;
+ }
+
/**
* Template to allow for customizing of the given {@link XStream}.
* Default implementation is empty.
diff --git a/org.springframework.oxm/src/test/java/org/springframework/oxm/xstream/XStreamMarshallerTests.java b/org.springframework.oxm/src/test/java/org/springframework/oxm/xstream/XStreamMarshallerTests.java
index 96ce6a022c..7d307cdc42 100644
--- a/org.springframework.oxm/src/test/java/org/springframework/oxm/xstream/XStreamMarshallerTests.java
+++ b/org.springframework.oxm/src/test/java/org/springframework/oxm/xstream/XStreamMarshallerTests.java
@@ -68,8 +68,8 @@ public class XStreamMarshallerTests {
@Before
public void createMarshaller() throws Exception {
marshaller = new XStreamMarshaller();
- Map aliases = new HashMap();
- aliases.put("flight", Flight.class);
+ Map aliases = new HashMap();
+ aliases.put("flight", Flight.class.getName());
marshaller.setAliases(aliases);
flight = new Flight();
flight.setFlightNumber(42L);
@@ -214,6 +214,14 @@ public class XStreamMarshallerTests {
assertXMLEqual("Marshaller does not use attributes", expected, writer.toString());
}
+ @Test
+ public void fieldAliases() throws Exception {
+ marshaller.setFieldAliases(Collections.singletonMap("org.springframework.oxm.xstream.Flight.flightNumber", "flightNo"));
+ Writer writer = new StringWriter();
+ marshaller.marshal(flight, new StreamResult(writer));
+ String expected = "42";
+ assertXMLEqual("Marshaller does not use aliases", expected, writer.toString());
+ }
@Test
@SuppressWarnings("unchecked")