@PackageInfo types are exposed as part of named interfaces
Description
JavaPackage.getExposedClasses() filters package-info by simple name only. A type that is meta-annotated with @PackageInfo stays in the result. Thus NamedInterface.of(JavaPackage) and NamedInterface.unnamed(…) include the metadata type in the named interface.
Kotlin users are affected most. Kotlin has no package-info.java, so a @PackageInfo type is the only way to declare a package-level named interface or the module metadata.
Example
The example comes from the reference documentation:
package example.order.spi
@PackageInfo
@NamedInterface("spi")
class ModuleMetadata
ApplicationModules.of(Application.class)
.getModuleByName("order").orElseThrow()
.getNamedInterfaces().getByName("spi").orElseThrow()
.asJavaClasses()
.forEach(System.out::println);
Actual: the spi named interface contains example.order.spi.ModuleMetadata.
Expected: it does not. The type stands for the package, as package-info.java does.
The unnamed interface has the same problem. A public base-package @ApplicationModule class ModuleMetadata is part of the unnamed interface.
Impact
- A metadata type is part of the module API. Another module can depend on it, and the verification accepts the dependency.
- The documentation output and the tools that read the module model show the metadata type as an API type.
Proposed fix
JavaPackage already defines the correct predicate, ARE_PACKAGE_INFOS, and exposes it as isPackageInfoType(JavaClass). NamedInterfaces.ofAnnotatedTypes already uses it. getExposedClasses() can use the same predicate:
return classes
.that(DescribedPredicate.describe("are not package info types", it -> !isPackageInfoType(it)))
.that(have(modifier(JavaModifier.PUBLIC)));
While I am not fundamentally opposed, I wonder what causes the assumption that these types can never be part of a named interface? @PackageInfo merely declares the type to be considered for annotation inspection otherwise only performed on package-info.java.
Although it's not wrong to assume folks would use the annotation on a type dedicated to only application-module-level annotations, it could be used on arbitrary types within the module that indeed have other responsibilities. Why would we prevent / break that use case?
I guess what plays into this is that @PackageInfo is primarily targeted to a Kotlin audience, and with that, all types are public by default, which in turn has been discussed as problematic for ages (or at least the lack of a package scope in Kotlin). That said, we can't assume Kotlin-only usage of the annotation and break valid use cases.
Thanks for the quick feedback. I agree that @PackageInfo on a type with other responsibilities is valid today, so a silent change of the default is not an option.
My assumption came from the original intent of the annotation. #522 describes it as a way to "promote a type in a package to be the logical equivalent to package-info.java". A package-info.java type is never part of a module's API. The reference documentation also shows only dedicated types, such as ModuleMetadata, that carry @ApplicationModule, or @PackageInfo with @NamedInterface.
For such a dedicated type, the exposure is a side effect. Another module can depend on ModuleMetadata, and the verification accepts it. With Kotlin, internal does not help, because the type is public in bytecode.
What do you think about making this behavior opt-in? For example:
- a property such as
spring.modulith.package-info-types.exposed=false, which removes package info types from the named interfaces and makes a reference from another module a violation, or - a warning first, when another module references a package info type, with stricter behavior in a later major version.
Both keep the dual-use case working by default. Users who treat these types as pure metadata can still keep them out of the module API.