Fix: Make packages imported from other ClassLoaders visible #133
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.
Problem
ClassRealm does not override
getPackage()
orgetPackages()
, so packages imported from other ClassLoaders viaimportFrom()
orimportFromParent()
are not visible through the standard ClassLoader package lookup methods. This breaks Apache JEXL's import pragma functionality, as documented in the JEXL source code, which verifies package existence before allowing imports.Root Cause
When a ClassRealm imports packages from another ClassLoader, those packages are defined in the source ClassLoader, not in the importing realm. The default
getPackage()
andgetPackages()
methods only return packages defined directly in the current ClassLoader, ignoring imported packages.Solution
This PR overrides the protected
getPackage(String)
andgetPackages()
methods in ClassRealm to:Check imported packages - When looking up a package by name, the implementation now checks all foreign imports and parent imports to see if the package matches an import pattern, and if so, delegates to the imported ClassLoader to retrieve the package.
Include imported packages in listings - When enumerating all packages, the implementation now collects packages from all imported ClassLoaders (filtered by import patterns) in addition to locally defined packages.
The implementation uses reflection to support both Java 8 (deprecated
getPackage()
/getPackages()
) and Java 9+ (getDefinedPackage()
/getDefinedPackages()
) APIs, ensuring compatibility across Java versions.Example
Testing
PackageVisibilityTest
) covering:importFrom()
importFromParent()
getPackages()
Impact
This fix enables Apache JEXL and other libraries that rely on package visibility to work correctly with ClassRealm's package import mechanism. The change is backward compatible and does not affect existing functionality.
Fixes issue where JEXL import pragma fails with ClassRealm-imported packages.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.