简体   繁体   English

将方法标记为永远不应使用

[英]Mark method as should never be used

i would like to know if there is some good way in java to show that some method should never be invoked? 我想知道在Java中是否有一些好方法可以表明永远不要调用某些方法? I'm thinking in case that I work with bigger team, and to tell guys that this method will never be invoked, so don't try to test it. 我在考虑是否要与更大的团队合作,并告诉大家永远不要调用此方法,因此请不要尝试对其进行测试。 Maybe assert is a good idea? 断言是个好主意吗?

Context: i'm using ReactiveX in java and my Observable will never be stopped, it can only invoke onError and onNext, but onCompleted will never be invoked, but my Observer extends Subscriber so i need to @Override onCompleted. 上下文:我在Java中使用ReactiveX,并且我的Observable永远不会停止,它只能调用onError和onNext,但是永远不会调用onCompleted,但是我的Observer扩展了Subscriber,因此我需要@Override onCompleted。

@Deprecated is the closest standard thing, and throwing UnsupportedOperationException is a reasonable thing to do in many similar cases. @Deprecated是最接近的标准操作,在许多类似情况下,抛出UnsupportedOperationException是合理的操作。

Note also that due to polymorphism, the caller may well not know the concrete type of the Observer they are calling until runtime, so no annotation could help. 还要注意,由于多态性,调用者可能直到运行时才知道他们正在调用的Observer的具体类型,因此没有注释可以提供帮助。

However, in the case you've described, I think you should write an empty implementation of onCompleted() and a test that just asserts that it doesn't throw an exception. 但是,在您描述的情况下,我认为您应该编写一个空的onCompleted()和一个仅断言它不会引发异常的测试。 Although you've decided that the Observable will never stop producing, it doesn't seem reasonable to throw an exception if one day it does. 尽管您已经确定Observable永远不会停止产生,但是如果有一天抛出异常,似乎是不合理的。

If you don't want others to invoke the method then make use @Deprecated annotation with proper java doc. 如果您不希望其他人调用该方法,请在适当的Java文档中使用@Deprecated批注。
Note that, its just an indication to the caller, though they can still call the method. 请注意,尽管调用者仍然可以调用该方法,但这仅是对调用者的指示。

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

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