What hack can I use to suppress a warning about unused functions?

Consider a private method that is called from the JNI and is not used otherwise, generating a compiler warning about the unused method:

private void someMethodCalledOnlyFromJNI() { // WARNING: method is never used // .... } 

This is some kind of legacy code in Java 1.4 - so no @SuppressWarnings .

What hack would you use to suppress this compiler warning?


Edit: Obviously, this is just a warning and can be easily ignored. But, personally, I hate warnings in my code just as I don't want to see errors in it. AFAIC - my code should have 0 warnings , this may be an exaggeration, but I am very pedantic in this.

As an example, someone can see this function, I don’t know that it is used from JNI and just removes it.

+4
source share
5 answers
  • Ignore him. This warning, after all, is the best option.
  • use protected (and add a comment for this reason) - tolerant
  • Make the dummy method just above it and make two calls to each other (again with comments) - ugly
  • configure the IDE to not show this warning at all (in eclipse it's Windows > Preferences > Java > Compiler > Errors/Warnings ) - not preferable

According to your update : having 0 warnings is not the goal you should set. The number of warnings depends on the settings, so if you do not have all the IDEs combined, this number will be different. And then you can add checkstyle / PMD to report warnings - then you will have even more. Reasonable behavior is to have warnings.

If you do not want anyone to delete this method, just add a comment:

 // This method is used is used by JNI. (Don't delete) 
+6
source

Somewhere else in the class:

  if(Boolean.FALSE.booleanValue()) { // prevents warning for unused private method which is used from JNI someMethodCalledOnlyFromJNI(); } 

(you cannot use simple false , because this leads to a warning about dead code).

+5
source

Either simply ignore the warning, or declare it as protected . If you go to protected and want to prevent subclassing / overriding, then also declare it final .

+3
source

For starters, this is just a warning, so this should not be a problem for you.

You can either modify the code to remove this function, thereby fixing the problem.

Or just call it from some place at the beginning / end of your code and ignore any results. As long as he is not going to try to tweak any thing that will affect the rest of your program, everything will be fine.

+1
source

You can make it publicly available. if this is an outdated code, I'm sure no one will complain :)

0
source

Source: https://habr.com/ru/post/1305096/


All Articles