hello,
I'm trying to realizewith Javassist a tool that can reduce an inheritance tree into a flat class (that is, if C3 inherits from C2, which in turn inherits from C1, I would like to suppress C1 and C2 and to augment C3 with its inherited attributes and methods).
So, the key problem is to copy into a class (say C2) all the attributes and the methods inherited from a super class (say C1). The real issue appears when dealing with the constructors of C1.
Let's consider the following situation, and the two solutions I've tried to adopt:
public class C1 { private int fieldc1; public C1(int b) { fieldc1 = b; } } class C2 { public C2(int i) { super(i); ... } } //----------- Solution1: //----------- C2 must be modified into: class C2 { private int fieldc1; public C2(int i, int dummy) { ... } // dummy is added. Problem1 : how to add a parameter to C2 using Javassist, because I have evil with the use of setAttribute. // Problem 2: the use of the primitives types like (int, string, float..) for dummy can pose a problem of conflict in the signatures public C2(int i) { this(i, 0); ... } // Problem 3: How to localize the call to "super", using Javassist, in order to substitute it by "this" // Problem 4: How to recognize the correct constructor of the super class (if many) which corresponds to that call // Problem 5: How to replace super(i) by this(i, 0), using Javassist // I manage to change "super" into "this" with setSuperClass but I am not able to modify the signature, i.e. to change this(i) into this(i, 0) } //----------- Solution2: //----------- C2 must be modified into: public class C2 { private int fieldc1; public C2(int i) { fieldc1 = b; //Code substitution. Problem: Can we substitute a line with a portion of source code? How, using Javassist? ... } }