Skip to content

Resolve packages through realm imports and parent - #159

Closed
slachiewicz wants to merge 2 commits into
masterfrom
agent/realm-imported-packages
Closed

slachiewicz wants to merge 2 commits into
masterfrom
agent/realm-imported-packages

Conversation

@slachiewicz

@slachiewicz slachiewicz commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Fixes #126. Package.getPackage(name) resolves through the caller's class loader, and ClassRealm inherited ClassLoader.getPackage, which never consults imports or the parent realm, so JEXL's import pragma dropped imported packages.

getPackage and getPackages now apply the same routing as loadClass: own class path and base loader, then any import that would load a class in the package, whether a package import or a single-class import, then the parent where parent imports allow it. A package is visible exactly where a class in it would load, so a FilteredClassRealm exposes no package its filter hides. Import or parent cycles return null instead of recursing.

Verified: ClassRealmPackageTest fails on master (expected: <package a> but was: <null>) and passes on JDK 8, 21 and 27.

getPackage and getPackages now follow the same import and parent-import
rules as loadClass, so Package.getPackage sees imported packages. Do not
widen this past those rules: a package must never be visible where a
class in it is not.
@slachiewicz slachiewicz added the bug Something isn't working label Oct 6, 2026
@slachiewicz
slachiewicz marked this pull request as ready for review October 6, 2026 20:44
@slachiewicz
slachiewicz requested a balanced review from Copilot October 6, 2026 20:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Synthetic package class names produce incorrect results for exact class imports.

Review effort: Balanced
Findings: 2 Medium severity · 1 Low severity

Open (3)
What changed in this PR

Adds package lookup across realm imports and parents while preventing cyclic recursion.

Changes:

  • Overrides package lookup and enumeration in ClassRealm.
  • Adds import, parent, filter, and cycle tests.
  • Adds a realm-loaded package probe.
File Description
ClassRealm.java Implements realm-aware package resolution.
ClassRealmPackageTest.java Tests package visibility scenarios.
PackageProbe.java Invokes caller-sensitive Package APIs.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/main/java/org/codehaus/plexus/classworlds/realm/ClassRealm.java Outdated
Comment thread src/main/java/org/codehaus/plexus/classworlds/realm/ClassRealm.java

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Exact imports of unnamed-package classes are not included in package lookup.

Review effort: Balanced
Findings: None

Resolved since last review (3)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Handle unnamed-package imports in package lookup

src/​main/​java/​org/​codehaus/​plexus/​classworlds/​realm/​ClassRealm.java:538

Exact imports of classes in the unnamed package are omitted here. For an import such as "A", lastIndexOf('.') is -1, so loadClass("A") routes to the imported loader but both getPackage("") and getPackages() ignore that loader even though a loaded default-package class has a Package named "". Treat a non-empty, dotless import as a candidate class name when packageName is empty.

@slachiewicz slachiewicz added the wontfix This will not be worked on label Oct 7, 2026
@slachiewicz slachiewicz closed this Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working wontfix This will not be worked on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Packages imported from other ClassLoaders are not visible

2 participants