1. 程式人生 > >Java類文件結構及javac的ClassReader類解讀

Java類文件結構及javac的ClassReader類解讀

ade add present ann oca max app contain sig

首先來看一下ClassFile,類註釋如下:

   A JVM class file.

Generic Java classfiles have one additional attribute for classes,
methods and fields:

"Signature" (u4 attr-length, u2 signature-index)


A signature gives the full Java type of a method or field. When
used as a class attribute, it indicates type parameters, followed

by supertype, followed by all interfaces.

methodOrFieldSignature ::= type
classSignature ::= [ typeparams ] supertype { interfacetype }

The type syntax in signatures is extended as follows:

type ::= ... | classtype | methodtype | typevar
classtype ::= classsig { ‘.‘ classsig }

classig ::= ‘L‘ name [typeargs] ‘;‘
methodtype ::= [ typeparams ] ‘(‘ { type } ‘)‘ type
typevar ::= ‘T‘ name ‘;‘
typeargs ::= ‘<‘ type { type } ‘>‘
typeparams ::= ‘<‘ typeparam { typeparam } ‘>‘
typeparam ::= name ‘:‘ type

This class defines constants used in class files as well

as routines to convert between internal ``.‘‘ and external ``/‘‘
separators in class names.

一些常量定義(由虛擬機嚴格規定)如下:

public final static int JAVA_MAGIC = 0xCAFEBABE;  // 魔數

    // 常量池中14種常量項
    public final static int CONSTANT_Utf8 = 1;
    public final static int CONSTANT_Unicode = 2;
    public final static int CONSTANT_Integer = 3;
    public final static int CONSTANT_Float = 4;
    public final static int CONSTANT_Long = 5;
    public final static int CONSTANT_Double = 6;
    public final static int CONSTANT_Class = 7;
    public final static int CONSTANT_String = 8;
    public final static int CONSTANT_Fieldref = 9;
    public final static int CONSTANT_Methodref = 10;
    public final static int CONSTANT_InterfaceMethodref = 11;
    public final static int CONSTANT_NameandType = 12;
    public final static int CONSTANT_MethodHandle = 15;
    public final static int CONSTANT_MethodType = 16;
    public final static int CONSTANT_InvokeDynamic = 18;

    public final static int MAX_PARAMETERS = 0xff; // max_parameters
    public final static int MAX_DIMENSIONS = 0xff; // max_dimensions
    public final static int MAX_CODE = 0xffff; // max_code
    public final static int MAX_LOCALS = 0xffff; // max_locals
    public final static int MAX_STACK = 0xffff; // max_stack

ClassReader類的註釋如下:

 This class provides operations to read a classfile into an internal
representation. The internal representation is anchored in (立足於) a
ClassSymbol which contains in its scope symbol representations
for all other definitions in the classfile.

Top-level Classes themselves appear as members of the scopes of PackageSymbols.

Error Diagnoses
Buffer Access
Constant Pool Access
Reading Types
Reading Attributes
Reading Java-language annotations
Reading Symbols
Adjusting flags
Loading Classes
Loading Packages
 

參考文章:

http://blog.csdn.net/column/details/jvm-principle.html

Java類文件結構及javac的ClassReader類解讀