Search code examples
javamixinscglib

Create Mixin without using interfaces


In Java, I have some legacy classes (from 3rd party library which I cannot edit) that to not implement any interfaces, but for which I would like to create a mixin.

I have looked at cglib for creating mixins, but this tutorial states that

cglib Mixins allow the combination of several objects into a single object. However, in order to do so, those objects must be backed by interfaces

So does anyone know how to create a mixin of two concrete classes, neither of which implement any interfaces?


I had been thinking about using bytecode generation to automatically extract an interface from a concrete class that I could then just feed into cglib, but I have no idea how to do that either...


Solution

  • No need for anything fancy, just a bit of dull donkey work.

    Create the interfaces you'd like to program against, then create concrete classes that implement them using the legacy library.

    You can now create mixins using cglib if you really want to, but more importantly you can develop your app without making it strongly dependent on the legacy 3rd party library.