重新定义从另一个编译方法调用的方法的方法签名



我试图用具有不同返回类型的不同方法替换方法,但似乎我能成功做到这一点的唯一方法是拥有所有方法的源代码,这些方法对被替换的方法具有调用站点。我甚至尝试检测调用方法来调用新的替换方法,但我得到一个VerifyError(操作数堆栈上的坏类型)。是否有一种方法,只使用字节码,而不依赖于源代码时,重建依赖的方法?

功能示例(带源代码依赖)

public class OverrideTest {  
    final ClassPool POOL = ClassPool.getDefault();  
    class Foo {  
        public Integer getFoo() { return new Integer(1); }  
        public String doA() { return getFoo().toString(); }  
    }  
    class FooReplacement {  
        public String getFoo() { return "A"; }  
    }  
    @Test  
    public void d() throws Throwable {  
        CtClass srcClass = POOL.getCtClass(Foo.class.getName());  
        CtClass extClass = POOL.getCtClass(FooReplacement.class.getName());  
        CtClass d = POOL.makeClass("Derp");  
        CtClass STRING = POOL.get("java.lang.String");  
        CtClass INT = POOL.get("java.lang.Integer");  
        {  
            CtMethod doA1 = srcClass.getMethod("doA", Descriptor.ofMethod(STRING, new CtClass[0]));  
            CtMethod getFoo1 = srcClass.getMethod("getFoo", Descriptor.ofMethod(INT, new CtClass[0]));  
            CtMethod getFoo = new CtMethod(INT, "getFoo", new CtClass[0], d);  
            CtMethod doA = new CtMethod(STRING, "doA", new CtClass[0], d);  
            d.addMethod(doA);  
            d.addMethod(getFoo);  
            doA.setBody(doA1, null);  
            getFoo.setBody(getFoo1, null);  
            d.setModifiers(d.getModifiers() & ~Modifier.ABSTRACT);  
            d.removeMethod(getFoo);  
            CtMethod getFooReplaced = new CtMethod(STRING, "getFoo", new CtClass[0], d);  
            d.addMethod(getFooReplaced);  
            CtMethod getFooReplaced1 = extClass.getMethod("getFoo", Descriptor.ofMethod(STRING, new CtClass[0]));  
            getFooReplaced.setBody(getFooReplaced1, null);  
            doA.setBody("{ return getFoo().toString(); }");  
            d.setModifiers(d.getModifiers() & ~Modifier.ABSTRACT);  
        }  
        {  
            Class<?> c = d.toClass();  
            Constructor<?> ctor = c.getConstructor();  
            Object derp = ctor.newInstance();  
            Method getFoo = derp.getClass().getMethod("getFoo");  
            Method doA = derp.getClass().getMethod("doA");  
            Object doResult = doA.invoke(derp);  
            Object getResult = getFoo.invoke(derp);  
            assertEquals("A", getResult);  
            assertEquals("A", doResult);  
        }  
    }  
}  

非功能示例(VerifyError)

public class OverrideTest {  
    final ClassPool POOL = ClassPool.getDefault();  

    class Foo {  
        public Integer getFoo() { return new Integer(1); }  
        public String doA() { return getFoo().toString(); }  
    }  

    class FooReplacement {  
        public String getFoo() { return "A"; }  
    }  

    @Test  
    public void d() throws Throwable {  
        CtClass srcClass = POOL.getCtClass(Foo.class.getName());  
        CtClass extClass = POOL.getCtClass(FooReplacement.class.getName());  
        CtClass d = POOL.makeClass("Derp");  
        CtClass STRING = POOL.get("java.lang.String");  
        CtClass INT = POOL.get("java.lang.Integer");  
        {  
            CtMethod doA1 = srcClass.getMethod("doA", Descriptor.ofMethod(STRING, new CtClass[0]));  
            CtMethod getFoo1 = srcClass.getMethod("getFoo", Descriptor.ofMethod(INT, new CtClass[0]));  
            CtMethod getFoo = new CtMethod(INT, "getFoo", new CtClass[0], d);  
            CtMethod doA = new CtMethod(STRING, "doA", new CtClass[0], d);  
            d.addMethod(doA);  
            d.addMethod(getFoo);  
            doA.setBody(doA1, null);  
            getFoo.setBody(getFoo1, null);  
            d.setModifiers(d.getModifiers() & ~Modifier.ABSTRACT);  

            CtMethod tempMethod = new CtMethod(getFoo.getReturnType(), "tempFoo", new CtClass[0], d);  
            d.addMethod(tempMethod);  
            doA.instrument(new MethodReplacer(getFoo, tempMethod));  
            d.removeMethod(getFoo);  

            CtMethod getFooReplaced = new CtMethod(STRING, "getFoo", new CtClass[0], d);  
            d.addMethod(getFooReplaced);  
            CtMethod getFooReplaced1 = extClass.getMethod("getFoo", Descriptor.ofMethod(STRING, new CtClass[0]));  
            getFooReplaced.setBody(getFooReplaced1, null);  

            doA.instrument(new MethodReplacer(tempMethod, getFooReplaced));  
            d.removeMethod(tempMethod);  
            d.removeMethod(doA);  
            CtMethod doA2 = new CtMethod(STRING, "doA", new CtClass[0], d);  
            d.addMethod(doA2);  
            doA2.setBody(doA, null);  
            d.setModifiers(d.getModifiers() & ~Modifier.ABSTRACT);  
        }  
        {  
            Class<?> c = d.toClass();  
            Constructor<?> ctor = c.getConstructor();  
            Object derp = ctor.newInstance();  
            Method getFoo = derp.getClass().getMethod("getFoo");  
            Method doA = derp.getClass().getMethod("doA");  
            Object doResult = doA.invoke(derp);  
            Object getResult = getFoo.invoke(derp);  
            assertEquals("A", getResult);  
            assertEquals("A", doResult);  
        }  
    }  

    class MethodReplacer extends ExprEditor {  
        private CtMethod replacedMethod;  
        private CtMethod replacement;  

        MethodReplacer(CtMethod replacedMethod, CtMethod replacement) {  
            this.replacedMethod = replacedMethod;  
            this.replacement = replacement;  
        }  

        @Override  
        public void edit(MethodCall mcall) throws CannotCompileException {  
            CtClass declaringClass = replacedMethod.getDeclaringClass();  
            try {  
                CtMethod m = mcall.getMethod();  
                if (declaringClass.equals(m.getDeclaringClass()) && m.equals(replacedMethod))  
                    mcall.replace("$_ = " + replacement.getName()+"($$);");  
            } catch (NotFoundException e) {  
                throw new RuntimeException("Unable to instrument a dependent method call to " + replacedMethod.getName(), e);  
            }  
        }  

    }  
}  

当你用一个方法编译一个类时:

class Foo {
  Object bar() { }
}

这个方法的描述符是方法签名的一部分,编译后看起来像这样:

()Ljava/lang/Object;

注意返回类型是方法签名的一部分!因此,对该方法的任何调用都是显式地对返回类型为Object的实例的方法进行调用。如果您将返回类型更改为例如Integer,则描述符将改为()Ljava/lang/Integer;

如果您已经编译了一个类Qux,用()Ljava/lang/Object;描述符调用Foo的方法,但随后将Foo中的方法更改为返回Integer,则JVM无法在不重新编译Qux的情况下调度方法调用:对于JVM, Qux正在调用的方法不再存在。当您使用javassist之类的工具只"重新编译"单个方法而不重新编译调用第一个方法的方法时,同样的问题也适用于Foo中定义的另一个方法。这就是你的例子中验证者抱怨的问题。

但是,Java编译器知道用于此目的的桥接方法的概念,其中Foo看起来像表示生成的Java字节码的伪Java代码。

class Foo {
  Object bar() { this.[Integer]bar(); }
  Integer bar() { }
}

,其中this.[Integer]bar();表示对bar方法的调用,该方法返回一个Integer[Object]bar()方法在这个上下文中代表一个桥接方法。您可以自己模拟这样一个桥接方法的创建,方法是首先重新定义方法签名以返回不同的类型,然后添加一个具有原始返回类型的桥接方法。

最新更新