Community
Participate
Working Groups
Env: eclipse-gtk-3.1M3 linux JDT seems unable to handle the following short-cut if-else statement: public class ShortcutIfElse { public ShortcutIfElse() { } public Integer[] getTypes() { List<Integer> list = new ArrayList<Integer>(); return list == null ? new Integer[0] : list.toArray(new Integer[list.size()]); } public static void main(String[] args) { Class clazz = null; try { clazz = Class.forName("ShortcutIfElse"); } catch (Throwable e) { e.printStackTrace(); } } } Exception in thread "main" java.lang.VerifyError: (class: ShortcutIfElse, method: getTypes signature: ()[Ljava/lang/Integer;) Wrong return type in function at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:164) ...
We miss a checkcast after the toArray() call. We generate: 29 invokeinterface [nargs : 2] #30 <Interface method java/util/List.toArray([Ljava/lang/Object;)[Ljava/lang/Object;> 34 areturn instead of: 29 invokeinterface [nargs : 2] #6 <Interface method java/util/List.toArray([Ljava/lang/Object;)[Ljava/lang/Object;> 34 checkcast #7 [Ljava/lang/Integer; 37 areturn
java.util.List.toArray(...) signature is: <T> T[] toArray(T[] a) So we should implicitely generate a cast to T[].
Created attachment 15711 [details] Apply on HEAD This is a case where we optimized the implicit conversion of the false part. Doing this we didn't set the genericCast field and therefore there is no cast generated after the invokeinterface.
A workaround is to reverse the condition. return list != null ? list.toArray(new Integer[list.size()]) : new Integer[0] ; works fine without the patch.
Patch looks good. We are also missing #computeConversion calls further down in case: if (scope.environment().options.sourceLevel >= ClassFileConstants.JDK1_5) {
Added regression test: GenericTypeTest#test408. Fixed
Also added #test409 for other case mentionned in previous comment, corresponding to scenario: import java.util.*; public class X { public Number getTypes() { List<Integer> list = new ArrayList<Integer>(); return list == null ? Float.valueOf(0) : list.get(0); } public static void main(String[] args) { Class clazz = null; try { clazz = Class.forName("X"); } catch (Throwable e) { e.printStackTrace(); } } }
*** Bug 80605 has been marked as a duplicate of this bug. ***
Verified in 200412140800