gwtgwtp

GWT result state changes after dispatch


data item boolean flag won't hold its state when item is returned from server through dispatcher to Presenter (client side).

shared package

 public class ResourceItem extends BaseResourceItem implements IsSerializable {

  private String name;

  public ResourceItem() {
    super();
  }

  public ResourceItem(String name) {
    super(true);
    this.name = name;
  } 
}

public class BaseResourceItem {

  private boolean removeEnabled = true;

  public BaseResourceItem() {
    super();
  }

  public BaseResourceItem(boolean removeEnabled) {
    super();
    this.removeEnabled = removeEnabled;
  }

  public boolean isRemoveEnabled() {
    return removeEnabled;
  }

  public void setRemoveEnabled(boolean removeEnabled) {
    this.removeEnabled = removeEnabled;
  }
}

Flag in question is removeEnabled . By default it's true, and even though I set it to false in server side, when Presenter gets it, it's been set to false for some reason. Am I missing something with serialization? (can't think of anything else at this point).

Server package

@GenDispatch
public class GetModelSettings {

    @Out(1)
    List<ResourceItem> listOfSettings;
}

public class GetModelSettingsHandler implements ActionHandler<GetModelSettingsAction, GetModelSettingsResult> {

        @Override
        public GetModelSettingsResult execute(GetModelSettingsAction action, ExecutionContext context)
                throws ActionException {

            ResourceItem item1 = new ResourceItem();
            ResourceItem item2 = new ResourceItem();
            item2.setRemoveEnabled(false);

            list.add(item1);
            list.add(item2);

            // item1 -> true
            // item2 -> false

            return new GetModelSettingsResult(list);
        }
    }

As you can see, a simple handler return a list. At this point, data is correct, one item has flag set to true, the other one to false.

Client package

public class ModelSettingsPresenter {

    dispatcher.execute(new GetModelSettingsAction(), new AsyncCallback<GetModelSettingsResult>() {

        @Override
        public void onSuccess(GetModelSettingsResult result) {
            itemList = result.getListOfSettings(); 
            // itemList.get(0) -> true
            // itemList.get(1) -> true
        }
    });
}

Data items both have flags set to true in this presenter. Any ideas why is this happening?


Solution

  • This has to do with serialization used with inheritance.

    During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream.

    More on it can be found in different thread Java object Serialization and inheritance