Compute a Java function's signature

断了今生、忘了曾经 提交于 2019-12-20 10:22:52

问题


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?


回答1:


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.

There's a table of type signatures on this page:

Signature    Java Type
Z    boolean
B    byte
C    char
S    short
I    int
J    long
F    float
D    double
V    void
L fully-qualified-class ;    fully-qualified-class
[ type   type[]

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 of int 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.




回答2:


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.




回答3:


A quick google search uncovered this webpage:

http://www.rgagnon.com/javadetails/java-0286.html

There are two parts to the signature. The first part is enclosed within the parentheses and represents the method's arguments. The second portion follows the closing parenthesis and represents the return type. The mapping between the Java type and C type is

Type     Chararacter 
boolean      Z 
byte         B 
char         C 
double       D 
float        F 
int          I 
long         J 
object       L 
short        S 
void         V 
array        [ 



回答4:


See here for some details.

Basically it's params, then return value.




回答5:


From the JLS, §8.4.2:

8.4.2 Method Signature

The signature of a method consists of the name of the method and the number and types of formal parameters to the method. A class may not declare two methods with the same signature, or a compile-time error occurs.

The example:

class Point implements Move {
  int x, y;
  abstract void move(int dx, int dy);
  void move(int dx, int dy) { x += dx; y += dy; }
}

causes a compile-time error because it declares two move methods with the same signature. This is an error even though one of the declarations is abstract.

So the "rule" is

the name of the method and the number and types of formal parameters to the method




回答6:


You can find this information in the the Java Virtual Machine Specification



来源:https://stackoverflow.com/questions/8066253/compute-a-java-functions-signature

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!