Bug 338404 - [quick fix] Generify simple Comparators: Add type arguments to supertype, change parameters of compare(Object, Object)
Summary: [quick fix] Generify simple Comparators: Add type arguments to supertype, cha...
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.7   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard: fix candidate
Keywords:
Depends on:
Blocks:
 
Reported: 2011-02-28 07:16 EST by Markus Keller CLA
Modified: 2021-03-18 13:06 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 Markus Keller CLA 2011-02-28 07:16:13 EST
HEAD

Add a quick fix that parametrizes simple Comparators like this one:

package xy;

import java.util.*;

public class Try {
    public static void main(String[] args) {
        List numbers= Arrays.asList(42, 17, -4, 1, 3);
        foo(numbers);
        System.out.println(numbers);
    }

    static void foo(List list) {
        Collections.sort(list, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Integer i1= (Integer) o1;
                Integer i2= (Integer) o2;
                return i1.compareTo(i2);
            }
        });
    }
}

Expected result:
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer i1, Integer i2) {
                return i1.compareTo(i2);
            }
        });

The quick fix should be smart and also work if the casts are in different order or if other statements are between the casts.

It should also work if one or both of the casts are inlined, e.g. transform

            public int compare(Object o1, Object o2) {
                return ((Integer) o1).compareTo((Integer) o2);
            }
into
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }