In a library project, I see numerous warnings suggesting that I reduce the visibility of public classes and methods. (Or in the case of methods I haven't written tests for, warnings about them being unused.) I'm using IntelliJ IDEA, but I assume that similar warnings would be seen if my project were imported into another IDE.

Should I use @SuppressWarnings("WeakerAccess") on all my intended entry points, or is there a more succinct IDE-agnostic way of expressing this?

Obviously, the fact that the classes and methods are public is supposed to convey that they're intended entry points. You could argue that the inspection just moves the possibility for error from the access modifier to the annotation. But thanks to the unfortunate defaults in most IDEs (which I've changed, BTW), mistakenly making something public is often an error of omission, whereas a mistaken annotation at least requires an error of commission. So I think the inspection is useful in that regard. I'm just not sure of the best way to suppress false positives.

  • 2
    I'm guessing your tests are in the same library as the code under test if it's warning you that methods don't need to be public? Move your tests to a separate library and that should fix that problem. As for areas of code without tests: write some tests! ;)
    – David Arno
    Jun 13 '16 at 7:46
  • Do you override methods where the original class and the overriding class specify different access modifiers for the same method (presumably public in the overriding class and protected in the base class)? Jun 13 '16 at 7:47
  • @DavidArno Good thinking! The tests were in the same package under src/test instead of src/main. Moving them to a different package eliminated many of the warnings. Jun 13 '16 at 8:04
  • @CodesInChaos No. Jun 13 '16 at 8:17

I would write a custom annotation, something like @API or @PublicFunction that will then suppress such warnings. Then you can directly specify which functions are part of the outward facing API, and which ones deserve real warnings.

I know some IDEs have this functionality built in, but I'm not sure about Java. I use Rider IDE with C# and has a [PublicAPI] annotation built in, that if added, suppresses such warnings. (Note that in c# annotations have [] instead of @). I suspect that if you want it to be IDE agnostic, you'll need to create one yourself. In java it's not to hard to make custom annotations.

I would avoid directly using warning suppression because if you later decide to remove a function from your public facing API you will miss the warning! Using the custom annotation, you can avoid this problem, and gain the added benefit of being able to search for all publicly exposed methods.

  • 1
    Since asking this question, I've converted strongly to the viewpoint expressed by David Arno in the comments. The warnings shouldn't be suppressed. They're telling you to write tests! Dec 18 '19 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.