Bug 76155 - [options] Unused code warning for unused methods in anonymous inner classes
Summary: [options] Unused code warning for unused methods in anonymous inner classes
Status: CLOSED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.1 RC2   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 96153 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-10-13 08:57 EDT by John Hendrikx CLA
Modified: 2005-06-10 09:59 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description John Hendrikx CLA 2004-10-13 08:57:43 EDT
When an anonymous inner class implements an interface like java.util.Comparator,
it makes little sense to include any public methods that are not in the
interface.  It is impossible to call them, except from within the inner class
itself:

new Comparator() {
    public int compare(Object o1, Object o2) {
        return 0;
    }

    public void uncallableMethod() {    // Unused code warning here
    }
}
Comment 1 John Hendrikx CLA 2004-10-29 07:02:41 EDT
More background: This warning is useful when refactoring interfaces.  Removing
methods in an interface can result in a lot of dead code in anonymous classes
implementing the interface.
Comment 2 Philipe Mulet CLA 2005-04-07 08:01:36 EDT
Deferring post 3.1
Comment 3 Philipe Mulet CLA 2005-05-30 05:28:11 EDT
reopening for consideration within 3.1
Comment 4 Philipe Mulet CLA 2005-05-30 05:36:50 EDT
Simple generalization of unused private members to all local members.
Comment 5 Philipe Mulet CLA 2005-05-30 07:20:21 EDT
Can be seen as simple to achieve, and a good polish item.
Also other tools like FindBugs support this scenario.

Erich: are you supporting this ? I have changes ready for it, trivial change.
Comment 6 Philipe Mulet CLA 2005-05-30 07:22:21 EDT
Dirk - do you have room for tuning UI for preferences, i.e. change 'unused or
unread private member' into 'unused private or local member' ?
Comment 7 Dirk Baeumer CLA 2005-05-30 09:19:44 EDT
This is an easy change as long as we don't add new problem ids.
Comment 8 Erich Gamma CLA 2005-05-30 09:23:53 EDT
This is useful and given the changes are trivial I have nothing against adding
it now.
Comment 9 Dirk Baeumer CLA 2005-05-30 10:06:44 EDT
Philippe, when support is in please forward the bug to JDT/UI so that I can
update the string.
Comment 10 Philipe Mulet CLA 2005-05-30 11:26:44 EDT
Dirk - entered bug 97284 for UI counterpart.
Comment 11 Philipe Mulet CLA 2005-05-30 11:58:03 EDT
Released changes to handle local members in same way as private ones. Reworded
problem message to read: "The field X.y is never read locally" in place of "The
private field X.y is never read locally". No new problem ID. 

Unused members are still special casing some well-known members for
serialization etc... also unused local members recognizes
overriding/implementing methods as implicit usage.

Adapted test suites which did contain lots of instances of code impacted by this.
Added NegativeTest#test440 to demonstrate specific example of it.
Comment 12 Philipe Mulet CLA 2005-06-01 17:55:06 EDT
*** Bug 96153 has been marked as a duplicate of this bug. ***
Comment 13 Olivier Thomann CLA 2005-06-06 20:33:18 EDT
Verified in N20050606-0010 + JDT/Core HEAD.
Comment 14 Jerome Lanneluc CLA 2005-06-10 09:59:34 EDT
Verified with I20050610-0010