Chapter 3 file structure and use of javap, Chapter 3 structure javap

Source: Internet
Author: User

Chapter 3 file structure and use of javap, Chapter 3 structure javap

Note: This article is mainly based on the "deep understanding of Java Virtual Machine (version 2)"

 

1. Use of javap and class file structure

Usage process:

Java source code:

1 package compile; 2/** 3 * class bytecode 4 */5 public class TestClass {6 private int m; 7 8 public int inc () {9 return m + 1; 10} 11}View Code

Find the directory of the java source file on the hard disk (eg. E: \ Java \ workspaceOfMyBatis3 \ baseUtil \ src \ compile)

Open the command window and execute"Javac-g TestClass. java"Generate the TestClass. class bytecode file, and then use"Javap-c TestClass> TCC.txt"Output the processing result of the code file to tcc.txt.

Open tcc.txt as follows:

Compiled from "TestClass. java "public class compile. testClass extends java. lang. object {public compile. testClass (); Code: 0: aload_0 1: invokespecial #1; // Method java/lang/Object. "<init>" :() V 4: return public int inc (); Code: 0: aload_0 1: getfield #2; // Field m: I 4: iconst_1 5: iadd 6: ireturn}View Code

Note:

  • Javac-g TestClass. java
    • -G: generate all debugging information, including local variable name and line number information.
  • Javap-c TestClass> TCC.txt. Common javap parameters:
    • -C: Output bytecode Code
    • -L (lower case L): Output Code, LineNumberTable, and LocalVariableTable
    • -S: Output Method signature (list of parameters and return values of the method)
    • -Verbose: contains-c,-l, and the compiled version of the output class file, constant pool, Stack, Locals, Args_size
  • For javap,-c or-verbose is commonly used.

"Javap-verbose TestClass> TCV.txtResult:

Compiled from "TestClass. java "public class compile. testClass extends java. lang. object SourceFile: "TestClass. java "/* source file name */minor version: 0/* minor version */major version: 50/* major version number, 50 --> jdk6 */Constant pool: /* constant pool: stores all method names, field names, and method signatures (method parameters + return values), type name, constant value in the class file */const #1 = Method #4. #18; // java/lang/Object. "<init>" :() V const #2 = Field #3. #19; // compile/TestClass. m: I const #3 = class #20; // compile/TestClass const #4 = class #21; // java/lang/Object const #5 = Asciz m; /* field name */const #6 = Asciz I;/* type name */const #7 = Asciz <init>;/* method name (const) */const #8 = Asciz () V;/* method signature (method parameter + return value) */const #9 = Asciz Code; const #10 = Asciz LineNumberTable; /* constant value in the class file: correspondence between the line number of the Java source code and the bytecode command */const #11 = Asciz LocalVariableTable;/* constant value in the class file: local variable table */const #12 = Asciz this; const #13 = Asciz Lcompile/TestClass;/* type of the current class "Lxxx; "indicates the xxx reference type */const #14 = Asciz inc;/* method name */const #15 = Asciz () I;/* method signature (method parameter + return value) */const #16 = Asciz SourceFile;/* constant value in the class file: source file name */const #17 = Asciz TestClass. java;/* constant value in the class file: source file name */const #18 = NameAndType #7: #8; // "<init> ":() V const #19 = NameAndType #5: #6; // m: I const #20 = Asciz compile/TestClass; /* type name */const #21 = Asciz java/lang/Object;/* type name */{public compile. testClass (); Code:/* method bytecode * // * Stack: depth of the operand Stack (this value is the depth allocated to the operand Stack in the class loading phase) * Locals: the allocation space of local variables (the unit is slot, not the number). For 64bit double and long, two slots are required. For other <= 32bit, only one slot * Args_size is required: Number of method parameters, including method parameters, this (this is only for instance methods, static method does not automatically add this) */Stack = 1, locals = 1, Args_size = 1 0: aload_0/* Push the local variables of the reference type in the 0th slots to the top of the operand stack. Here this */1: invokespecial #1 of LocalVariableTable; // Method java/lang/Object. "<init>" :() V/* invokespecial #1: Call the method represented by #1 constant. Here is super (), the element at the top of the current stack acts as the receiver of this method #1 */4: return/* to return this method. The return value of this method is Void, and the return command is executed, method end */LineNumberTable:/* correspondence between the line number of the Java source code and the bytecode command */line 5: 0 LocalVariableTable: /* local variable table */Start Length Slot Name Signature 0 5 0 this Lcompile/TestClass; public int inc (); Code: Stack = 2, Locals = 1, args_size = 1 0: aload_0/* Push the local variables of the reference type in the 0th slots to the top of the operand stack. Here is this */1: getfield #2 of LocalVariableTable; // Field m: I/* getfield #2: Obtain the #2 instance (instance m) defined in the constant table, and then push m to the top of the operand stack */4: iconst_1/* press an int constant 1 */5 to the top of the stack: iadd/* to add the two elements at the top of the stack (here is 1 and m ), then press the result to the top of the stack */6: ireturn/* and return the int value result of the top stack from the current method */LineNumberTable: line 9: 0 LocalVariableTable: start Length Slot Name Signature 0 7 0 this Lcompile/TestClass ;}View Code

Note:

  • In the above file, the/* xxx */annotation is added by me. // The annotation is generated by javap itself.
  • You need to know that the above files are not generated *. class file ,*. the content of the class file is a string of hexadecimal characters close to the machine code. It starts with a magic number "0 xCAFEBABE", which is the standard for determining whether a file is a class file. After that, the minor version (major version.pdf) is compiled by class. The order at the bottom is the same as that at tcv.txt.
  • In the tcv.txt file, a no-argument constructor method is added. The no-argument constructor calls the no-argument constructor of the parent class Object of TestClass (that is, super () is executed () method). The parameter-free constructor is added in step 3 "Semantic Analysis" of javac variation. For details, refer to Chapter 2 Javac compilation principles.

Note:

  • Constant pool storage content
    • Store all method names
    • Field name
    • Method signature (method parameter + return value)
    • Type name
    • Constant Value in the class file
  • The first four parts of the constant pool can be called symbolic references (that is, there are only some names but no actual addresses. After classes are loaded at runtime, the actual memory will be allocated for these things, then the symbolic reference will be converted to a direct reference, and it will be used by JVM)
  • Composition of the constant pool: Symbol reference, constant (this constant contains the constants defined in our code, such as eg, string constants, and constants in the class file, such as eg. SourceFile ).
  • For details about the primary version number (eg.50 corresponds to JDK 6 and 51 corresponds to JDK 7), see P167
  • Stack: depth of the operand Stack (this value is the depth allocated to the operand Stack during class loading)
  • Locals: space allocated for local variables (in slot, not a number). For 64-bit double and long variables, two slots are required. For other variables <= 32bit, only one slot is required.
  • Args_size: Number of method parameters, including method parameters and this (this is only for instance methods, static method does not automatically add this)
  • Inc () method: I have commented out the execution process of this method in detail, which is the basic process (based on stacks) for JVM to execute a method)

Reminder:

  • The Code part is our main concern. The key part of this part is what the meaning of each bytecode instruction is. Detailed view of the "in-depth analysis of Java Web technology insider (revision)" P124-P135

Summary:

  • Understanding the class file structure helps us understand the class loading mechanism, and understand the class loading mechanism. The most direct benefit is that we can compile the class loading tool by ourselves, for example, the smarty framework is a Class Loader
  • Reading the bytecode commands after executing javap helps us understand the java code execution process and has some benefits for us to locate the problem (although I have not used this method in development)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.