Farid.O
Farid.O

Reputation: 333

Problem serialising generated class

I am using GWT 2.3 for my application. I created a generator to generate classes and add behaviour to them.

My generator works fine and I can call GWT.create(MyClass.class) on the client. This does return a MyClassImpl object with the correct fields/methods. When I try to serialise the object to send it back to the server I have a SerializationException.

After debugging it appears that the RPC generator cannot find the class definition for MyClassImpl.

Is there a way of fixing this? I assumed the class definition generated by my own generator would be available to the RPC generator unless this one is run before my generator?

Thank you in advance for your help.

Farid



Hello and thank you for you quick answer,

My generated class is as follows: - it has a public no arg constructor - it implements Serializable (and to be sure I tried all combinations of Serializable and IsSerializable) - is is generated (by my generator when I call GWT.create() ) in a shared package - All its attributes are "Simple" (primitive or String)

It looks like this (where TestClass is my marker interface for the generator):

package com.test.shared;

import com.test.shared.TestClass;

public class TestClass_Impl implements TestClass, Serializable {
  private String testString = "TestString";

  public TestClass_Impl() {}

  public String getTestString() {
    return testString;
  }
}

I can call GWT.create(TestClass.class) on the client. I get a instance of TestClass_Impl but as soon as I try to send it over the network to teh server through a GWT RPC call I get an exception:

com.test.server.TestServiceServlet-29927840: An IncompatibleRemoteServiceException was thrown while processing this call.
com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException: java.lang.ClassNotFoundException: com.test.shared.TestClass_Impl
    at com.google.gwt.user.server.rpc.RPC.decodeRequest(RPC.java:315)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:206)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
....
Caused by: com.google.gwt.user.client.rpc.SerializationException: java.lang.ClassNotFoundException: com.test.shared.TestClass_Impl
    at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamReader.deserialize(ServerSerializationStreamReader.java:573)
    at com.google.gwt.user.client.rpc.impl.AbstractSerializationStreamReader.readObject(AbstractSerializationStreamReader.java:119)
...
Caused by: java.lang.ClassNotFoundException: com.test.shared.TestClass_Impl
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at org.mortbay.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:366)
    at org.mortbay.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:337)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamReader.deserialize(ServerSerializationStreamReader.java:543)

It looks like GWT RPC cannot find the class TestClass_Impl on the server when it tries to instantiate it.

Any idea? Thanks,

farid



Thank you Jusio. That is exactly what was going on. GWT seems to generate the java source only for the purpose of cross-compiling it into javascript and then discards the generated java files. There is a compiler option to keep the generated java code in a specific folder but the resulting .java file is not compiled with the build.

It is possible, although very convoluted and clumsy, to change the build process to pick up source files generated by my generator and include them in the build path so they can be compiled and available at runtime.

I have instead created a separate class to generate my java source code. This class is used by the generator to generate the java source to be cross-compiled by GWT. The same class is then used by my application to dynamically generate and load these java classes into the VM. They are then available at runtime and all seems to be working fine.

Thank you again for your help and if anyone needs more information about this workaround I will be happy to send it to them.

Upvotes: 0

Views: 594

Answers (2)

jusio
jusio

Reputation: 9920

I believe the problem is, that generated class should exist on both client and server. That's why you get this error. As far as i know generators don't work on the server side. May be there is a way to make them work, but I don't know about it. Possible solution - launch compiler with -gen option to save generated classes to the disk, compile them to *.class and copy to the server class path. Or don't use GWT-RPC =)

Upvotes: 2

Saket
Saket

Reputation: 46137

Ensure that your generator makes the generated class IsSerializable (i.e., implements the IsSerializable interface).

Upvotes: 0

Related Questions