JVM学习笔记-方法(Methods)

 

Methods

Each method declared in a class or interface or generated by the compiler is described in the class file by a method_info table. The two types of compiler-generated methods that may appear in class files are instance initialization methods (named <init) and class initialization methods (named <clinit). For more information on the compiler-generated methods, see Chapter 7, "The Lifetime of a Class." The format of the method_info table is shown in Table 6-22.

 

在class文件中,每个在类和接口中声明的方法,或者由编译器产生的方法,都由一个可变长度的method_info表来描述。同一个类中不能存在两个名字及描述符完全相同的方法。需要注意的是,在java程序设计语言中,尽管在同一个类或者接口中声明的两个方法不能有同样的特征签名(除返回类型之外的描述符),但在同一个class文件中,两个方法可以拥有同样的特征签名,前提是它们的返回值不能相同。换句话说:在Java源文件的同一个类里,如果声明了两个具有相同名字和相同参数类型、但返回值不同的方法,这个程序将无法编译通过。在Java程序设计语言中,不能仅仅通过返回值的不同来重载方法。但是同样的两个方法可以和谐地在一个class文件中共存。

有可能在class文件中出现的两种编译器产生的方法是:实例初始化(名为<init>)和类与接口初始化方法(名为<clinit>)。Method_info表的格式如表6-22所示。

 

 

Table 6-22. Format of a method_info table

Type Name Count
u2 access_flags 1
u2 name_index 1
u2 descriptor_index 1
u2 attributes_count 1
attribute_info attributes attributes_count

Method_info表中各项如下:

access_flags

The modifiers used in declaring the method are placed into the methodís access_flags item. Table 6-23 shows the bits used by each flag.

在声明方法时使用的修饰符存放在方法的access_flags项中,表6-23列出了各个标志所使用的位。1.2版本中加进了ACC_STRICT标志,它指明方法中的所有表达式都必须使用FP-strict模式进行计算。

 

Table 6-23. Flags in the access_flags item of method_info tables

Flag Name Value Meaning if Set Set By
ACC_PUBLIC 0x0001 Method is public Classes and all methods of interfaces
ACC_PRIVATE 0x0002 Method is private Classes only
ACC_PROTECTED 0x0004 Method is protected Classes only
ACC_STATIC 0x0008 Method is static Classes only
ACC_FINAL 0x0010 Method is final Classes only
ACC_SYNCHRONIZED 0x0020 Method is synchronized Classes only
ACC_NATIVE 0x0100 Method is native Classes only
ACC_ABSTRACT 0x0400 Method is abstract Classes and all methods of interfaces

For methods declared in a class (not an interface), at most one of ACC_PUBLIC, ACC_PRIVATE, and ACC_PROTECTED may be set. If a methodís ACC_FINAL flag is set, then its ACC_SYNCHRONIZED, ACC_NATIVE, and ACC_ABSTRACT flags must not be set. If a methodís ACC_PRIVATE or ACC_STATIC flag is set, then its ACC_ABSTRACT flag must not be set. All methods declared in interfaces must have their ACC_PUBLIC and ACC_ABSTRACT flags set.

类(不包括接口)中声明的方法只能拥有ACC_PUBLIC、ACC_PRIVATE、ACC_PROTECTED这三个标志中的一个。如果设定了一个方法的ACC_ABSTRACT标志,那么它的ACC_PRIVATE、ACC_STATIC、ACC_FINAL、ACC_SYNCHRONIZED、ACC_NATIVE以及ACC_STRICT标志都必须清除。接口中声明的所有方法必须有ACC_PUBLIC和ACC_ABSTRACT标志,除此以外接口方法不能使用其他标志,但接口初始化方法(<clinit>)可以使用ACC_STRICT标志。

 

Instance initialization (<init) methods may only use flags ACC_PUBLIC, ACC_PRIVATE, and ACC_PROTECTED. Because class initialization (<clinit) methods are invoked by the Java Virtual Machine, never directly by Java bytecodes, the the access_flags for <clinit methods is ignored.

实例初始化方法(<init>)可以只使用ACC_PUBLIC、ACC_PRIVATE和ACC_PROTECTED标志。因为类与接口初始化方法(<clinit>)只由Java虚拟机直接调用,永远不会被Java字节码直接调用,这样,<clinit>方法的access_flags中的标志位,除去ACC_STRICT之外的所有位都应该被忽略。

 

All unused bits in access_flags must be set to zero and ignored by Java Virtual Machine implementations.

在access_flags中未用到的位都被设为0,Java虚拟机实现也将忽略它们。

name_index

The name_index gives the index of a CONSTANT_Utf8_info entry that gives the simple (not fully qualified) name of the method.

Name_index项提供了CONSTANT_Utf8_info入口的索引,该入口给出了方法的简单名称(不是全限定名)。在class文件中的每一个方法的名称,都必须或者未<init>,或者为<clinit>,或者是Java程序设计语言中有效的方法名称(简单名称,不是全限定名)。

 

descriptor_index

The descriptor_index gives the index of a CONSTANT_Utf8_info entry that gives the descriptor of the method.

 

Descriptor_index提供了CONSTANT_Utf8_info入口的索引,该入口给出了方法的描述符。


attributes_count and attributes

The attributes item is a list of attribute_info tables. The attributes_count indicates the number of attribute_info tables in the list. Three kinds of attributes that are defined by the Java Virtual Machine specification that may appear in this item are Code, Exceptions, and Synthetic. These three attributes are described in detail later in this chapter.

Attributes项是由多个attribute_info表组成的列表。Attribute_count给出了列表中attribute_info表的数量。一个字段在其列表中可以有任意数量的属性。在此项中可能会出现的由Java虚拟机规范定义的四种属性是:Code、Deprecated、Exceptions和Synthetic。这四种属性将在本章后面进一步阐述。Java虚拟机只需要识别Code和Exception属性。虚拟机实现必须忽略任何无法识别的属性。

 

你可能感兴趣的:(method)