Java – best practices for controlling access to “. Internal” packages
I write eclipse plug-ins and export some classes as APIs, while I want to restrict access to other classes
I follow eclipse's common habit of dividing these classes into ". Internal" subpackages
However, I cannot use "package" or default level access on these classes because many of them need to be used by exported classes
What are the best practices to prevent or prevent users of my API from using these classes for their own purposes? Is there an automatic checker?
I admit that when I had no choice, I had already used some of eclipse's inner classes
Clarification: I have a similar need for non - plug - in code
Solution
Not just meta inf / manifest MF is updated to a plug-in OSGi project (if not yet?) It should look like:
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: My-plugin Bundle-SymbolicName: com.mycompany.mypluginname Bundle-Version: 1.0.0 Bundle-Vendor: MyCompany Bundle-requiredExecutionEnvironment: JavaSE-1.6 Service-Component: Import-Package: org.apache.log4j;version="1.2.14" (,separated etc) Export-Package: com.mycompany.mypluginname.myapipackage;version="1.0.0"
Then it is well omitted Internal package The platform should rest
By the way, then use import package: in any dependent bundle, plugins, etc., rather than relying on jar / Project (this is the old, sucking way, it doesn't work - as you found)
This can greatly reduce your code dependencies If you decide that your plug-in code should belong to different jar / bundle, you just need to move the separate package and make the new bundle / plug-in export it Because the client bundle imports packages from "cloud" (cloud as OSGi platform), you can move code more freely
Note: as stated in the comments, you do not need to run the application in OSGi to get this "benefit" Eclipse can compile its code under OSGi package restrictions, and your build / server can run in the "unprotected world" For example, OSGi lists do not perform any operations on third parties (who want to use them internally), but provide "notifications" and restrictions to those who want them