JVM (iii)-Class structure and class loader

Source: Internet
Author: User

  • class file Structure
    • The class file is a 8-bit byte stream. All 16, 32, and 64 bits of data are stored in Big-endian form with a continuous number of 8 bits.

  • Data type in class file
    • U1, U2, U4 represent unsigned 1, 2, 4 byte data.

    • The 0......* type data will be represented as table.

  • Classfile structure
    • U4 Magic;

    • U2 minor_version;

    • U2 major_version;

    • U2 Constant_pool_count;

    • Cp_info Constant_pool[constant_pool_count-1];

    • U2 access_flags;

    • U2 This_class;

    • U2 Super_class;

    • U2 Interfaces_count;

    • U2 Interfaces[interfaces_count];

    • U2 Fields_count;

    • Field_info Fields[fields_count];

    • U2 Methods_count;

    • Method_info Methods[methods_count];

    • U2 Attributes_count;

    • Attribute_info Attributes[attributes_count];

    • Magic
      • Identify the file type with a value of 0xCAFEBABE.

    • Minor_version, Major_version
      • Minor, major version number of the class file institution

    • Constant_pool_count
      • Value is Onstant_pool length +1

    • Constant_pool[]
      • Chang (indexed by [1,constant_pool_count-1]), stores string constants, class and interface names, field names, and other constants that are referenced by the class file structure and sub-structures.

      • Each cell in a constant pool is labeled with a "tag" byte.

    • Access_flags
      • Access permission Flags

      • Class and Interface access rights indicator

      • Acc_public 0x0001 declared public; May is accessed from outside it package.

      • Acc_final 0x0010 declared FINAL; No subclasses allowed.

      • Acc_super 0x0020 Treat Superclass methods specially when invoked bythe invokespecial instruction.

      • Acc_interface 0x0200 is an INTERFACE, not a class.

      • Acc_abstract 0x0400 declared ABSTRACT; must not being instantiated.

      • Acc_synthetic 0x1000 declared synthetic; Generated by a compiler and not present in the source code.

      • Acc_annotation 0x2000 declared as an ANNOTATION type.

      • Acc_enum 0x4000 declared as an ENUM type.

      • The Acc_enum flag indicates, the this class, or its superclass are declared as an enumerated type.

      • An annotation type must has its acc_annotation flag set. If the Acc_annotation flag is set, the ACC_INTERFACE flag must are set as well. IF Theacc_interface Flag of this class file was not set, it may have any of the the other flags in Table 4.1 set, except the AC C_annotation flag. However, such a class file cannot has both its acc_final and Acc_abstract flags set (jls§8.1.1.2).

      • The Acc_super flag indicates which of the alternative semantics are to being expressed by the invokespecial instruction (§invo kespecial) If it appears in the This class. Compilers to the instruction set of the Java Virtual machine should set the ACC_SUPER flag.

      • The ACC_SUPER flag exists for backward compatibility and code compiled by older compilers for the Java programming Langua Ge. In Oracle's JDK prior to release 1.0.2, the compiler generated Classfile access_flags in which the flag now representing A Cc_super had no assigned meaning, and Oracle ' s Java Virtual machine implementation ignored the flag if it is set.

      • All bits of the Access_flags item is not assigned in Table 4.1 is reserved for future use. They should is set to zero in generated class files and should is ignored by Java Virtual machine implementations.

    • This_class
      • Must be a valid index number for a constant pool, pointing to the constant_class_info structure of the class file.

    • Super_class
      • 0 or not 0. If it is not 0, it must be a valid index number for a constant pool, pointing to the constant_class_info structure of the superclass.

    • Interfaces_count
      • Indicates the number of interfaces for the direct parent class, interface.

    • Interfaces[]
      • [0,interfaces_count], which points to the constant_class_info structure of the hyper-interface.

    • Fields_count
      • Indicates the number of class variables, instance variables, defined in the class or interface in the FIELD_INFO structure.

    • Fields[]
      • The FIELD_INFO structure of variables defined in this class is stored. Excludes parts defined in superclass and hyper-interfaces.

    • Methods_count
      • The number of method_info structures in the method table.

    • Methods[]
      • The method table stores the method_info structure of the methods defined in the class or interface. Methods inherited from superclass and hyper-interfaces are not included.

    • Attributes_count
      • The number of data in the attributes.

    • Attributes[]
      • Slightly. See section 4.7 in Jvms7.

  • Load, connect, and initialize
    • Reference from http://www.cnblogs.com/mengdd/p/3562003.html

    • Run a constant-rate pool
      • Where the reference is initialized to a symbolic reference.

    • Load
      • Look for a class or interface binary file and create it in memory.

    • Connection
      • Converts the loaded class or interface to a state that can be run in the JVM.

      • Check
      • Check the class structure.

      • Get ready
      • Initializes a static field for the class or interface as the default value.

      • Analytical
      • The symbol reference is converted to a direct reference.

    • Initialization
    • The initialization of a class or interface, including methods that run a class or interface. When a Java Virtual machine Initializes a class, all of its parent classes are required to be initialized, but this rule does not apply to the interface.

      • Initialization of the class
        • During the initialization phase, the Java virtual machine executes the initialization statement of the class, assigning the initial value to the static variables of the class. Declarations of static variables, as well as static blocks of code, are treated as initialization statements for classes, and Java virtual opportunities execute them sequentially in the order in which the initialization statements are in the class file.

        • In a program, there are two ways to initialize a static variable:

        • Initialize at the declaration of the static variable;

        • Initialize in a static block of code.

      • Initialization steps for a class
        • If the class has not been loaded and connected, load and connect it first.

        • If the class has a direct parent class, and the parent class is not initialized, the immediate parent class is initialized first.

        • If there are initialization statements in the class, then these initialization statements are executed sequentially.

      • The initialization time of the class
        • Each class or interface is initialized when it is first actively used by a Java program.

        • To create an instance of a class

        • Access a static variable for a class or interface, or assign a value to the static variable.

        • Invokes the static method of the class.

        • Reflection.

        • Initializes a subclass of a class.

        • A class that is marked as the startup class when the Java virtual machine is started.

      • The particularity of the interface
        • When a class is initialized, the interface it implements is not initialized first.

        • When an interface is initialized, its parent interface is not initialized first.

      • Static variables of final type
        • Whether a static variable of the final type is a compile constant or variable affects the execution of the initialization statement block.

        • If the value of a static variable is a compile-time constant, the type is not initialized (the static block of the class does not execute);

        • If the value of a static variable is a non-compile constant, that is, only the runtime has a deterministic initialization value, the type is initialized (the static block of the class is executed).

      • Attribution clarity of active use
        • Active use of a class or interface can be considered only if the static variable or static method accessed by the program is indeed defined in the current class or in the current interface.

  • Class Loader
    • Three types of system-supplied class loaders
      • Launch class loader (Bootstrap ClassLoader)
        • The class library (by file name) that is recognized by the virtual machine is loaded into memory, either under Java_home\lib or under the-xbootclasspath specified path. cannot be used directly by Java programs.

      • Extension class loader (Extension ClassLoader)
        • The class library is loaded into memory under Java_home\lib\ext or under the specified path of the java.ext.dirs system variable. Developers can use it directly.

      • Application class loader (application ClassLoader)
        • Responsible for loading the class library specified under User classpath. Developers can use it directly.

    • Parental delegation Model
      • In this model, the ClassLoader implements hierarchical relationships in a combined pattern, except for the top-level ClassLoader, where the class loader should have a parent classloader. When class loading occurs, precedence is loaded by the parent class loader.

    • Threading Context Loader (thread contextual ClassLoader)
      • The child ClassLoader is loaded by the parent class loader assignment. Applies to load actions that involve the SPI (Service Provider Interface), such as Jndi, JDBC, JCE, JAXB, Jbi, and so on.

    • Osgi
      • Please refer to the specific implementation framework.

This article is from the Java Technology Stack notes blog, so be sure to keep this source http://stroll.blog.51cto.com/11038467/1852661

JVM (iii)-Class structure and class loader

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.