I'm working on a modular plugin system and I'm aiming for a setup where certain classes/APIs are exposed safely. For example, let's say we have a module **A** with both **A.public** and **A.private** classes/APIs. My goal is to allow a module **B** to access **A.public** without any way to touch **A.private**. The challenge arises when a method in **A.public** needs access to an **A.private** class. If I permit loading **A.private**, then it could potentially breach the encapsulation and allow module B access to restricted parts. Is there a method to accomplish this safely, or is it an impossible task? Also, for context, I'm aware of the Java Platform Module System (JPMS) but I require runtime protection and programmatic control rather than just relying on module config files.
2 Answers
It sounds like you're looking for a specific mechanism in Java. From what I gather, module layers might be what you need. They provide a way to create a hierarchy of modules and control how they interact with each other, which could allow module B to access **A.public** while still restricting access to **A.private**. Check out this [plugin demo](https://github.com/bowbahdoe/plugindemo) as a potential starting point! Let me know if it aligns with what you're trying to achieve.
I think you're getting a bit mixed up with the terms. **A.private** can't be a top-level class since private access doesn't apply that way. Instead, you might want to look into package-private access. With the module system, you can ensure that public classes that shouldn't be accessed outside the module are simply not exported. This way, only what you want is visible to module B.
Right, I see what you're saying about package-private. I want to ensure this level of control is enforced programmatically and not just at compile time. Could Jigsaw modules help with that?