获取方法对象而不按名称寻址方法
有没有一种方法可以获取方法对象,而不必使用方法的名称来获取它?
例如,我有一个类:
class Car {
public String drive();
public String giveUp();
public String fillUp();
}
我想创建一个 Map
方法(又名:("move",drive()),("name",giveUp()) ,....)。
由于使用了混淆,我无法通过名称获取方法对象。有没有办法获取方法名称而不必绑定它?
我想问这个问题的另一种方式是:
对于一个有 getClass() 的类,是否有等效的方法?我正在寻找与 GiveUp.Method 类似的东西。
Is there a way to get a method object without having to use the Method's name to grab it?
For example I have the class:
class Car {
public String drive();
public String giveUp();
public String fillUp();
}
I would like to create a Map<String, Method>
of methods (aka: ("move", drive()), ("name", giveUp()), ....).
I'm not able to get the method object via name due to obfuscation being used. Is there a way to grab the method name without having to bind this?
I guess another way of asking this is:
For a class you have getClass(), is there an equivalent for methods? I'm looking for something allong the lines of giveUp.Method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java 中没有像
Car.giveUp.method()
这样的构造,因为方法不像类和对象那样是“一等公民”。如果不知道混淆器对您的代码做了什么,或者添加其他信息,您就无法区分这些方法,因为除了它们的名称之外,它们具有相同的签名。
一些混淆器生成将原始名称映射到混淆后名称的文本文件,您可以使用该映射文件在运行时识别混淆后的方法。
一些
您可以向方法添加注释,例如
带有一个自写的注释
@MappedMethod
和一个String
类型的默认属性。然后使用反射获取所有方法及其注解,并使用注解值作为键。There is no such construct as
Car.giveUp.method()
in Java, because methods are not "first-class citizens" like classes and objects.Without knowing what the obfuscator does to your code, or adding additional information, you cannot distinguish the methods, because apart from their name, they have the same signature.
Some obfuscators produce text files that map the original name to the obfuscated name, and you could use that map file to identify the obfuscated method at runtime.
You could add an annotation to the method, like
with a self-written annotation
@MappedMethod
and a default attribute of typeString
. Then use reflection to get all methods and their annotations, and use the annotation value as key.您可以使用反射来获取所有方法。
您可以迭代方法并映射它们:
You can use Reflection to get all the methods.
Them you iterate over methods and map them: