我想使用 @Deprecated 注释仅弃用给定类的扩展,而不是类中包含的所有方法和字段。
也就是说,如果您扩展给定的类,则会出现警告 - 但对方法或字段的引用不会触发警告。已经有几个类扩展了此类,并且我希望针对这些客户端发出弃用警告 - 我还无法破坏它们(但它们可以重新编译 - 不需要 ABI 兼容性)。
在Java 1.6(JDT编译器)中可能吗?
I would like to deprecate only the extension of a given class, not all the methods and fields contained within a class, using the @Deprecated annotation.
That is, a warning will occur if you extend a given class - but references to methods or fields will not trigger a warning. There are already several classes that extend this class, and I want to have the deprecation warnings target these clients - I can't break them yet (but they can be recompiled - ABI compatibility not needed).
Is it possible in Java 1.6 (JDT compiler)?
发布评论
评论(5)
两个注意事项
1) 该类可能已经扩展,因此不要将其标记为 Final,否则可能会破坏向后兼容性。
2)您不希望扩展该类,因此应将其标记为最终类。
我认为你应该做的是用新类扩展旧类,将旧类标记为已弃用,然后将新类声明为最终类。在新类中,您可以添加 @SuppressWarning 标记来消除已弃用的消息,然后您应该获得干净的编译。
使用旧类的代码将收到 @Deprecated 警告,但仍会编译。使用新类的代码将干净地编译。这是对用户的“强烈建议”,而不是向后兼容的中断,而且由于 API 100% 兼容,因此他们很容易修复。
Two considerations
1) The class may already be extended, so don't mark it final or you could break backwards compatibility.
2) You don't want the class extended, so it should be marked final.
I think what you should do is extend the old class with a new class, mark the old class deprecated, and declare the new class final. In the new class, you can add the @SuppressWarning tag to quiet the deprecated message, then you should sitll get a clean compile.
Code using the old class will get a @Deprecated Warning, but will still compile.. Code using the new class will compile cleanly. Kind of a "strong suggestion" to your users instead of a backward compatible break, and pretty easy for them to fix since the API is 100% compatible.
我怀疑使用 @Deprecated 注释是否可以做到这一点。
如果您可以控制源,则可以执行以下操作:
SomeClass
重命名为SomeClassSuper
SomeClass
的新类并让它扩展了SomeClassSuper。SomeClass
设置为最终的(这会阻止客户端扩展它)。也就是说,从
到
I doubt that this is possible using the
@Deprecated
annotation.If you have control over the sources you could however do the following:
SomeClass
toSomeClassSuper
SomeClass
and let it extendSomeClassSuper
.SomeClass
final (which prevents clients from extending it).That is, go from
to
不,没办法。您可以将类设为
final
,但这样您就会破坏现有的扩展。也许可以通过为 APT 开发定制处理器来实现。但一般情况下不会强制执行。
No, no way. You can make the class
final
, but thus you'll break existing extensions.Perhaps it is possible by developing a custom processor for the APT. But it will not be enforced in the general case.
如果它适合您的设计,您也许可以将所需的功能(例如后代需要访问的构造函数)设为私有,并提供扩展需要使用的受保护+已弃用的构造函数。
If it fits your design you could perhaps make needed functionality (e.g. constructors that descendants need to access) private, and provide a protected+deprecated constructor which extensions would need to use.
@Deprecated 注释不支持这一点,您应该将您的类标记为
final
。虽然这会导致编译器错误,但它更符合逻辑,扩展您的类要么是安全的,要么这样做是错误的。The @Deprecated annotation does not support this, you should mark your class
final
instead. While this causes a compiler error it is more logical, either it is safe to extend your class or it is an error to do it.