Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Oct 6, 2025

Problem

ClassRealm does not override getPackage() or getPackages(), so packages imported from other ClassLoaders via importFrom() or importFromParent() 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() and getPackages() methods only return packages defined directly in the current ClassLoader, ignoring imported packages.

Solution

This PR overrides the protected getPackage(String) and getPackages() methods in ClassRealm to:

  1. 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.

  2. 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

ClassWorld world = new ClassWorld();
ClassRealm realmA = world.newRealm("realmA");
ClassRealm realmB = world.newRealm("realmB");

// Add log4j to realmA
realmA.addURL(new File("log4j-api-2.23.1.jar").toURI().toURL());

// Import the package from realmA to realmB
realmB.importFrom("realmA", "org.apache.logging.log4j");

// Load a class from the imported package
Class<?> loggerClass = realmB.loadClass("org.apache.logging.log4j.Logger");

// NOW WORKS: Package is visible through Class.getPackage()
Package pkg = loggerClass.getPackage();
assert pkg != null;
assert "org.apache.logging.log4j".equals(pkg.getName());

Testing

  • Added comprehensive test suite (PackageVisibilityTest) covering:
    • Packages imported via importFrom()
    • Packages imported via importFromParent()
    • Multiple packages from different realms
    • Package enumeration via getPackages()
  • All 96 existing tests continue to pass
  • Code formatting verified with spotless

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

This section details on the original issue you should resolve

<issue_title>Packages imported from other ClassLoaders are not visible</issue_title>
<issue_description>ClassRealm does not override getPackage() or getPackages(), so Package#getPackage() and Package#getPackages() do not return packages that are imported from other ClassLoaders.

I discovered this because it breaks Apache JEXL's import pragma. As you can see here, it tries to verify that the package exists. That fails, so the import is ignored.</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #126

💡 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.

Co-authored-by: slachiewicz <6705942+slachiewicz@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Fix package visibility issue with ClassLoader imports Fix: Make packages imported from other ClassLoaders visible Oct 6, 2025
@Copilot Copilot AI requested a review from slachiewicz October 6, 2025 22:01
Copilot finished work on behalf of slachiewicz October 6, 2025 22:01
@slachiewicz slachiewicz marked this pull request as ready for review October 6, 2025 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Packages imported from other ClassLoaders are not visible
2 participants