我试图用一个具有不同返回类型的不同方法替换一个方法,但似乎我能成功完成它的唯一方法是让所有方法的源代码都有一个被替换方法的调用站点.我甚至尝试使用调用方法来调用新的替换方法,但是我得到了一个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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
非功能示例(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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当您使用单个方法编译类时:
class Foo {
Object bar() { }
}
Run Code Online (Sandbox Code Playgroud)
该方法的描述符是方法签名的一部分,编译后将如下所示:
()Ljava/lang/Object;
Run Code Online (Sandbox Code Playgroud)
请注意,返回类型是方法签名的一部分!因此,对该方法的任何调用都是显式对返回类型实例的方法进行的Object。如果将返回类型更改为例如 an Integer,则描述符将改为()Ljava/lang/Integer;。
如果您编译了一个类来使用描述符Qux调用方法,但随后更改了该方法以返回一个,则在不重新编译的情况下,JVM 无法调度该方法调用:对于 JVM,正在调用的方法不再存在。当您使用 javassist 之类的工具仅“重新编译”单个方法而不重新编译调用第一个方法的方法时,定义的另一个方法也存在同样的问题。这是验证者在您的示例中抱怨的问题。Foo()Ljava/lang/Object;FooIntegerQuxQuxFoo
然而,Java 编译器知道用于此目的的桥接方法的概念,其Foo看起来像表示生成的 Java 字节代码的伪 Java 代码。
class Foo {
Object bar() { this.[Integer]bar(); }
Integer bar() { }
}
Run Code Online (Sandbox Code Playgroud)
编译后,其中this.[Integer]bar();表示对bar返回Integer. 该[Object]bar()方法代表了这种情况下的桥接方法。您可以自己模拟创建此类桥接方法,方法是首先重新定义方法签名以返回不同的类型,然后另外添加具有原始返回类型的桥接方法。
| 归档时间: |
|
| 查看次数: |
1387 次 |
| 最近记录: |