The following steps are used to derive a nonarray class or interface C denoted by N from a purported representation in class file format using the class loader L.
-
First, the Java Virtual Machine determines whether
Lhas already been recorded as an initiating loader of a class or interface denoted byN. If so, this derivation attempt is invalid and derivation throws aLinkageError. -
Otherwise, the Java Virtual Machine attempts to parse the purported representation. The purported representation may not in fact be a valid representation of C, so derivation must detect the following problems:
-
If the purported representation is not a
ClassFilestructure (§4.1, §4.8), derivation throws aClassFormatError. -
Otherwise, if the purported representation is not of a supported major or minor version (§4.1), derivation throws an
UnsupportedClassVersionError.UnsupportedClassVersionError, a subclass ofClassFormatError, was introduced in JDK 1.2 to enable easy identification of aClassFormatErrorcaused by an attempt to load a class whose representation uses an unsupported version of theclassfile format. In JDK 1.1 and earlier, an instance ofNoClassDefFoundErrororClassFormatErrorwas thrown in case of an unsupported version, depending on whether the class was being loaded by the system class loader or a user-defined class loader. -
Otherwise, if the purported representation does not actually represent a class or interface named
N, derivation throws aNoClassDefFoundError.This occurs when the purported representation has either a
this_classitem which specifies a name other thanN, or anaccess_flagsitem which has theACC_MODULEflag set.
-
-
If C has a direct superclass, the symbolic reference from C to its direct superclass is resolved using the algorithm of §5.4.3.1. Note that if C is an interface it must have
Objectas its direct superclass, which must already have been loaded. OnlyObjecthas no direct superclass.Any exception that can be thrown as a result of failure of class or interface resolution can be thrown as a result of derivation. In addition, derivation must detect the following problems:
-
If any of the superclasses of C is C itself, derivation throws a
ClassCircularityError. -
Otherwise, if the class or interface named as the direct superclass of C is in fact an interface or a
finalclass, derivation throws anIncompatibleClassChangeError. -
Otherwise, if the class named as the direct superclass of C has a
PermittedSubclassesattribute (§4.7.31) and any of the following is true, derivation throws anIncompatibleClassChangeError:- The superclass is in a different run-time module than C (§5.3.6).
- C does not have its
ACC_PUBLICflag set (§4.1) and the superclass is in a different run-time package than C (§5.3). - No entry in the
classesarray of the superclass'sPermittedSubclassesattribute refers to a class or interface with the nameN.
-
Otherwise, if C is a class and some instance method declared in C can override (§5.4.5) a
finalinstance method declared in a superclass of C, derivation throws anIncompatibleClassChangeError.
-
-
If C has any direct superinterfaces, the symbolic references from C to its direct superinterfaces are resolved using the algorithm of §5.4.3.1.
Any exception that can be thrown as a result of failure of class or interface resolution can be thrown as a result of derivation. In addition, derivation must detect the following problems:
-
If any of the superinterfaces of C is C itself, derivation throws a
ClassCircularityError. -
Otherwise, if any class or interface named as a direct superinterface of C is not in fact an interface, derivation throws an
IncompatibleClassChangeError. -
Otherwise, for each direct superinterface named by C, if the superinterface has a
PermittedSubclassesattribute (§4.7.31) and any of the following is true, derivation throws anIncompatibleClassChangeError:- The superinterface is in a different run-time module than C.
- C does not have its
ACC_PUBLICflag set (§4.1) and the superinterface is in a different run-time package than C. - No entry in the
classesarray of the superinterface'sPermittedSubclassesattribute refers to a class or interface with the nameN.
-
If no exception is thrown in steps 1-4, then derivation of the class or interface C succeeds. The Java Virtual Machine marks C to have L as its defining loader, records that L is an initiating loader of C (§5.3.4), and creates C in the method area (§2.5.4).
When derivation succeeds, the process of loading and creating C is not complete until every class loader that was involved in loading C (directly or indirectly) returns C as its result. Depending on the behavior of user-defined class loaders, the process of loading and creating C may yet fail (§5.3.2).
If an exception is thrown in steps 1-4, then derivation of the class or interface C fails with that exception.