计算 Java 函数的签名
有没有办法计算 Java 类的方法签名?签名
就像 ([Ljava/lang/String;)V
表示一个以 String[]
作为参数的函数
并返回void
。
计算签名的规则是什么?
Is there a way to compute a Java class's method's signature? A signature
like ([Ljava/lang/String;)V
represents a function that takes a String[]
as argument
and returns void
.
What's the rule to compute the signature?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它始终是一组括号,包围着参数的类型指示符,一个接一个,没有逗号或任何其他内容,紧接着是右括号后面的返回值的类型指示符。这非常简单。
这是一个类型签名表:
最后两个意味着要命名一个类,例如,
Ljava/lang/Object;
,并命名一个数组(例如)int
,你说[I
,而int
的数组的数组就是[[I
。如果您想根据反射来计算 Java 代码中的签名,那就很简单了;只需使用上面的表格以及处理对象和数组的规则即可。
It's always a set of parentheses enclosing type signifiers for the arguments, one after the other with no commas or anything, followed by a type signifier for the return value after the closing paren. It's pretty straightforward.
Here’s a table of type signatures:
Those last two mean that to name a class, you say, for example,
Ljava/lang/Object;
, and to name an array of (for example)int
, you say[I
, and an array of array ofint
is[[I
.If you wanted to literally compute the signature in Java code based on reflection, it'd be simple enough; just use the table above with rules for handling objects and arrays.
只需在包含
.class
文件的文件夹中运行javap -s
即可。它会100%准确地告诉你。这些事情无需猜测。Just run
javap -s <class-name>
in the folder containing the.class
files . It will tell you with 100% accuracy. No need to guess these things.快速谷歌搜索发现了这个网页:
http://www.rgagnon.com/javadetails/ java-0286.html
A quick google search uncovered this webpage:
http://www.rgagnon.com/javadetails/java-0286.html
请参阅此处了解一些详细信息。
基本上是参数,然后返回值。
See here for some details.
Basically it's params, then return value.
来自 JLS,§8.4.2:
所以“规则”是
From the JLS, §8.4.2:
So the "rule" is
您可以在 Java 虚拟机中找到此信息规格
You can find this information in the the Java Virtual Machine Specification
您可以使用 apache commons-bcel
输出以编程方式完成此操作
You can do it programmatically by using apache commons-bcel
outputs