Skip to content
Alessandro Autiero
Go back

Proposal: Allow annotations on module directives

Context: this started as a question on r/java in July 2024: module declarations can be annotated, but their directives can’t.

@SomeAnnotation // Allowed
module com.groupName.artifactName {
   @AnotherAnnotation // Not allowed today
   exports ...;
   @AnotherAnnotation // Not allowed today
   requires ...;
}

Summary

This proposal suggests allowing annotations on module directives in a module-info, similar to how module declarations can currently be annotated. This enhancement, while narrow in scope, would give developers more flexibility when designing libraries deeply integrated with Java’s built-in module system. I have built a proof of concept on GitHub which is already fully working and completely backwards compatible. I still haven’t:

  1. Built automated integration tests
  2. Added support for the new ClassFile API

Before doing that though I wanted to collect some feedback on my implementation, especially on:

  1. Whether this change makes sense to the maintainers
  2. The new bytecode attributes I introduced and whether better alternatives exist
  3. The uses() accessor in ModuleDescriptor

All changes I made are documented in the respective commits, but if you have any questions please let me know.

Bytecode

My implementation introduces two new bytecode attributes:

  • ModuleDirectivesRuntimeInvisibleAnnotations
  • ModuleDirectivesRuntimeVisibleAnnotations

which both contain, for each type of module directive, the number of directives that were annotated and, for each directive that was annotated:

  1. The index of the directive relative to all the directives of the same type that are present in the module
  2. The number of annotations that were attached to the directive
  3. The metadata of the annotation in the standard format used in the bytecode

Before working on this implementation, I had never designed bytecode attributes, so my approach might be suboptimal; if any better alternatives exist, I’ll implement the changes as soon as possible. Other approaches I considered:

  • Using RuntimeInvisibleAnnotations and RuntimeVisibleAnnotations: module directives are stored in the Module attribute alongside the module name, flags and version, instead of in a specialized table like class fields or local variables. For this reason, they cannot declare their own attributes (nested attributes are not supported by the JVM).
  • Modifying the Module attribute to contain the annotations directly in the existing directives metadata: while this approach doesn’t require additional bytecode attributes, it completely breaks existing tooling that parses a module-info, because of the same constraints explained for the previous approach.

In conclusion, while my proposed implementation is fully backwards compatible, it introduces two new bytecode attributes.

Public API changes

All changes are backwards compatible, but I’ve included some reasoning on some design choices I made.

javax.lang.model.element.ModuleElement.Directive

  • Implement AnnotatedConstruct to support annotations during compile-time processing.
  • Directive does not currently implement Element and cannot do so in the future, as it already defines a method named getKind which clashes with Element’s getKind because of their different return types (DirectiveKind and ElementKind respectively). Implementing Element would also require ElementVisitor to implement DirectiveVisitor. I don’t think that it’s necessary for Directive to implement Element, as it wasn’t intended to do so when Java 9 was introduced, so I see no reason or backwards-compatible way to do so.

java.lang.module.ModuleDescriptor

  • Added a new sealed common class named Directive that Exports, Requires, Opens, Uses and Provides extend. This class contains an annotations() accessor to allow callers to access annotations.
  • Added a new Uses class that represents uses directives. This introduces a slight backwards-compatibility issue, as an accessor named uses() already exists in ModuleDescriptor and it returns a Set<String> instead of a Set<Uses>. For now I added an accessor named used() that returns a Set<Uses>, but usedServices could be a better name. I don’t think that changing uses() to return Set<Uses> is an option, as that would break backwards compatibility, and having an odd-named accessor shouldn’t be that much of an issue if we find a fitting name.

Follow-up: motivation

When asked for concrete motivation, I pointed to a similar JEP, JEP 120 (Repeating Annotations): its aim was to allow developers to use multiple annotations of the same type on an element, a limitation previously addressed with container annotations. In the case of this hypothetical JEP, the aim is to allow developers to annotate module directives instead of using the enclosing module declaration as the target for annotations that actually belong to single directives.

It’s hard to point at a particular issue in the ecosystem, because this is a limitation of the language itself: the projects that would use this feature can’t exist yet. Similarly, JEP 104 (Type Annotations) used the Checker Framework’s existence as a success metric, but that project could only be built after annotations could be applied to types. If I had to name a possible application comparable to the Checker Framework for JEP 104, I’d say that a build system’s dependency management could be built entirely around the module-info, instead of a separate file like in Maven or Gradle.


Originally sent to the OpenJDK compiler-dev mailing list on November 11, 2024. The prototype lives on the module_directives_annotations branch of my JDK fork.



Previous Post
Artemis - Alternative HIDIVE streaming client for iOS devices
Next Post
Sampling should support embedded resources