Bug 162171

Summary: [1.5][compiler] Needs explicit cast / unnecessary cast confusion with varargs
Product: [Eclipse Project] JDT Reporter: Pekka Enberg <penberg>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: VERIFIED WORKSFORME QA Contact:
Severity: normal    
Priority: P3    
Version: 3.3   
Target Milestone: 3.3 M5   
Hardware: PC   
OS: Linux   
Whiteboard:

Description Pekka Enberg CLA 2006-10-25 03:38:21 EDT
As demonstrated with the below test case, passing an subclass array to a method that takes a vararg parameter of the parent class causes Eclipse to print out a warning that you need an explicit cast. However, when adding an explicit cast, Eclipse prints out a warning that the cast is redundant.

public class VarargsTest extends TestCase {
    public void testPassingSubclassArrayAsVararg() {
        // The argument of type VarargsTest.Subclass[] should explicitly be
        // cast to VarargsTest.Parent[] for the invocation of the varargs
        // method processVararg(VarargsTest.Parent...) from type VarargsTest.
        // It could alternatively be cast to VarargsTest.Parent for a varargs
        // invocation
        processVararg(new Subclass[] {});
    }

    public void testPassingSubclassArrayAsVarargWithCast() {
        // Unnecessary cast from VarargsTest.Subclass[] to
        // VarargsTest.Parent[]
        processVararg((Parent[]) new Subclass[] {});
    }

    private void processVararg(Parent... objs) {
    }

    class Parent {
    }

    class Subclass extends Parent {
    }
}
Comment 1 Philipe Mulet CLA 2007-01-10 07:50:17 EST
The warning requiring explicit cast should not appear.
This was resolved a while ago; now you can safely remove the unnecessary cast.

Worksforme.

Added VarargsTest#test058