JVM Knowledge

The representation of annotations in class files is described in §4.7.16-§4.7.22. These sections make it clear how to represent annotations on declarations of classes, interfaces, fields, methods, method parameters, and type parameters, as well as annotations on types used in those declarations. Annotations on package declarations require additional rules, given here.

When the compiler encounters an annotated package declaration that must be made available at run time, it emits a class file with the following properties:

  • The class file represents an interface, that is, the ACC_INTERFACE and ACC_ABSTRACT flags of the ClassFile structure are set (§4.1).
  • If the class file version number is less than 50.0, then the ACC_SYNTHETIC flag is unset; if the class file version number is 50.0 or above, then the ACC_SYNTHETIC flag is set.
  • The interface has package access (JLS §6.6.1).
  • The interface's name is the internal form (§4.2.1) of package-name.package-info.
  • The interface has no superinterfaces.
  • The interface's only members are those implied by The Java Language Specification, Java SE 16 Edition (JLS §9.2).
  • The annotations on the package declaration are stored as RuntimeVisibleAnnotations and RuntimeInvisibleAnnotations attributes in the attributes table of the ClassFile structure.