Community
Participate
Working Groups
In the following code: class WidgetChildren { void addChildren(Widget w) { if (w instanceof Composite) { Composite composite = (Composite) w; addAll((Widget[]) composite.getChildren()); } } void addAll(Widget... widgets) { } } Eclipse flags the last line with the warning "Unnecessary cast from Control[] to Widget[]". Removing the cast, however, results in the warning "The argument of type Control[] should explicitly be cast to Widget[] for the invocation of the varargs method addAll(Widget...) from type WidgetChildren. It could alternatively be cast to Widget for a varargs invocation" Is it possible to remove one of these warnings?
Which build ID are you using ?
I20060922-0010
Reproduced. class X { void addChildren(Widget w) { if (w instanceof Composite) { Composite composite = (Composite) w; addAll((Widget[]) composite.getChildren()); addAll(composite.getChildren()); } } void addAll(Widget... widgets) { } } class Widget {} class Control extends Widget {} class Composite extends Control { Control[] getChildren() { return null; } }
We perform ok in following direct case: class X { void addChildren(Widget w) { if (w instanceof Composite) { Composite composite = (Composite) w; addAll((Control[]) composite.getChildren()); addAll(composite.getChildren()); } } void addAll(Control... widgets) { } } class Widget {} class Control extends Widget {} class Composite extends Control { Control[] getChildren() { return null; } }
I would expect the offending cast to be unnecessary.
And when unnecessary cast is removed, no varargs warning should occur either.
Created attachment 51397 [details] Proposed patch
Fixed in HEAD Released for 3.3M3 Added VarargsTest#test052-053
Verified for 3.3 M3 using build I20061030-0010
*** Bug 163889 has been marked as a duplicate of this bug. ***
*** Bug 170765 has been marked as a duplicate of this bug. ***