开发者

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

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.


  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)


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


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.


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.


you can make it public. if it's legacy code I am sure no one will complain :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜