Fix loading package-info classes#56
Merged
marchermans merged 3 commits intoMcModLauncher:mainfrom Jan 12, 2024
Merged
Conversation
Contributor
|
I can't say I understand the issue. Can you add a relevant regression test? |
4955c69 to
9a052fb
Compare
marchermans
approved these changes
Jan 12, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The issue
Currently, SJH's
ModuleClassLoaderis unable to loadpackage-infoclasses, which are placed in the unnamed module when it callsdefinePackage.Java offers 2 ways of defining packages - one with version information and another with a module parameter, which are mutually exclusive. However, our use case requires both package versioning information and the module to be present.
ModuleClassLoaderis designed to only load classes from named modules. When callingClass.getPackage().getPackageInfo(), Java will try to load the package-info class usingClassLoader#findClass(String moduleName, String name), passing innullfor the moduleName parameter as the package is placed in an unnamed module. Here, MCL expectsmoduleNameto be non-null at all times. However, that is not true according to java's documentation:MCL will happily pass this parameter to
Configuration#findModule, which expects a non-null value, resulting in a NPE.Steps to reproduce
getPackage().getPackageInfo()on your favourite class, which is loaded by aModuleClassLoaderExpected behavior
Much like other classes,
package-infoclasses should be placed in named modules, too, rather than being loaded in the unnamed module.The solution
To make java load package-info classes in their module, we must replace the value of the
NamedPackage#modulefield. Since java puts no restrictions in place that would prevent us from setting a named module on aNamedPackageinstance, we reflectively replace the default unnamed module value with the package's actual module.Step 1: Grab the trusted method lookup instance and create a field handle for the package module field
Step 2: After defining each class (necessary for the module to be known), ensure its package is placed in a named module