I have class A
that implements interface I
. I have two classes B
and C
, each extends A
and adds a new method. The new method in Class B
is different from that of C
. I need to create a Proxy (sort of composite) that should have all the methods of A
and the new methods in B
and C
.
I tried to use Mixin$Generator in CGLIB but I get Error java.lang.ClassFormatError: Duplicate interface name in class file
.
Can someone suggest a solution?
It is impossible to truly compose two non-interface classes in Java. If those two objects have a common structure (by
A
), then this would result in a conflict since any field or method inA
would be doubled what is illegal in Java. However, if there is no such conflicting information present, then the classesB
andC
should not share a super classA
.If you want to apply this sort of mixin delegation, you will need to create some interface abstraction for your classes. These interfaces can then be combined inside a proxy and delegate to the instances of
B
andC
depending on which of those implement an interface. This, and nothing more, is offered by cglib'sMixin
. Here is an example of how the composition with cglib works: