Details
-
Type: Bug
-
Status: Closed
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: 1.1
-
Component/s: None
-
Labels:None
Description
JavaMethodConverter raises ConversionException whenever it tries to unmarshal an xmlString which contains a primitive parameter.
I have attached a testcase and a fix. This fix simply implements a primitiveClassNamed() method, initially i wanted to use a DefaultClassMapper to do the work of primitiveClassNamed() so as to avoid duplicating code, but i failed to gain access to a DefaultClassMapper instance from JavaMethodConverter.
Note: the testcase below is also used for the bug (XSTR-148) i reported on 14 Oct.
regards
chung-onn
Testcase
=====
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import com.thoughtworks.xstream.XStream;
public class XStreamTester extends TestCase {
public static void main(String[] args)
/*
- Test alias for interface class
*/
public void testAlias() { System.out.println("\nAlias with aliased xmlString"); XStream xs = new XStream(); xs.alias("addressBookInfo",AddressBookInfo.class,AddressBook.class); xs.alias("addressInfo",AddressInfo.class, Address.class); AddressBookInfo addrInfo = new AddressBook(); String xmlString = xs.toXML(addrInfo); System.out.println("***xmlstring:\n" +xmlString); AddressBookInfo addrInfo1 = (AddressBookInfo) xs.fromXML(xmlString); System.out.println(" Object:"+addrInfo1); System.out.println("***xmlString:\n"+xs.toXML(addrInfo1)); }
/*
- Test aliasing after from a non-alias xmlstring
*/
public void testAlias1() { System.out.println("\nAlias without aliased xmlString"); XStream xs = new XStream(); AddressBookInfo addrInfo = new AddressBook(); String xmlString = xs.toXML(addrInfo); System.out.println("***xmlstring:\n" +xmlString); xs.alias("addressBookInfo",AddressBookInfo.class,AddressBook.class); xs.alias("addressInfo",AddressInfo.class, Address.class); AddressBookInfo addrInfo1 = (AddressBookInfo) xs.fromXML(xmlString); System.out.println(" Object:"+addrInfo1); System.out.println("***xmlString:\n"+xs.toXML(addrInfo1)); }
public void testMethod() throws SecurityException, NoSuchMethodException{
Method m = Address.class.getDeclaredMethod("setPcode",new Class[]
);
XStream xs = new XStream();
String xmlString = xs.toXML(m);
System.out.println("***xmlstring:\n" +xmlString);
Method m1 = (Method)xs.fromXML(xmlString);
System.out.println(" Object:"+m1);
System.out.println("***xmlString:\n"+xs.toXML(m1));
}
public interface AddressBookInfo
{ public List getAddresses(); public void setAddresses(List address); } public class AddressBook implements AddressBookInfo {
private List addresses;
public AddressBook()
{ addresses = new ArrayList(); AddressInfo addr = new Address("Home","Home",234); AddressInfo addr1 = new Address("Office","Office",567); addresses.add(addr); addresses.add(addr1); }public List getAddresses()
{ return addresses; }public void setAddresses(List addresses)
{ this.addresses = addresses; }}
public interface AddressInfo
{ public String getAddr1(); public String getAddr2(); public void setAddr1(String addr1); public void setAddr2(String addr2); } public class Address implements AddressInfo {
private String addr1="addr1";
private String addr2="addr2";
private int pcode = 0;
public Address(String addr1, String addr2, int pcode)
public String getAddr1()
{ return addr1; }public String getAddr2()
{ return addr2; }public void setAddr1(String addr1)
{ this.addr1 = addr1; }public void setAddr2(String addr2)
{ this.addr2 = addr2; }public int getPcode()
{ return pcode; }public void setPcode(int pcode)
{ this.pcode = pcode; }}
}
Fix for JavaMethodConverter
==================
private Class loadClass(String className) throws ClassNotFoundException {
Class primitiveClass = primitiveClassNamed(className);
if(primitiveClass != null)
return classLoader.loadClass(className);
}
/**
- Lookup table for primitive types.
*/
private Class primitiveClassNamed(String name) { return name.equals("void") ? Void.TYPE : name.equals("boolean") ? Boolean.TYPE : name.equals("byte") ? Byte.TYPE : name.equals("char") ? Character.TYPE : name.equals("short") ? Short.TYPE : name.equals("int") ? Integer.TYPE : name.equals("long") ? Long.TYPE : name.equals("float") ? Float.TYPE : name.equals("double") ? Double.TYPE : null; }
Hi Chung-Onn,
could you please provide a patch for this bug,
as it makes life so much easier.
In Eclipse, all you need to do is right-click on the project
and select Team > Create Patch ...
The wizard will guide you to create a txt file with the cvs diff -u
which you can attach.
Please do the same for
XSTR-148.Many thanks!