Method ‘...’ is never used
就像這篇文章裡提到,警告的出現會形成一定程度上的困擾,最好是能夠消除掉,畢竟這是一個無法避免的寫法,但又不應該是個問題。
照「調整 Android Studio 編譯檢查規則」中的方法把檢查規則整個取消掉,是個一勞永逸的方法。但是也把其可能的錯誤也抑制掉了,徒增程式品質上的風險,並非上策。
另外一個可行的選項是在方法上宣告 SuppressWarnings 的 Annotation,像是 @SuppressWarnings("unused")、@SuppressWarnings({"UnusedDeclaration"})、@SuppressWarnings({"UnusedParameters”})。跟前一個方案一樣,應該顯示的警告也同時被遮蔽掉了,不是最佳的方式。
所幸 Android Studio 也不是沒有提供對策,在有問題的程式碼上按下「alt+enter」,會跳出 Menu 並可以選擇 Suppress for methods annotated by ‘...’ 項目。以 greenrobot 的 EventBus 為例,所有以 org.greenrobot.eventbus.Subscribe 為 Annotation 的 Method 都不會再顯示警告,所以問題順利地解決了。
以上的解決方法應該已經很多人都知道了,只是還有一個衍生的問題。如果使用的 Framework 提供的 Annotation 不只一個,還是得要一個一個地重覆相同的動作,不能一次搞定嗎?
這時可以開啟「調整 Android Studio 編譯檢查規則」中提到的 Inspections 選項畫面,切換到「Java->Declaration redundancy->Unused declaration」項目。畫面右方的 Options Frame 最下方有一個【Configure annotations...】的按鈕,點下去之後會再跳出另一個小視窗。接著就可以在視窗的最下方新增【Add Annotation Class】或【Add Annotations Pattern】,前一個是新增單一個 Class,另一個則是使用萬用字元來增加一整組的 Class。
以上說明的這些資訊都被存在 .idea/misc.xml 的檔案中,打開後會看到類似以下的內容:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<component name="EntryPointsManager"> | |
<entry_points version="2.0" /> | |
<list size="1"> | |
<item index="0" class="java.lang.String" itemvalue="org.greenrobot.eventbus.Subscribe" /> | |
</list> | |
</component> |
如果你有莫名的控制慾,想要凡事都掌握在手中,可以直接修改檔案的內容,也會達到一樣的效果。
0 意見:
張貼留言