简体   繁体   English

如何在java字节码中修改局部变量的类型

[英]How to modify type of a local variable in java byte code

I am rewriting bytecode using Javassist. 我正在使用Javassist重写字节码。 I need to modify type of some local variables to object. 我需要修改一些局部变量的类型来对象。 How can I access local variables and change their types using Javassist? 如何使用Javassist访问局部变量并更改其类型?

Thanks. 谢谢。

This question is quite old but since I did something similar I'll post my solution: 这个问题已经很老了但是因为我做了类似的事情,我会发布我的解决方案:

Since I did not found any solution to change the type of a field in a class in Javassist, I just deleted the old method and added a new one with the same name but my desired type: 由于我没有找到任何改变Javassist中类的字段类型的解决方案,我只是删除了旧方法并添加了一个具有相同名称但我想要的类型的新方法:

CtClass point = ClassPool.getDefault().get("Point");
CtField toBeDeleted = point .getField("fieldName");
point .removeField(toBeDeleted);
CtField newField = CtField.make("public int fieldName = 0;", point);
point.addField(newField );

So with this example I took the field fieldName in class point that was of type let's say Object and know has been changed in a field of type 'int' and initialized to 0 所以在这个例子中,我在类point中获取了字段fieldName ,类型为Object并且知道已在类型为'int'的字段中更改并初始化为0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM