質問

I am trying to figure out the following two entries under the constant pool section in the class file:

int foo(int x) {
    return x;
}

#11 = Utf8               foo
#12 = Utf8               (I)I        // 1   
#13 = Utf8               x
#14 = Utf8               I           // 2

Does the entry (1) -> (I)I denotes start of the function and the entry (2) -> I denotes end of function.

Is this compiler specific or could vary from one compiler to another?

Similar question for Lambda functions as well.

interface I { int foo(int x); }
class X {
I x = (int x1) -> 0;
}

 #15 = Utf8            lambda$new$0
 #16 = Utf8            (I)I
 #17 = Utf8            x1
 #18 = Utf8            I

Sorry Could not find anything on JLS docs.

Thanks

役に立ちましたか?

解決

The constant pool does not have a concept of "method start" and "method end".

What you are actually seeing here are string constants, which have the type "Utf8" in the constant pool. From the pool's point of view, these are just strings -- however, they are compiler-generated strings representing various things. These strings are required and their format is described in the JVMS. There are a few types. From your lambda example:

  • lambda$new$0 - This is a signature, described in 4.3.4.
  • (I)I - This is a method descriptor (not the same as a signature), described in 4.3.3
  • I - This is a field descriptor, described in 4.3.2.

The simple I entry is just a field of type int.

For your (I) I entry, the string between the (..) is the parameter type. It can be a list. I is the primitive type int. The string outside the (..) is the return type. So (I) I describes a method with a parameter list accepting an int and a return type of int.

Specifically, from the JVMS, method descriptor strings have the format:

MethodDescriptor:
    ( ParameterDescriptor* ) ReturnDescriptor

ParameterDescriptor:
    FieldType

ReturnDescriptor:
    FieldType
    VoidDescriptor

VoidDescriptor:
    V

Where FieldType is (from table 4.2):

Character     Type       Interpretation
B             byte       signed byte
C             char       Unicode character code point in the Basic 
                           Multilingual Plane, encoded with UTF-16
D             double     double-precision floating-point value
F             float      single-precision floating-point value
I             int        integer
J             long       long integer
L ClassName;  reference  an instance of class ClassName
S             short      signed short
Z             boolean    true or false
[             reference  one array dimension

Note, however, that these are still strings like any other. When you actually call these methods, a different kind of entry - a method reference - will appear in the constant pool. javap identifies these with the "NameAndType" type (as opposed to "Utf8" for strings), and they will take forms like (using your first example):

#123 = NameAndType      #11:#12;   // "foo":(I)I

They reference the method name string as well as the descriptor string.

The javap documentation itself is rather sparse and contains only command line information. The JVMS is the closest thing to a detailed manual that javap has.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top