On Sat, 11 Mar 2023 17:00:18 GMT, Alan Bateman <al...@openjdk.org> wrote:

>> `buildModules` is expected to be called with additional packages but instead 
>> it's called with all packages including all exported and open packages.   
>> 
>> 
>>     /**
>>      * Build a module descriptor into a byte array.
>>      * @param moduleAttribute the {@code Module} attribute
>>      * @param packages additional module packages
>>      * @param handler a handler that receives a {@link ClassBuilder}
>>      * @return the classfile bytes
>>      */
>>     public static byte[] buildModule(ModuleAttribute moduleAttribute,
>>                                      List<PackageDesc> packages,
>>                                      Consumer<? super ClassBuilder> handler) 
>> {
>> 
>> 
>> I checked the implementation that seems to match `@param packages` that 
>> expects additional module packages that are not exported nor open.  If it 
>> intends to take additional packages, it will need to filter the exported and 
>> open packages at the callsite.
>> 
>> Or the `packages` parameter lists all packages that will be used to create 
>> `ModulePackages` attribute.  This seems to be easier to understand.
>
> Maybe the variants of Classfile.buildModule need to be looked at again. For 
> the usage here, buildModule(ModuleAttribute, Consumer<? super ClassBuilder>) 
> would be more useful as it would allow all of the additional attributes to be 
> emitted in the handler rather than having buildModule making the decision on 
> whether to emit the ModulePackages attribute.

`Classfile::buildModule` is a helper method simplifying the major use case - 
building valid module with minimal user effort and knowledge. It emits 
`ModulePackages` if there are any packages specified and not exported nor 
opened. That is the configuration which JVM expects and majority of users are 
not aware of.
Other module-specific attributes can be added in the underlying handler, using 
`ClassBuilder::with`.

A different building approach can be used in cases (which I would like to know 
more about) where even `ModulePackages` attribute needs to be "customised" or 
provided unconditionally. It is always possible to build module as a class 
using `Classfile::build` from scratch and provide all the attributes manually.

-------------

PR: https://git.openjdk.org/jdk/pull/11368

Reply via email to