Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What hack can I use to suppress an unused function warning?

Tags:

java

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

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

This is some legacy code in Java 1.4 - so no dice on @SuppressWarnings.

What hack would you use to suppress this compiler warning?


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

Just as an example, someone might see this function, not know it is used from JNI, and simply delete it.

like image 482
Yuval Adam Avatar asked Mar 25 '10 13:03

Yuval Adam


4 Answers

  1. Ignore it. It is a warning, after all - best option
  2. use protected (and add a comment for the reason why) - bearable
  3. Make a dummy method just above it and make the two call each other (again with comments) - ugly
  4. configure the IDE not to show this warning at all (in eclipse it is Windows > Preferences > Java > Compiler > Errors/Warnings) - not preferable

As per your update: having 0 warnings is not a goal you should set. The number of warnings depends on the settings, so if you don't all have unified IDEs, this number will vary. And then you can add checkstyle / PMD to report warnings as well - then you'll have even more. The reasonable behaviour is to have a warnings treshold.

If you don't want anyone to delete this method, just add a comment:

// This method is used is used by JNI. (Don't delete)
like image 164
Bozho Avatar answered Oct 14 '22 07:10

Bozho


Somewhere else in the class:

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

(can't use simple false because that results in dead code warning).

like image 21
Michael Borgwardt Avatar answered Oct 14 '22 05:10

Michael Borgwardt


Either just ignore the warning, or declare it as protected instead. If you go for protected and want to prevent subclassing/overriding as well, then declare it final as well.

like image 3
BalusC Avatar answered Oct 14 '22 05:10

BalusC


To start with, its only a warning, thus it should not be an issue for you.

You could either mod the code to remove that function thus removing the problem.

Or just call it from some where at the start/end of your code and ignore any results. As long as it is not going to try to set up any thing that will affect the rest of your program you will be fine.

like image 1
thecoshman Avatar answered Oct 14 '22 05:10

thecoshman