简体   繁体   中英

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? 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.

@Deprecated is the closest standard thing, and throwing UnsupportedOperationException is a reasonable thing to do in many similar cases.

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.

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. 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.

If you don't want others to invoke the method then make use @Deprecated annotation with proper java doc.
Note that, its just an indication to the caller, though they can still call the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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