4 Replies Latest reply on Jan 7, 2015 12:44 PM by Phillip Nakaska

    Class cast exception encountered with manually mapped nested classes

    Phillip Nakaska Newbie

      Encountering a ClassCastException when Deserializing an external class (Facet) from a Donor project.

      The Donor project provides a (nested) class that needs to be marshalled between client and server.

      The donor project 'Provider' defines a 'Facet' class.

      The nested Facet class (nested within the SearchResult class)  is returned by the search service (SearchService.search()), and the

      client is attempting to deserialize the nested Facet class, but hitting a class cast exception.

       

      The test project 'erraiexample'   includes configuration to manually add the external classes from 'Provider' project.

      (mapped through resources/com/kdm/provider/Searchprovider.gwt.xml)

      The classes that have to be added cannot be marked @Portable, so instead they are made Serializable and have default constructors.

       

      When the default page is loaded. the ErraiExampleApp.init() calls a server side search service and receives a response in a ResponseCallback.

      The ResponseCallback uses  MarshallingWrapper.fromJSON() to create the local unmarshalled object. (we have RestClient.setJacksonMarshallingActive(true))

      this part is successful, and referencing other nested classes in the SearchResult works fine, such as for the 'Detail' class, and geoShape (Geoshape uses a custom Marshaller successfully).

      However when trying to reference the ArrayList<Facet> entries list, (via iterator) a ClassCastException is encountered.

      Below is the browser console output  of the ResponseCallback, Facets:1 is just before the CCE. This shows successful receipt of the Detail instance, and the failure of the Facet creation.

      ...

      15:20:43 INFO [ErraiExampleApp] searchResultCallback:  de-marshalled! ConsoleLogHandler.java:77

      15:20:43 INFO [ErraiExampleApp] searchResultCallback:   detail.value:'hello111 GET' ConsoleLogHandler.java:77

      15:20:43 INFO [ErraiExampleApp] searchResultCallback:   detail.value:'hello222 GET' ConsoleLogHandler.java:77

      15:20:43 INFO [ErraiExampleApp] searchResultCallback:  numGeometries=1 ConsoleLogHandler.java:77

      15:20:43 INFO [ErraiExampleApp] searchResultCallback:  about to reference Facets: ConsoleLogHandler.java:77

      15:20:43 INFO [ErraiExampleApp] searchResultCallback:  Facets:1 ConsoleLogHandler.java:77

      15:20:49 SEVERE [LogConfiguration] null

      ...

      Attached are the Donor project 'Provider' and the test project 'ErraiExample'

        • 1. Re: Class cast exception encountered with manually mapped nested classes
          Phillip Nakaska Newbie

          Here are the same projects further simplified by pulling out all the JTS (Geometry classes).

          • 2. Re: Class cast exception encountered with manually mapped nested classes
            Christian Sadilek Master

            Hi Phillip!

             

            The project doesn't build as it relies on what seems to be an internal repository (http://caldm-dev.kelman.com/nexus/content/repositories/internal-thirdparty/). It would probably be best if you could put a minimal test case / example up on GitHub that's easy to set up / run and reproduces the error.

             

            I can take another look then. Any more details on the error stack-trace etc. would also be appreciated.

             

            Cheers,

            Christian

            • 3. Re: Re: Class cast exception encountered with manually mapped nested classes
              Phillip Nakaska Newbie

              Sorry, forgot about the local repositories. I've stripped them out and adjusted the POM, the error is still encountered.

              Attached the corrected projects.

              the ResponseCallback in the  ErraiExampleApp.java init()  is where the problem is encountered.

              The SearchResponse is marshalled, the Details list nested  in the SearchResult  is fine and the details can be referenced. The Facet list,

              references to the list or items on the list cause the ClassCastException, in this case when it's iterating through the list at (for (Facet facet : facets) below):

               

                final ResponseCallback searchResultCallback = new ResponseCallback()

                  {

                    @Override

                    public void callback(Response response)

                    {

                      log.info("searchResultCallback: callback: setting up, response.getStatusCode()="

                          + response.getStatusCode());

                      if (response.getStatusCode() == 200)

                      {

                        log.info("searchResultCallback:  service response text:'" + response.getText() + "'");

                        response.getClass();

                        SearchResult re = MarshallingWrapper.fromJSON(response.getText(), SearchResult.class);

                        log.info("searchResultCallback:  de-marshalled!");

                      

                        List<Detail> details = re.getDetails();

                        for (Detail detail : details) {

                          log.info("searchResultCallback:  detail.value:'" +  detail.getValue() + "'");

                        }

                        log.info("searchResultCallback:  about to reference Facets:");

                        List<Facet> facets = re.getFacets();

                        log.info("searchResultCallback:  Facets:1");

                        for (Facet facet : facets) {

                          log.info("searchResultCallback:  facet.count:'" +  facet.count + "'");

                        }

                        log.info("searchResultCallback:  done.");

                      }

                    }

              tracing the code in the console debugger leads to a call to Cast.java line 57(dynamicCast), and it dies in canCast(). throwing the ClassCastException at line 59

               

              but only this message is visible on the console

              16:04:53 SEVERE [LogConfiguration] null

              Hope this helps to clarify.

              • 4. Re: Re: Class cast exception encountered with manually mapped nested classes
                Phillip Nakaska Newbie

                I changed All the Facet, FacetEntry,  Field and SearchResult class data fields to 'private', and ensured getters and setters were provided.

                Once I did that, the marshalling now works. classes from external providers which cannot use annotations need to Implement Serializable, but also need all fields and fields of all nested classes to be explicitly marked private and not left implicitly package private.