Bug 105801

Summary: [1.5][compiler] Too many warnings for non-matching types of arguments of varargs call
Product: [Eclipse Project] JDT Reporter: Markus Keller <markus.kell.r>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: VERIFIED FIXED QA Contact:
Severity: minor    
Priority: P3    
Version: 3.1   
Target Milestone: 3.2 M6   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Markus Keller CLA 2005-08-02 10:57:24 EDT
I20050726-1222

Too many warnings for non-matching types of arguments of varargs call:
- varargs(new Object[] {1, 2}) warns, but shouldn't
- varargs((Serializable) new Serializable[] {3, 4}) warns, but shouldn't

See inlined explanations:

import java.io.Serializable;
import java.util.Arrays;

public class Try {
    static void array(Serializable... items) {
        System.out.println(Arrays.deepToString(items));
    }
    static void varargs(Serializable... items) {
        System.out.println(Arrays.deepToString(items) + " (argument wrapped)");
    }

    public static void main(String[] args) {
        varargs(new Object[] {1, 2}); //warns "Varargs argument Object[]
            //should be cast to Serializable[] ..", but proposed cast to
            //Serializable[] fails at run time (javac does not warn here)
        array(new Serializable[] {3, 4});
        array(new Integer[] {5, 6}); //warns (as javac does)
        array(null); //warns (as javac does)
        
        varargs((Serializable) new Object[] {1, 2});
        varargs((Serializable) new Serializable[] {3, 4}); //warns about
            //unnecessary cast, although cast is necessary (causes varargs call)
        varargs((Serializable) new Integer[] {5, 6});
        varargs((Serializable) null);
        
        array((Serializable[]) new Serializable[] {3, 4}); //warns about
            //unnecessary cast
        array((Serializable[]) new Integer[] {5, 6});
        array((Serializable[]) null);
        array((Serializable[]) new Object[] {1, 2}); // CCE at run time
    }
}
Comment 1 Philipe Mulet CLA 2006-03-06 04:50:56 EST
Added VarargsTest#test038-045.

Tuned the reporting for unnecessary cast and need for varargs cast to address these discrepancies. Thanks for the excellent testcases.

Fixed
Comment 2 David Audel CLA 2006-03-28 06:39:13 EST
Verified for 3.2 M6 using build I20060328-0010