What is the difference between "descriptor" and "signature"?
Asked Answered
J

3

21

I am now using ASM (Java bytecode instrumentation library). To retrieve the signature of given method, there is a field which is named "desc". I guess this is an abbreviation of "descriptor", but why isn't it named as "signature"? Is there any difference between "descriptor" and "signature"?

Jeopardize answered 23/9, 2011 at 8:52 Comment(0)
T
18

Looking at the JVM spec section 4.3.3, for one thing the descriptor contains the return type - whereas that isn't part of a the signature of a method.

A method descriptor represents the parameters that the method takes and the value that it returns

but...

Two methods have the same signature if they have the same name and argument types

(Given this, it's also not clear that the descriptor contains the name of the method...)

Tenuis answered 23/9, 2011 at 8:57 Comment(0)
P
29

In the context of asm, you care about internal names, method descriptors, type descriptors and signatures. Section numbers are from the asm doc.

2.1.2 Internal names

"The internal name of a class is just the fully qualified name of this class, where dots are replaced with slashes."

com/snark/Boojum

2.1.3 Type descriptors

[[Ljava/lang/Object;

2.1.4 Method descriptor

A method descriptor is a list of type descriptors that describe the parameter types and the return type of a method, in a single string.

int[] m(int i, String s) becomes (ILjava/lang/String;)[I

4.1. Generics (for signatures)

"For backward compatibility reasons the information about generic types is not stored in type or method descriptors (which were defined long before the introduction of generics in Java 5), but in similar constructs called type, method and class signatures."

This Java:

List<List<String>[]>

Becomes this signature:

Ljava/util/List<[Ljava/util/List<Ljava/lang/String;>;>;
Pindaric answered 6/11, 2012 at 5:13 Comment(2)
This answer is right and clear, why not choose this?Derekderelict
@Derekderelict the answer was from almost a decade ago. It's very, very, very, very.... very unlikely that the original requester remembers anything about it or is working on anything similar these days.Pindaric
T
18

Looking at the JVM spec section 4.3.3, for one thing the descriptor contains the return type - whereas that isn't part of a the signature of a method.

A method descriptor represents the parameters that the method takes and the value that it returns

but...

Two methods have the same signature if they have the same name and argument types

(Given this, it's also not clear that the descriptor contains the name of the method...)

Tenuis answered 23/9, 2011 at 8:57 Comment(0)
S
6

"descriptor" probably refers to the method descriptor as defined in the JVM spec § 4.3.3. It describes the parameter types and the return type of a method. It does not contain the method name.

"signatur" probably refers to the signature of as defined in the Java Language Specification § 8.4.2. It contains the name of the method as well as the parameter types. It does not contain the return type.

Note that those two terms are defined in two different places and at different levels. A method descriptor exists at the JVM-level, so it's pretty detached from the Java language. The signature, however is a very similar concept, but acts on the Java language level (as it's defined in the JLS).

Selfstarter answered 23/9, 2011 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.