Search code examples
gwtautobean

GWT Autobean set initial value of created interface


We are using AutoBeans to create our Pojo objects for use in RPC-Calls. What is the recommended approach for the Pojo to have a default value or other class initialization?

For example

 public interface SamplePojo {
        // should default to 5
        int getSampleProperty();
        void setSampleProperty(int sampleProperty);
    }


    public interface ModelFactory extends AutoBeanFactory {
        AutoBean<SamplePojo> getSamplePojo();   
    }

And SamplePojo has a int property that we always want to default to 5.


Solution

  • AutoBeans should be seen as low-level, mapping straight to/from JSON. With that in mind, you don't want getSampleProperty() to be 5, you rather want to detect the absence of specific value for the property and use 5 in that case.

    So, if 0 (the default value of an int) is not an acceptable value for the property, then simply "use 5 if the property is 0". Otherwise, change the return type to Integer and "use 5 if the property is null".