I have class 'A' that implements interface 'I'. I have two classes 'B' and 'C', each extends A and adds a new method. C and B do not override any method in A. The new method in Class 'B' has a signature different from that of 'C' and the new method is the only difference between C and B. I need to create a Proxy (sort of composite of objects of 'B' and 'C') that should have all the methods of A and the new methods in 'B' and 'C'.
I tried to use Mixin$Generator in CGLIB to create a composite proxy but I get Error "java.lang.ClassFormatError: Duplicate interface name in class file".
Has anyone encountered a similar situation? Any suggestions for solving this issue?
Thanks for your time.
Here is the code updated with the interface and all the classes.
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Mixin;
import net.sf.cglib.proxy.Mixin.Generator;
interface I {
public boolean testSomething();
}
class A implements I {
@Override
public boolean testSomething() {
boolean isRoot = true;
System.out.println("Returning '" + isRoot + "' from '" + this + "' ...");
return isRoot;
}
}
class B extends A {
public boolean isB() {
boolean isRoot1 = true;
System.out.println("Returning " + isRoot1 + " from : " + this);
return isRoot1;
}
}
class C extends A {
public int getInt() {
int someInt = 2;
System.out.println("Returning " + someInt + " from : " + this);
return someInt;
}
}
public class TestMixin {
public static Object invokeMethod(Object target, String methodName)
throws Exception {
Method method = target.getClass().getMethod(methodName);
return method.invoke(target);
}
public static Mixin newInstance(Object[] delegates) {
return newInstance(null, delegates);
}
public static Mixin newInstance(Class[] interfaces, Object[] delegates) {
Generator gen = new Generator();
gen.setStyle(Mixin.STYLE_EVERYTHING);
if(interfaces != null) {
gen.setClasses(interfaces);
}
gen.setDelegates(delegates);
return gen.create();
}
public static void main(String[] args) throws Exception {
// B and C extend Class 'A' which implements 'I' interface
B root1 = new B();
C root2 = new C();
A[] roots = { root1, root2 };
Class<?>[] interfaces = new Class[] { B.class, C.class };
// newInstance causes java.lang.ClassFormatError: Duplicate interface
// name in class file com/mycom/cglib/B$$MixinByCGLIB$$831a43ec
Mixin mixin = TestMixin.newInstance(interfaces, roots);
System.out.println("Mixin Object: " + mixin);
System.out.println(invokeMethod(mixin, "testSomething"));
System.out.println(invokeMethod(mixin, "isB"));
System.out.println(invokeMethod(mixin, "getInt"));
}
}