Method Signature Ambiguity
alex.buckley at oracle.com
Mon Sep 13 23:39:24 UTC 2021
A method signature in Java is as defined by JLS 8.4.2.
Since method signatures in Java are erased when creating method
descriptors in a class file (JVMS 4.3.3), it is useful to record the
unerased method signatures in the class file. This allows, for example,
java.lang.reflect.Method::getGenericParameterTypes to expose an unerased
method signature. The Signature attribute (JVMS 188.8.131.52) records the
unerased method signatures, among other things.
Frankly the Signature attribute is rather obscure and you can mostly
forget about it and its method signatures. The primary confusion I have
seen over the years is between a method signature in Java (does not
include return type) and a method descriptor in the class file (does
include return type). When speaking about class files, many people say
"method signature" when they mean "method descriptor", and it's then
unclear whether they mean to care about the return type. This shows that
the presence of clear terminology is no guarantee of its proper use.
On 9/13/2021 5:59 AM, G G wrote:
> Sorry if this is the wrong place for this email, I'm fairly new to
> OpenJDK projects. I was recently asked "What is a method signature in
> Java?" Having recently worked with bytecode, I was confident that my
> JVMS (§184.108.40.206
> based knowledge would be correct in this fashion:
> A/method signature/encodes type information about a (possibly
> generic) method declaration. It describes any type parameters of the
> method; the (possibly parameterized) types of any formal parameters;
> the (possibly parameterized) return type, if any; and the types of
> any exceptions declared in the method's|throws|clause.
> However, after a quick Google search I was able to uncover that the JLS
> (§ <http://goog_6165633>8.4.2
> gave a different definition:
> Two methods or constructors,|M|and|N|, have the/same signature/if
> they have the same name, the same type parameters (if any) (§8.4.4
> and, after adapting the formal parameter types of|N|to the type
> parameters of|M|, the same formal parameter types.
> While both are consistent with the implementation, I believe that the
> two disagree on the question of "What is a method signature in Java?"
> After asking a few other people, I found that I was told each definition
> a few times, so I do believe that I am not entirely alone in this confusion.
> My suggestion for this is to rename one or both of the names. The JLS
> definition could be changed to "override method signature", as the
> definition seems to be defined around that. The JVMS definition could
> steal a page out of what Wikipedia currently calls it (not backed up by
> official documentation as far as I know) and call it an "internal method
> I may be completely off base here, if so at the very least it will be a
> learning experience for me and a few others.
More information about the compiler-dev