Community
Participate
Working Groups
Code example ------------ * before save: MethodHandle mh = lookup.findVirtual(instance.getClass(), "toString", MethodType.methodType(String.class)); String actual = (String) mh.invoke(instance); * after save: MethodHandle mh = lookup.findVirtual(instance.getClass(), "toString", MethodType.methodType(String.class)); String actual = mh.invoke(instance); This will cause a compilation error, mh.invoke returns an java.lang.Object and must be casted to String. When Save Action "remove unnecessary casts" is enabled the cast to String is removed even though the cast is necessary.
I cannot reproduce the problem using R4.2. If you can still reproduce this, then please attach code that allows us to reproduce the problem.
Created attachment 219359 [details] eclipse preferences dialog
Created attachment 219360 [details] eclipse editor before save
Created attachment 219361 [details] eclipse editor after save
(In reply to comment #1) > I cannot reproduce the problem using R4.2. If you can still reproduce this, > then please attach code that allows us to reproduce the problem. I'm using OS X 10.8 and Eclipse Juno, Build id: 20120614-1722 and when I have "remove unnecessary casts" enabled for my save actions, the cast in the code below is removed when I save the file import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; public class EclipseBug { public static void main(String[] args) throws Throwable { String str = "test"; MethodHandle mh = MethodHandles.lookup().findVirtual(String.class, "toString", MethodType.methodType(String.class)); String actual = (String) mh.invoke(str); assert "test".equals(actual); } } Which in turn results in a compilation failure: "Type mismatch: cannot convert from Object to String" I've attached a couple of screenshots.
Thanks for the source example. I can reproduce it now. This got broken in 3.7.1.
JDT Core does not report the cast to be unnecessary.
(In reply to comment #7) > JDT Core does not report the cast to be unnecessary. That's wrong. The option is just set to "ignore" by default. The bug is that the compiler does report the cast as unnecessary, although it is not unnecessary in this specific example, since MethodHandle#invoke(..) is an @PolymorphicSignature method. I think the fix can be implemented quite locally in CastExpression#resolveType(BlockScope) where there is already code that explicitly deals with polymorphic signature methods. > This got broken in 3.7.1. That's because Java7 support was only added there.
(In reply to comment #8) > (In reply to comment #7) > > JDT Core does not report the cast to be unnecessary. > > That's wrong. The option is just set to "ignore" by default. I too used to my own preferences. Sorry about that.
Tentatively target 3.8.1, May need resetting after analysis.
Created attachment 219659 [details] Patch under test
Jay, thanks for a quick review of this patch.
Released in master via http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=4b88df3d6aa6e51d756e8a42349e40785ab5b763 for 4.3 M1 Released for 3.8.1 via http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?h=R3_8_maintenance&id=ea2576c1a82d41bcfcd9f16f595541bf5bd702a9
Patch looks good to me.
Reran the tests on master and all are green.
Verified for 4.3 M1 with build I20120808-2000
Verified for 3.8.1 with build M20120809-1200.