0

I have two classes.

XClass1

  @XStreamAlias("reply")
  public class XClass1 {

    @XStreamAlias("message")
    private String message;

    public String getMessage() {
      return message;
    }

  }

XClass2

  @XStreamAlias("reply")
  public class XClass2 {

    @XStreamAlias("status")
    private String status;

    public String getStatus() {
      return status;
    }

  }

I am using Xstream to convert xml to Java

@Test
  public void test() {
    XStream stream = new XStream() {
      @Override
      protected MapperWrapper wrapMapper(MapperWrapper next) {
        return new MapperWrapper(next) {
          @Override
          public boolean shouldSerializeMember(@SuppressWarnings("rawtypes") Class definedIn, String fieldName) {
            if (definedIn == Object.class)
              return false;

            return super.shouldSerializeMember(definedIn, fieldName);
          }
        };
      }
    };
    stream.autodetectAnnotations(true);
    stream.processAnnotations(new Class[] {XClass1.class, XClass2.class});

    String reply = "<reply><message>Not OK</message></reply>";
    XClass1 lass1 = (XClass1) stream.fromXML(reply);

    String reply2 = "<reply><status>Not OK</status></reply>";
    XClass2 lass2 = (XClass2) stream.fromXML(reply2);
  }

I am getting class cast exception when converting xml to java

Error at this place

 XClass1 lass1 = (XClass1) stream.fromXML(reply);

Exception

java.lang.ClassCastException: com.myapp.Test123$XClass2 cannot be cast to com.myapp.Test123$XClass1
    at com.myapp.Test123.test(Test123.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Patan
  • 17,073
  • 36
  • 124
  • 198

2 Answers2

0

There is a little bug in the last line:

XClass2 lass2 = (XClass2) stream.fromXML(reply);

I think it shoult be

XClass2 lass2 = (XClass2) stream.fromXML(reply2); // reply2 
F Vicedo
  • 46
  • 3
  • Sorry for incorrect question. It is typo. I still get the error. Would you be able to help. – Patan Oct 26 '16 at 13:26
  • Maybe the problem is, that both classes have the same XStreamAlias "reply". How will XStream know, which class to use? – F Vicedo Oct 26 '16 at 13:31
  • Yes true. Is there any solution to this. – Patan Oct 26 '16 at 13:33
  • I wonder if you could solve the problem just using one single class containing a status an a message? Maybe all the replies have a status (http does so) and they may or may not have a message – F Vicedo Oct 26 '16 at 13:41
0

It says that the equality of two classes depends on class name and loader.

XStream must be using a different class loader.

The quick solution would be to set same class loader current thread is using:

XStream xStream = new XStream();
xStream.setClassLoader(Thread.currentThread().getContextClassLoader());

Credit should go to this post https://stackoverflow.com/a/43324535/2935802

Sudhakar
  • 3,104
  • 2
  • 27
  • 36