Many components of the AngularFaces library share common properties and methods. Some of these methods are simple, some of them are complex. I’d like to move them to another class instead of implementing them multiple times. Unfortunately the component classes don’t derive from a common base class. That’s nothing I can fix. That’s just the way PrimeFaces and Mojarra are designed. So I can’t factor out the common traits using inheritance.
The second common approach, the delegation pattern, certainly works. However, it amounts to writing a lot of boiler plate code. At first glance, this isn’t much of a problem since most modern IDEs offer a refactoring tool to implement the boiler plate code. Eclipse is going to help you. But as Venkat Subramaniam points out in his Java One 2013 talk (roughly at 31:30), this approach has severe disadvantages. Adding a method to the base class forces you to add it to every class using the base class as a delegate. The same applies to removing a method or changing a methods signature.
So what we really want to have is something like Scala’s trait or Groovy’s
@delegate annotation. This is often referred to as “mixin”. Basically, it’s a kind of multiple inheritance, but typically mixins or traits are defined in a slightly less anarchic way. For instance, the famous diamond inheritance problem of C++ simply can’t occur.
However, porting AngularFaces to another JVM language might reduce the library’s attractiveness to other developers, not to mention it’s versatility. Virtually every JVM language features nice integration with Java, but few JVM languages play nicely with other JVM languages other than Java.
Some time ago I’ve seen a surprising approach to implement mixins using AspectJ. But again, AspectJ might repel some Java developers and adds to the tool stack.
Another possibility is to wait for Java 8. Defender methods deliver most of the functionality I’m missing. But I don’t know how long it takes until OpenShift – where I host the AngularFaces showcase – is going to support Java 8, so this isn’t an attractive option, either.
So it’s tempting to abandon all hope. But wait – what about Javassist?