Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 322497

Summary: add code style problem for invocation of varargs method with unnecessary "new Type[] {..}"
Product: [Eclipse Project] JDT Reporter: Markus Keller <markus.kell.r>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: NEW --- QA Contact:
Severity: enhancement    
Priority: P3 CC: akurtakov, daniel_megert, jjohnstn
Version: 3.7   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Markus Keller CLA 2010-08-12 05:57:59 EDT
HEAD

We could add a code style problem for an invocation of a varargs method with unnecessary "new Type[] {..}" right in the arguments list.

Example:
public class Try {
    public static void main(String[] args) {
        foo("x", "1", "1");
        foo("x", 1);
        foo("x", "a", "b");
        
        // add a problem for these two:
        foo("x", new String[] {"a"});
        foo("x", new Object[] {"a", "b"});
    }
    
    static void foo(String first, Object... o) {
        System.out.println(o.getClass());
    }
}

JDT/UI can then add a quick fix to remove the unnecessary explicit array creation. The quick fix should also work in the 'new String[] {"a"}' case in the example, although there's another problem reported there.