Search code examples
javagwtantlrgwt-rpcantlr3

Serialize ANTLR Exceptions for GWT RPC


I am using GWT-RPC to call an ANTLR grammar. If the grammar fails, I create an object containing the errors/exceptions that were thrown by the grammar and return it to the client.

When I do this I get the exception:

com.google.gwt.user.client.rpc.SerializationException: Type 'org.antlr.runtime.NoViableAltException' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded.

I have found that there is an identical class with the addition of a public no argument constructor (needed for GWT-RPC serialization) in the com.google.appengine.repackaged.org.antlr.runtime package.

How do I convert the org.antlr.runtime.NoViableAltException into a com.google.appengine.repackaged.org.antlr.runtime.NoViableAltException?


Solution

  • As an alternative for creating new Exceptions that can be serialized, I have made my Parser override the emitErrorMessage method from BaseRecognizer.

    @members {
        @Override
        public void emitErrorMessage(String msg) {
            // The original prints to stdout.
            // You can do what you like with the message.
        }
    }
    

    As Tassos suggested in his answer, I did not actually need the exception, just the message from it.