看起来第一个屏幕截图上的代码从根本上被打破了. JVM字节码中的对象构造可以分为两个阶段:在堆上分配内存并针对分配的内存调用构造函数(带有可选参数):
new #1 //allocate memory
dup //duplicate to not loose the object after calling constructor
//push c-tor args onto the stack here
invokespecial > //constructor, second `this` is lost
areturn //returns first `this`
你基本上做的是:在堆上分配一些原始(可能是零)内存并对该内存块调用虚方法Fraction_congu.你还没有调用构造函数!
还应该有invokevirtual,我猜这个生成的方法是私有的.
更新:我假设您要转换以下类:
class Fraction {
public Fraction(float den, float num) {
//original constructor code here
}
public int m() {
return 1;
}
}
成:
class Fraction {
public Fraction(float den, float num) {
//proxy method
//place for extra logic
Fraction_CONGU(den, num);
}
private Fraction_CONGU(float den, float num) {
//original constructor code here
}
public int m() {
//proxy method
//place for extra logic
return m_CONGU
}
private int m_CONGU() {
return 1;
}
}
如你所见,这是完全可能的(如果我的想法正确).只需编译此Java代码,看看编译器如何实现它.
这引出了一个问题:你不能只使用AspectJ进行编译时编织吗?