07 November 2011

How to inherit static methods in Scala

In our project we're using Apache Axis soap web services. With Axis, you have to define your pojos in a certain way. Not only do you need your getters and setters, you have to define a static method so that the axis libraries can find out type information to create the wsdl and static methods to serialize and deserialize to and from XML. These must be static methods, so are very hard to factor out in Java. We need to duplicate them for each POJO class.
public class WebServiceObject {
    private Integer id;

public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
// Type metadata public static TypeDesc getTypeDesc() { TypeDesc typeDesc = new TypeDesc(WebServiceObject.class, true); typeDesc.setXmlType(new QName("to", "WebServiceObject")); SoapHelper.addTypeDesc(typeDesc, "id", "int", true); return typeDesc; }
public static Serializer getSerializer(String mechType, Class<?> javaType, QName xmlType) { return new BeanSerializer(javaType, xmlType, typeDesc); }
public static Deserializer getDeserializer(String mechType, Class<?> javaType, QName xmlType) { return new BeanDeserializer(javaType, xmlType, typeDesc); } }
As you can see, there is a lot of boilerplate here. Some of the POJOs are large (66 attributes), so there is a *lot* of boilerplate. Is there anything we can do about this? Let's see. The first thing we can use is @BeanProperty, to get rid of the getters and setters. This annotation, which we can apply to a field, generates a getter and setter for that field.
class WebServiceObject {
  @BeanProperty var id: Integer = _
So already this is a lot better. But what about the static methods? We can apply a trick here. If we define a method in a companion object, then the class gets a static forwarder for that method. So I can define a getTypeDesc in the companion object, and then java can call it using the normal static way, i.e: WebServiceObject.getTypeDesc(). And we can use inheritance with companion objects. Yay!
So this is the entire code:
class WebServiceObject {
  @BeanProperty var id: Integer = _

object WebServiceObject extends SoapSerializer { val typeDesc = { val typeDesc = createTypeDesc(classOf[WebServiceObject], "to", "WebServiceObject") addTypeDesc(typeDesc, "id", "int", true) } }
trait SoapSerializer { val typeDesc: TypeDesc
def getTypeDesc() = typeDesc
def getSerializer(mechType: String, javaType: Class[_], xmlType: QName) = new BeanSerializer(javaType, xmlType, typeDesc)
def getDeserializer(mechType: String, javaType: Class[_], xmlType: QName) = new BeanDeserializer(javaType, xmlType, typeDesc) }
So our pojo has gone from 26 java lines to 9 Scala lines. Our 66 attribute POJO was 698 lines but is now 154.
There is more we could do, but this is enough for the minute. Our code is pretty well factored, and the amount of duplication is now acceptable. More importantly, you can read the code :-)

No comments: