Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute a Java function's signature

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?

like image 731
Niklas R Avatar asked Nov 09 '11 14:11

Niklas R


People also ask

What is a signature of a function in Java?

The method signature in java is defined as the structure of the method that is designed by the programmer. The method signature is the combination of the method name and the parameter list. The method signature depicts the behavior of the method i.e types of values of the method, return type of the method, etc.

What is the function's signature?

A function's signature includes the function's name and the number, order and type of its formal parameters. Two overloaded functions must not have the same signature. The return value is not part of a function's signature.

What is class signature in Java?

A signature is a list that specifies a class constructor, an instance method, or a static method, thereby distinguishing it from other constructors, instance methods, or static methods.


2 Answers

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.

like image 90
Ernest Friedman-Hill Avatar answered Sep 29 '22 20:09

Ernest Friedman-Hill


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.

like image 32
asloob Avatar answered Sep 29 '22 18:09

asloob