langtools/test/tools/javac/lambda/TargetType63.java
author mcimadamore
Wed, 17 Jul 2013 14:04:01 +0100
changeset 18909 8f9fc5d876e4
permissions -rw-r--r--
8012242: Lambda compatibility and checked exceptions Summary: Inference variables in 'throws' clause with no constraints should be inferred as RuntimeException Reviewed-by: jjg, vromero

/*
 * @test /nodynamiccopyright/
 * @summary smoke test for inference of throws type variables
 * @compile/fail/ref=TargetType63.out -XDrawDiagnostics TargetType63.java
 */
class TargetType63 {

    interface F<T extends Throwable> {
        void m() throws T;
    }

    void g1() { }
    void g2() throws ClassNotFoundException { }
    void g3() throws Exception { }

    <Z extends Throwable> void m1(F<Z> fz) throws Z { }
    <Z extends ClassNotFoundException> void m2(F<Z> fz) throws Z { }

    void test1() {
        m1(()->{ }); //ok (Z = RuntimeException)
        m1(this::g1); //ok (Z = RuntimeException)
    }

    void test2() {
        m2(()->{ }); //fail (Z = ClassNotFoundException)
        m2(this::g1); //fail (Z = ClassNotFoundException)
    }

    void test3() {
        m1(()->{ throw new ClassNotFoundException(); }); //fail (Z = ClassNotFoundException)
        m1(this::g2); //fail (Z = ClassNotFoundException)
        m2(()->{ throw new ClassNotFoundException(); }); //fail (Z = ClassNotFoundException)
        m2(this::g2); //fail (Z = ClassNotFoundException)
    }

    void test4() {
        m1(()->{ throw new Exception(); }); //fail (Z = Exception)
        m1(this::g3); //fail (Z = Exception)
    }
}