JVM Knowledge

The Signature attribute is a fixed-length attribute in the attributes table of a ClassFile, field_info, or method_info structure (§4.1, §4.5, §4.6). A Signature attribute records a signature (§4.7.9.1) for a class, interface, constructor, method, or field whose declaration in the Java programming language uses type variables or parameterized types. See The Java Language Specification, Java SE 13 Edition for details about these constructs.

There may be at most one Signature attribute in the attributes table of a ClassFile, field_info, or method_info structure.

The Signature attribute has the following format:

Signature_attribute {
    u2 attribute_name_index;
    u4 attribute_length;
    u2 signature_index;
}

The items of the Signature_attribute structure are as follows:

  • attribute_name_index

    The value of the attribute_name_index item must be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Utf8_info structure (§4.4.7) representing the string "Signature".

  • attribute_length

    The value of the attribute_length item must be two.

  • signature_index

    The value of the signature_index item must be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Utf8_info structure (§4.4.7) representing a class signature if this Signature attribute is an attribute of a ClassFile structure; a method signature if this Signature attribute is an attribute of a method_info structure; or a field signature otherwise.

Oracle's Java Virtual Machine implementation does not check the well-formedness of Signature attributes during class loading or linking. Instead, Signature attributes are checked by methods of the Java SE Platform class libraries which expose generic signatures of classes, interfaces, constructors, methods, and fields. Examples include getGenericSuperclass in Class and toGenericString in java.lang.reflect.Executable.

4.7.9.1. Signatures

Signatures encode declarations written in the Java programming language that use types outside the type system of the Java Virtual Machine. They support reflection and debugging, as well as compilation when only class files are available.

A Java compiler must emit a signature for any class, interface, constructor, method, or field whose declaration uses type variables or parameterized types. Specifically, a Java compiler must emit:

  • A class signature for any class or interface declaration which is either generic, or has a parameterized type as a superclass or superinterface, or both.

  • A method signature for any method or constructor declaration which is either generic, or has a type variable or parameterized type as the return type or a formal parameter type, or has a type variable in a throws clause, or any combination thereof.

    If the throws clause of a method or constructor declaration does not involve type variables, then a compiler may treat the declaration as having no throws clause for the purpose of emitting a method signature.

  • A field signature for any field, formal parameter, or local variable declaration whose type uses a type variable or a parameterized type.

Signatures are specified using a grammar which follows the notation of §4.3.1. In addition to that notation:

  • The syntax [x] on the right-hand side of a production denotes zero or one occurrences of x. That is, x is an optional symbol. The alternative which contains the optional symbol actually defines two alternatives: one that omits the optional symbol and one that includes it.
  • A very long right-hand side may be continued on a second line by clearly indenting the second line.

The grammar includes the terminal symbol Identifier to denote the name of a type, field, method, formal parameter, local variable, or type variable, as generated by a Java compiler. Such a name must not contain any of the ASCII characters . ; [ / < > : (that is, the characters forbidden in method names (§4.2.2) and also colon) but may contain characters that must not appear in an identifier in the Java programming language (JLS §3.8).

Signatures rely on a hierarchy of nonterminals known as type signatures:

  • A Java type signature represents either a reference type or a primitive type of the Java programming language.

    JavaTypeSignature:
      ReferenceTypeSignature
      BaseType
    

    The following production from §4.3.2 is repeated here for convenience:

    BaseType:
      (one of)
      B C D F I J S Z
    
  • A reference type signature represents a reference type of the Java programming language, that is, a class or interface type, a type variable, or an array type.

    A class type signature represents a (possibly parameterized) class or interface type. A class type signature must be formulated such that it can be reliably mapped to the binary name of the class it denotes by erasing any type arguments and converting each . character to a $ character.

    A type variable signature represents a type variable.

    An array type signature represents one dimension of an array type.

    ReferenceTypeSignature:
      ClassTypeSignature
      TypeVariableSignature
      ArrayTypeSignature
    
    ClassTypeSignature:
      L [PackageSpecifier] SimpleClassTypeSignature {ClassTypeSignatureSuffix} ;
    
    PackageSpecifier:
      Identifier / {PackageSpecifier}
    
    SimpleClassTypeSignature:
      Identifier [TypeArguments]
    
    TypeArguments:
      < TypeArgument {TypeArgument} >
    
    TypeArgument:
      [WildcardIndicator] ReferenceTypeSignature
      *
    
    WildcardIndicator:
      +
      -
    
    ClassTypeSignatureSuffix:
      . SimpleClassTypeSignature
    
    TypeVariableSignature:
      T Identifier ;
    
    ArrayTypeSignature:
      [ JavaTypeSignature
    

A class signature encodes type information about a (possibly generic) class declaration. It describes any type parameters of the class, and lists its (possibly parameterized) direct superclass and direct superinterfaces, if any. A type parameter is described by its name, followed by any class bound and interface bounds.

ClassSignature:
  [TypeParameters] SuperclassSignature {SuperinterfaceSignature}

TypeParameters:
  < TypeParameter {TypeParameter} >

TypeParameter:
  Identifier ClassBound {InterfaceBound}

ClassBound:
  : [ReferenceTypeSignature]

InterfaceBound:
  : ReferenceTypeSignature

SuperclassSignature:
  ClassTypeSignature

SuperinterfaceSignature:
  ClassTypeSignature

A method signature encodes type information about a (possibly generic) method declaration. It describes any type parameters of the method; the (possibly parameterized) types of any formal parameters; the (possibly parameterized) return type, if any; and the types of any exceptions declared in the method's throws clause.

MethodSignature:
  [TypeParameters] ( {JavaTypeSignature} ) Result {ThrowsSignature}

Result:
  JavaTypeSignature
  VoidDescriptor

ThrowsSignature:
  ^ ClassTypeSignature
  ^ TypeVariableSignature

The following production from §4.3.3 is repeated here for convenience:

VoidDescriptor:
  V

A method signature encoded by the Signature attribute may not correspond exactly to the method descriptor in the method_info structure (§4.3.3). In particular, there is no assurance that the number of formal parameter types in the method signature is the same as the number of parameter descriptors in the method descriptor. The numbers are the same for most methods, but certain constructors in the Java programming language have an implicitly declared parameter which a compiler represents with a parameter descriptor but may omit from the method signature. See the note in §4.7.18 for a similar situation involving parameter annotations.

A field signature encodes the (possibly parameterized) type of a field, formal parameter, or local variable declaration.

FieldSignature:
  ReferenceTypeSignature