如何防止使用 google guava 库中的 beta 类?
几个月来,我们一直在制作中使用 Google 集合。我们想开始使用番石榴来实现附加功能。然而,我担心将番石榴带入我们的产品堆栈中,因为一些开发人员可能会开始使用“测试版”类。
我们的代码中有各种单元测试,但目前,我不想包含“beta”类 b/c,它将来可能会发生变化。
有没有简单的方法来检测项目是否包含任何“beta”番石榴类?
We have been using Google collections in the production for several months. We would like to start using guava for additional functions. However, I'm afraid to bring guava into our product stack b/c some developers may start to use 'beta' classes.
We have various unit-tests in our code but at this point, I prefer not to include 'beta' class b/c it is subject to change in the future.
Is there any easy way to do detect if the project includes any 'beta' guava classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Overstock.com 最近发布了一个 Findbugs 插件,该插件标记了@Beta 类、方法或字段。
Overstock.com recently released a Findbugs plugin that flags usage of @Beta classes, methods, or fields.
在单元测试中,设置一个方面,以便在使用任何 beta 类(或任何不受欢迎的类)时记录和/或失败。
In your unit tests, setup an aspect to log and/or fail when any of the beta classes (or any unwelcome class) is used.
显然 Google Guava 有一个
@ Beta
注释,指示您不想使用哪些类或方法。不幸的是,这个注释是
@Retention(value=CLASS)
我从未使用过,但由于它应该保存在.class
文件中,这可能意味着它仍然会是可用于Class.getDeclaredAnnotations()
。如果不是,您将必须使用 CGLIB 或类似的字节码级别库来找到它。鉴于您可能想要检测 CI 应用程序或向应用程序添加检查类加载器以检测 beta API 的使用情况
Apparently Google Guava has an
@Beta
annotation which indicates which classes or methods you don't want to use.Unfortunalty this annotation is
@Retention(value=CLASS)
which I've never used but since it's supposed to be kept in.class
files it might mean that it will still be availiable toClass.getDeclaredAnnotations()
. If it's not you will have to use CGLIB or similar bytecode level library to find it.Given that you might want to instrument your CI application or add a checking classloader to your app to detect usage of beta API
如果您使用的是 eclipse,访问规则 是一种选择。每当您导入或以其他方式使用受限类时,您都会收到编译时错误。
If you're using eclipse, access rules are one option. You'd get a compile-time error whenever you are importing or otherwise using a restricted class.
以下是 Guava 的 Beta 类。您必须告诉其他开发人员在使用 guava 类之前检查此链接。
Here is a list of Guava's Beta Classes.You will have to tell other developers to check this link before using a guava class.
我想如果你有一个 beta 类列表,你可能可以使用反射来实现这一点,你可以使用 Gili 的链接。然后它就变得非常简单 - 只需查看这个答案:
可以您使用反射找到包中的所有类?
我可能只是将其放入单元测试中,如果单元测试看到您不喜欢的类,则单元测试会失败。
I was thinking you could probably use reflection for that if you had a list of beta classes, which you can using Gili's link. Then it gets pretty easy - just see this answer:
Can you find all classes in a package using reflection?
I'd probably just put that in a unit test and have the unit test fail if it sees a class you don't like.