简体   繁体   English

控制对“.internal”包的访问的最佳实践

[英]Best practice for controlling access to a “.internal” package

I write Eclipse plugins and export some classes as an API while wanting to restrict access to other classes. 我编写Eclipse插件并将一些类导出为API,同时希望限制对其他类的访问。

I follow the common Eclipse practice of separating these classes into a ".internal" subpackage. 我按照常见的Eclipse实践将这些类分成“.internal”子包。

However, then I can't use "package" or default level access on these classes since many of them need to be used by the classes I'm exporting. 但是,我不能在这些类上使用“包”或默认级别访问,因为我们导出的类需要使用它们中的许多。

What is the best practice for preventing or discouraging users of my API from using these classes for their own purposes? 防止或阻止我的API用户出于自己的目的使用这些类的最佳做法是什么? Is there an automatic checker? 有自动检查器吗?

I admit that I've dabbled in using some of Eclipse's internal classes when I had no choice :) 我承认,当我别无选择时,我已经涉足了使用Eclipse的一些内部类:)

Clarification: I have a similar need with non-plugin code. 澄清:我对非插件代码有类似的需求。

Isn't it just a case of updating the META-INF/MANIFEST.MF to being a plug-in osgi project (if it's not already?). 是不是只是将META-INF / MANIFEST.MF更新为插件osgi项目(如果它还没有?)。 It should look something 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"

And then nicely omit the .internal package. 然后很好地省略.internal包。 The platform should do the rest. 平台应该完成其余的工作。

By the way, you then use the Import-Package: in any dependent bundles, plugins etc, rather than depending on the jar/project (which is the old, sucky way which doesn't work - as you're finding). 顺便说一句,然后你可以在任何依赖包,插件等中使用Import-Package:而不是依赖于jar /项目(这是一种旧的,糟糕的方式,它不起作用 - 正如你所发现的那样)。

This gives you massive de-coupling of your code dependencies. 这使您可以大量解耦代码依赖项。 If you decide that your plugin code should belong in a different jar/bundle, you just move individual packages, and make the new bundle/plug-in export it. 如果您确定您的插件代码应属于不同的jar / bundle,则只需移动单个包,并使新的bundle / plug-in导出它。 Since the client bundles just import the package from "the cloud" (cloud being the OSGi platform), you can move code a lot more freely. 由于客户端捆绑包只是从“云”(云是OSGi平台)导入包,因此您可以更自由地移动代码。

Note : As mentioned in the comments, you don't need to be running your apps in OSGi to get this "benefit". 注意 :如评论中所述,您无需在OSGi中运行应用程序即可获得此“好处”。 Eclipse can compile it's code under OSGi package restrictions, and your build/server can run in "the unprotected world". Eclipse可以在OSGi包限制下编译它的代码,并且您的构建/服务器可以在“未受保护的世界”中运行。 eg the OSGi manifests don't enforce anything to 3rd parties (who wish to use .internal), but provide "notifications" and restrictions to those that want them. 例如,OSGi清单不对第三方(希望使用.internal)强制执行任何操作,但为需要它们的人提供“通知”和限制。

For plugin projects : Use the Manifest setup that Stephen mentions. 对于插件项目 :使用Stephen提到的Manifest设置。 (You can also edit this through the "Runtime" page of the Manifest editor. Note that if you would like to only expose a package to specific plugins you can do that as well using (您也可以通过Manifest编辑器的“运行时”页面进行编辑。请注意,如果您只想将包暴露给特定的插件,您也可以使用它

Export-Package: com.javadude.foo;x-friends:="com.javadude.fee"

For non-plugin projects : Use separate source folders to define what is exported from your project. 对于非插件项目 :使用单独的源文件夹来定义从项目中导出的内容。

  1. Right-click project and choose New Source Folder (perhaps name it "internal-source") 右键单击项目并选择“新建源文件夹”(可能将其命名为“internal-source”)
  2. Right-click project and choose Properties 右键单击项目,然后选择“属性”
  3. Go to Java Build Path 转到Java Build Path
  4. Click the Order and Export tab 单击“订购和导出”选项卡
  5. Uncheck the source folders you do not want to export 取消选中您不想导出的源文件夹

Only the source folders that are checked under "Order and Export" will be added to the classpath of referencing projects. 只有在“订购和导出”下检查的源文件夹才会添加到引用项目的类路径中。

Unless you have to handle somebody else's untrusted code using your trusted library, please don't restrict it. 除非您必须使用受信任的库处理其他人的不受信任的代码,否则请不要限制它。

Needing internal/package access sometimes indicates a failure of your API design, and you can't know until long afterwords. 需要内部/程序包访问有时会表明您的API设计失败,直到很久以后才能知道。

Make it public, name it InternalWhatever, and live with it. 公开它,将其命名为InternalWhatever,并与之共存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM