[英]Stay compatible with (changed) doclet API on jdk7 and jdk8
I'm developing a maven plugin that uses the Doclet API ( ConfigurationImpl.getInstance()
and HtmlDocletWriter.commentTagsToString()
for example). 我正在开发一个使用Doclet API的Maven插件(例如,
ConfigurationImpl.getInstance()
和HtmlDocletWriter.commentTagsToString()
)。 This API is contained in tools.jar that comes with the JDK. 该API包含在JDK随附的tools.jar中。
On jdk8, these methods don't exist anymore so the plugin breaks. 在jdk8上,这些方法不再存在,因此插件中断。
I'm now looking for a way to make my plugin work with both jdk7 and 8. 我现在正在寻找一种使我的插件与jdk7和8一起使用的方法。
I'm thinking about using an interface that offers a stable API and providing different implementations for each JDK. 我正在考虑使用提供稳定API的接口,并为每个JDK提供不同的实现。 However, I'm not sure how I can switch/load the correct implementation.
但是,我不确定如何切换/加载正确的实现。 Mind that everything needs to be packaged in the same jar (the maven plugin).
注意,所有内容都必须打包在同一个jar中(Maven插件)。
Any suggestions? 有什么建议么?
Create a factory that will give you an instance of your interface. 创建一个工厂,为您提供界面实例。 Within the factory you check the
java.version
system property. 在工厂中,您可以检查
java.version
系统属性。 If it is >= 8
then you will instantiate the interface implementation for JDK 8, otherwise the one for JDK 7. Those implementation classes must only be loaded in that factory. 如果
>= 8
,则将实例化JDK 8的接口实现,否则将实例化JDK 7的接口实现。那些实现类必须仅在该工厂中加载。 So you won't get a problem with missing APIs, because the class won't be loaded that depends on that API. 因此,您不会因缺少API而遇到问题,因为不会加载依赖于该API的类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.