使用 javassist 添加方法参数名称

编程入门 行业动态 更新时间:2024-10-23 13:30:12
本文介绍了使用 javassist 添加方法参数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当方法参数名称不存在时,如何将方法参数名称添加到方法中?

How can I add method parameter names to a method, when none exists?

有一些示例说明如何使用以下方法检索这些名称(如果存在):

There are some examples on how to retrieve these names, if they exist, with a method like this:

CtMethod m; CodeAttribute codeAttribute = m.getMethodInfo().getCodeAttribute(); if (codeAttribute != null) { LocalVariableAttribute table = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag); if (table != null) for (int i = 0; i < table.tableLength(); i++) m.getMethodInfo().getConstPool().getUtf8Info(table.nameIndex(i)); }

这将给出方法的所有参数名称(如果存在).

This will give all the parameter names of a method, if they exist.

我怎样才能做相反的事情?

How can I do the reverse?

推荐答案

Javassist 不允许删除方法或字段,但它允许更改名称.所以如果一个方法不再需要,它应该通过调用 setName() 和CtMethod 中声明的 setModifiers().

Javassist does not allow to remove a method or field, but it allows to change the name. So if a method is not necessary any more, it should be renamed and changed to be a private method by calling setName() and setModifiers() declared in CtMethod.

Javassist 不允许添加现有方法的额外参数.而不是做即,一种接收额外参数以及其他参数的新方法参数应该添加到同一个类中.例如,如果你想向方法添加额外的 int 参数 newZ:

Javassist does not allow to add an extra parameter to an existing method, either. Instead of doing that, a new method receiving the extra parameter as well as the other parameters should be added to the same class. For example, if you want to add an extra int parameter newZ to a method:

void move(int newX, int newY) { x = newX;y = newY;}

在一个 Point 类中,那么你应该在 Point 类中添加以下方法:

in a Point class, then you should add the following method to the Point class:

void move(int newX, int newY, int newZ) {

void move(int newX, int newY, int newZ) {

// do what you want with newZ. move(newX, newY);

}

有关更多信息,请查看此jboss-javassist.github.io/javassist/tutorial/tutorial2.html

for more information, check this jboss-javassist.github.io/javassist/tutorial/tutorial2.html

更多推荐

使用 javassist 添加方法参数名称

本文发布于:2023-10-31 06:25:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545286.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:名称   参数   方法   javassist

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!