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:
- Built automated integration tests
- Added support for the new ClassFile API
Before doing that though I wanted to collect some feedback on my implementation, especially on:
- Whether this change makes sense to the maintainers
- The new bytecode attributes I introduced and whether better alternatives exist
- The
uses()accessor inModuleDescriptor
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:
ModuleDirectivesRuntimeInvisibleAnnotationsModuleDirectivesRuntimeVisibleAnnotations
which both contain, for each type of module directive, the number of directives that were annotated and, for each directive that was annotated:
- The index of the directive relative to all the directives of the same type that are present in the module
- The number of annotations that were attached to the directive
- 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
RuntimeInvisibleAnnotationsandRuntimeVisibleAnnotations: module directives are stored in theModuleattribute 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
Moduleattribute 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
AnnotatedConstructto support annotations during compile-time processing. Directivedoes not currently implementElementand cannot do so in the future, as it already defines a method namedgetKindwhich clashes withElement’sgetKindbecause of their different return types (DirectiveKindandElementKindrespectively). ImplementingElementwould also requireElementVisitorto implementDirectiveVisitor. I don’t think that it’s necessary forDirectiveto implementElement, 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
DirectivethatExports,Requires,Opens,UsesandProvidesextend. This class contains anannotations()accessor to allow callers to access annotations. - Added a new
Usesclass that representsusesdirectives. This introduces a slight backwards-compatibility issue, as an accessor nameduses()already exists inModuleDescriptorand it returns aSet<String>instead of aSet<Uses>. For now I added an accessor namedused()that returns aSet<Uses>, butusedServicescould be a better name. I don’t think that changinguses()to returnSet<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.