Compute a Java function's signature
Asked Answered
R

7

33

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?

Rompish answered 9/11, 2011 at 14:11 Comment(0)
C
48

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:

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.

Conventioner answered 9/11, 2011 at 14:12 Comment(1)
The link produces a 404Extinguisher
L
13

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.

Liver answered 1/12, 2013 at 22:42 Comment(0)
P
5

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        [ 
Pannell answered 9/11, 2011 at 14:16 Comment(0)
B
1

See here for some details.

Basically it's params, then return value.

Bryology answered 9/11, 2011 at 14:16 Comment(0)
N
1

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

Nephoscope answered 9/11, 2011 at 14:17 Comment(4)
Did I misunderstand the question?Nephoscope
I think the OP was talking about the actual chars used in the short-hand version, although it wasn't explicitly stated, making the downvote a bit harsh.Bryology
It's kind of ironic, IMO, that the OP asks about signatures, but the "shorthand" example provided includes nothing about the method name.Nephoscope
Yeah, I think there's some confusion between a signature and a descriptor, but hey.Bryology
L
1

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

Lalla answered 9/11, 2011 at 14:19 Comment(0)
A
0

Is there a way to compute a Java class's method's signature?

You can do it programmatically by using apache commons-bcel

package com.mageddo.coc.classes;

import java.io.IOException;

import com.sun.org.apache.bcel.internal.classfile.ClassParser;

public class JavaClass {

  public static String structureAsText(Class<?> clazz) throws IOException {

    final String classPath =
        String.format(
            "/%s.class",
            clazz.getName()
                .replace('.', '/')
        );

    final ClassParser classParser = new ClassParser(
        JavaClass.class.getResourceAsStream(classPath),
        clazz.getSimpleName() + ".java"
    );

    
    return classParser.parse()
        .toString();
  }

  public static void main(String[] args) throws IOException {
    System.out.println(JavaClass.structureAsText(JavaClass.class));
  }

}

outputs

public class com.mageddo.coc.classes.JavaClass extends java.lang.Object
filename        JavaClass.java
compiled from       JavaClass.java
compiler version    52.0
access flags        33
constant pool       98 entries
ACC_SUPER flag      true

Attribute(s):
    SourceFile: JavaClass.java

3 methods:
    public void <init>()
    public static String structureAsText(Class clazz) [Signature: (Ljava/lang/Class<*>;)Ljava/lang/String;]
        throws Exceptions: java.io.IOException
    public static void main(String[] args)
        throws Exceptions: java.io.IOException
Aekerly answered 9/7, 2022 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.