Community
Participate
Working Groups
Build 3.2M5 There is no need to save value on stack before invoking subroutine in the case of "return null". public class X { public static void main(String[] args) { foo(); } static Object foo() { try { return null; } finally { System.out.println("SUCCESS"); } } }
Currently generates: // Method descriptor #19 ()Ljava/lang/Object; // Stack: 2, Locals: 2 static Object foo(); 0 aconst_null 1 astore_1 2 getstatic System.out : PrintStream [22] 5 ldc <String "SUCCESS"> [28] 7 invokevirtual PrintStream.println(String) : void [30] 10 aload_1 11 areturn 12 astore_0 13 getstatic System.out : PrintStream [22] 16 ldc <String "SUCCESS"> [28] 18 invokevirtual PrintStream.println(String) : void [30] 21 aload_0 22 athrow Exception Table: [pc: 0, pc: 2] -> 12 when : any
Added GenericTypeTest#test044
Now consider constant expressions and null literal in optimized scenario of return statements.
To verify this bug is fixed, the generated bytecode pattern should be shorter: In 1.5 target mode, instead of: // Method descriptor #19 ()Ljava/lang/Object; // Stack: 2, Locals: 2 static Object foo(); 0 aconst_null 1 astore_1 2 getstatic System.out : PrintStream [22] 5 ldc <String "SUCCESS"> [28] 7 invokevirtual PrintStream.println(String) : void [30] 10 aload_1 11 areturn ... it should be: // Method descriptor #19 ()Ljava/lang/Object; // Stack: 2, Locals: 1 static Object foo(); 0 getstatic System.out : PrintStream [22] 3 ldc <String "SUCCESS"> [28] 5 invokevirtual PrintStream.println(String) : void [30] 8 aconst_null 9 areturn ... Observe no temp 1 got allocated (one local less), and aconst_null directly precedes the return instruction.
Verified for 3.2 M6 using build I20060327-0010