Community
Participate
Working Groups
I upgraded from 3.2 M5a to 3.2 RC2 and noticed a problem with unnecessary cast warning decetion. I can't get the following example to compile cleanly with 3.2 RC2. The one with 'extra' cast worked fine with 3.2 M5a. public class EclipseCastWarningTest extends TestCase { private Method method; @Override protected void setUp() throws Exception { method = Long.class.getMethods()[0]; } public void testShouldNotProduceWarning() { // Unnecessary cast from Class<?>[] to Class[] Long.class.getMethod("hello", (Class[]) method.getParameterTypes()); } public void testShouldProduceWarning() throws Exception { // The argument of type Class<?>[] should explicitly be cast to // Class[] for the invocation of the varargs method getMethod(String, // Class...) from type Class<Long>. It could alternatively be cast to // Class for a varargs invocation. Long.class.getMethod("hello", method.getParameterTypes()); } }
Standalone test case: import java.lang.reflect.Method; public class EclipseCastWarningTest { private Method method; public void testShouldNotProduceWarning() throws SecurityException, NoSuchMethodException { // Unnecessary cast from Class<?>[] to Class[] Long.class.getMethod("hello", (Class[]) this.method.getParameterTypes()); } public void testShouldProduceWarning() throws Exception { // The argument of type Class<?>[] should explicitly be cast to // Class[] for the invocation of the varargs method getMethod(String, // Class...) from type Class<Long>. It could alternatively be cast to // Class for a varargs invocation. Long.class.getMethod("hello", this.method.getParameterTypes()); } }
Even simpler test case: public class X { Y<?> [] foo() { return null; } void bar(Y... y) { } void fred() { bar(foo()); bar((Y[])foo()); } } class Y<E> { }
In 3.1.2, there is no warning for: bar((Y[])foo());
Created attachment 40256 [details] Regression test
These are actually 2 places to fix. One in the method arguments invocation check to tune the test for varargs cast insertion. The other in CastExpression check for unnecessary warning; following same semantics.
Created attachment 40311 [details] Proposed patch
Need to test the fix extensively. A bit risky.
Actually the cast in "bar((Y[])foo());" should still be reported as unnecessary.
Created attachment 40688 [details] Better patch
Martin - does the presence of the extra warning alter JDT/UI behaviour ? i.e. bogus cleanup/refactoring etc... I have a fix I believe is right at hand.
Dani/Martin - may consider for 3.2RC4 if it has ramifications in your layer.
I support the fix as clean up works on the whole project and will remove the supposedly unnecessary casts and might introduce errors that users won't find that easely.
Ok, candidating for RC4. Dani - I need an additional vote
The patch makes sense. Approving for 3.2 RC4.
Fix released. Added VarargsTest#test048
Verified with I20060511-2000 for 3.2RC4