8

A couple of days ago, I started refactoring some code to use the new Java 8 Streams library. Unfortunately, I ran into a compile time error when performing Stream::map with a method which is declared to throw a generic E that is further specified to be a RuntimeException.

Interesting enough, the compile time error goes away when I switch to using a method reference.

Is this a bug, or is my method reference not equivalent to my lambda expression?

(Also, I know I can replace p->p.execute(foo) with Parameter::execute. My actual code has additional parameters for the execute method).


Error message

Error:(32, 43) java: unreported exception E; must be caught or declared to be thrown

Code

import java.util.ArrayList;
import java.util.List;


public class JavaBugTest
{
    interface AbleToThrowException<E extends Exception>
    {
    }

    interface Parameter {
        public <E extends Exception> Object execute(AbleToThrowException<E> algo) throws E;
    }

    interface ThrowsRuntimeException extends AbleToThrowException<RuntimeException>
    {
    }

    static ThrowsRuntimeException foo;


    public static Object manualLambda(Parameter p)
    {
        return p.execute(foo);
    }

    public static void main(String[] args)
    {
        List<Parameter> params = new ArrayList<>();
        params.stream().map(p -> p.execute(foo)); // Gives a compile time error.
        params.stream().map(JavaBugTest::manualLambda); // Works fine.
    }

}

System setup

  • OS: Windows x64
  • Java compiler version: Oracle JDK 1.8.0_11
  • IDE: Intellij
7
  • 1
    Which compiler are you using? I don't get a problem with Eclipse's compiler. But I do with Oracle's. Commented Jul 27, 2014 at 22:31
  • @SotiriosDelimanolis My IDE is Intellij. But, Intellij is claiming that javac is doing the actual compiling : "Information:Using javac 1.8.0_11 to compile java sources". Nevertheless, I guess I should try compiling manually from the command line.
    – user406009
    Commented Jul 27, 2014 at 22:33
  • 2
    Note that the two map invocations are not equivalent. Still, I think this is a bug in the compiler. Commented Jul 27, 2014 at 22:58
  • 1
    Note that the manualLambda case works by the simple fact that the manualLambda method does not declare any exceptions. Commented Jul 28, 2014 at 11:40
  • 2
    @SotiriosDelimanolis the bytecode Eclipse generates for both calls is identical. I agree that this is likely a bug in javac.
    – Jeff
    Commented Jul 29, 2014 at 2:46

1 Answer 1

3

A very simple solution is to explicitly provide a type argument for Parameter#execute(..).

params.stream().map(p -> p.<RuntimeException>execute(foo)); // Gives a compile time error.

Without the explicit type argument, it seems like the JDK compiler cannot infer a type argument from the invocation context, though it should. This a bug and should be reported as such. I have now reported it and will update this question with new details when I have them.

Bug Report

3
  • I think someone at the JAX2014 conference some months ago already mentioned this bug and already posted the link to the bugreport - but I cannot find it at the moment. But there is a list somewhere describing the differences between the eclipse compiler and javac concerning java 8 features.
    – mschenk74
    Commented Aug 3, 2014 at 9:24
  • @mschenk74 Did you happen to find the bugreport in the mean time? We are also experiencing this bug and would like to see the status. Commented Jun 18, 2015 at 12:51
  • I haven't found the bug report I mentioned yet.
    – mschenk74
    Commented Jun 18, 2015 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.