Another Java 8 Lambda bug report

Luke De Gruchy luke.degruchy at intelerad.com
Fri Jan 25 09:48:16 PST 2013


Hi,

I have another bug to report.   I can't see why anybody would write new 
code that does this, but perhaps this would be the result of a 
refactoring of anonymous inner class code gone wrong.

As before, I'm running this code on RHEL 6 64-bit with the latest Java 8 
lambda build:

The following program, which combines anonymous inner classes and lambdas:

public class LambdaToAnon {
     public static void main( String[] args ) {
         Function<String,Function<String,String>> 
conditionalFunctionStartWithAnon =
         (aText) -> {
             if ( aText.length() > 3 ) {
                 return new Function<String,String>() {
                     @Override
                     public String apply(String newText) {
                         return newText;
                     }
                 };
             } else {
                 return new Function<String,String>() {
                     @Override
                     public String apply(String newText) {
                         return newText;
                     }
                 };
             }
         };
     }
}

Results in the following runtime error:

$ ~/Development/java/jdks/jdk1.8.0/bin/java LambdaToAnon
Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
Exception Details:
   Location:
     LambdaToAnon.main([Ljava/lang/String;)V @1: invokedynamic
   Reason:
     Type '[Ljava/lang/String;' (current frame, stack[0]) is not 
assignable to 'LambdaToAnon'
   Current Frame:
     bci: @1
     flags: { }
     locals: { '[Ljava/lang/String;' }
     stack: { '[Ljava/lang/String;' }
   Bytecode:
     0000000: 2aba 0002 0000 4cb1

     at java.lang.Class.getDeclaredMethods0(Native Method)
     at java.lang.Class.privateGetDeclaredMethods(Class.java:2443)
     at java.lang.Class.getMethod0(Class.java:2686)
     at java.lang.Class.getMethod(Class.java:1620)
     at 
sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:528)
     at 
sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:510)

Oddly enough, this program, which is a reverse of the previous program 
(anonymous inner classes calling lambdas, instead of the other way 
around), compiles and runs cleanly:

public class AnonToLambda {
     public static void main( String[] args ) {
         Function<String,Function<String,String>> 
conditionalFunctionStartWithAnon =
             new Function<String,Function<String,String>>() {
                 @Override
                 public Function<String,String> apply(String aText) {
                     if ( aText.length() > 3 ) {
                         return (text) -> ("My value");
                     } else {
                         return (text) -> (text);
                     }
                 }
         };
     };
}

Thanks,
Luke

-- 
Luke de Gruchy
Senior Software Developer
Intelerad Medical Systems Incorporated
895 de la Gauchetiere W, Suite 400
Montreal, Quebec, Canada H3B 4G1
luke.degruchy at intelerad.com
www.intelerad.com


-- 

This email or any attachments may contain confidential or legally 
privileged information intended for the sole use of the addressees. Any 
use, redistribution, disclosure, or reproduction of this information, 
except as intended, is prohibited. If you received this email in error, 
please notify the sender and remove all copies of the message, including 
any attachments.



More information about the lambda-dev mailing list