Search code examples
javaparsingxstream

XStream Error Resolution


I'm using a java application to parse some XML using XStream.

The XML is :

<object>
      <name>an Object Name</name>
      <ncss>154</ncss>
      <functions>48</functions>
      <classes>1</classes>
      <javadocs>44</javadocs>
    </object>

When parsing this I get the following error :

Exception in thread "main" com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field java.lang.Object.name
---- Debugging information ----
field               : name
class               : java.lang.Object
required-type       : java.lang.Object
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /objects/object/name
class[1]            : compareObjects.input.Objects
version             : null
-------------------------------
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.determineType(AbstractReflectionConverter.java:453)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:294)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:322)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1058)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1042)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:913)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:904)
    at compareObjects.Logic.mainClass.main(mainClass.java:60)

This is due to the fact that my code contains an Object class with a member name , but the JDK is referring to java.lang.Object class , which has no such field.

How do I resolve this conflict ? Any help would be welcome. Thanks in advance.

Edit : The code for the same is :

package compareObjects.Logic;


    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;

    import com.thoughtworks.xstream.XStream;
    import com.thoughtworks.xstream.io.xml.DomDriver;
    import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;

import compareObjects.input.*;
import compareObjects.outPut.changedObject;
import compareObjects.outPut.changedObjects;



    public class mainClass {

    public static void main(String[] args) throws IOException{

        BufferedReader br = new BufferedReader(new FileReader(new File("D:\\Object\\old_code_complexity.xml")));
        BufferedReader br2 = new BufferedReader(new FileReader(new File("D:\\Object\\new_code_complexity.xml")));

        String line;  //two strings to hold the old and new file's contents
        String line2;


        StringBuilder sb = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();


        //Objects representing the output to be stored in XML Format

        changedObjects oPack = new changedObjects();

        // remove multiple occurrences of space in the xml input file old_code_complexity.xml
        while((line=br.readLine())!= null){
            sb.append(line.trim());
        }

        String x = sb.toString();

     // remove multiple consecutive occurrences of spaces in contents of new_code_complexity.xml
        while((line2=br2.readLine())!= null){
            sb2.append(line2.trim());
        }

        //Create xstream instance to disable xstream from pushing extra _ character as escape sequence

        XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("_-", " ")));

        // Create aliases to shorten class names in output file.


        xstream.alias("objects",Objects.class);

        Objects oList = (Objects)xstream.fromXML(x);

        String x2 = sb2.toString();
        Objects oList2 = (Objects)xstream.fromXML(x2);


        for(int i = 0; i< oList.getSize();i++)
        {
           for(int j = 0; j< oList2.getSize();j++)

        {
           if(oList.getObject(i).getName().equals(oList2.getObject(j).getName()))
           {

               if(oList.getObject(i).equals(oList2.getObject(j)))
                    {
                      oPack.addSimilarObject(((new changedObject("old_code_complexity.xml",oList.getObject(i),"new_code_complexity.xml",oList2.getObject(j)))));
                    }
            else
            {
                oPack.addChangedObject(((new changedObject("old_code_complexity.xml",oList.getObject(i),"new_code_complexity.xml",oList2.getObject(j)))));

            }

         }
          }



        }

        System.out.println(xstream.toXML(oPack));
        System.out.println("Objects Changed " + oPack.getDifferentObjects());
        System.out.println("Packages Not Changed" + oPack.getSimilarObjects());

    }
    }

Solution

  • xstream.alias("objects",Objects.class);
    

    This tells XStream to create an Object of type Objects when it encounters <objects> tag in the xml.

    You didn't tell it what it should do for <object>, so it is (I think) creating the one it knows, viz. java.lang.Object.

    As I mentioned in the comment, perhaps you need

    xstream.alias("object", Your.Package.Object.class);