7039822: Project Coin: add explicit tests for the lub of an exception parameter

Wed, 27 Apr 2011 17:03:24 -0700

author
darcy
date
Wed, 27 Apr 2011 17:03:24 -0700
changeset 985
a8f5cad1e6bb
parent 984
4c5f13798b8d
child 986
5c81ba0eddff

7039822: Project Coin: add explicit tests for the lub of an exception parameter
Reviewed-by: mcimadamore, jjg

test/tools/javac/multicatch/Neg07.java file | annotate | diff | comparison | revisions
test/tools/javac/multicatch/Neg07.out file | annotate | diff | comparison | revisions
test/tools/javac/multicatch/Pos10.java file | annotate | diff | comparison | revisions
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/multicatch/Neg07.java	Wed Apr 27 17:03:24 2011 -0700
     1.3 @@ -0,0 +1,50 @@
     1.4 +/*
     1.5 + * @test  /nodynamiccopyright/
     1.6 + * @bug 7039822
     1.7 + * @summary Verify typing of lub of exception parameter w.r.t getClass
     1.8 + * @author Joseph D. Darcy
     1.9 + * @compile/fail/ref=Neg07.out -XDrawDiagnostics Neg07.java
    1.10 + */
    1.11 +
    1.12 +public class Neg07 {
    1.13 +    private static void test(int i) {
    1.14 +        try {
    1.15 +            thrower(i);
    1.16 +        } catch (SonException | DaughterException e) {
    1.17 +            Class<? extends HasFoo> clazz2 = e.getClass(); // Rejected!
    1.18 +            HasFoo m = e;
    1.19 +            e.foo();
    1.20 +        }
    1.21 +    }
    1.22 +
    1.23 +    private static interface HasFoo {
    1.24 +        void foo();
    1.25 +    }
    1.26 +
    1.27 +    static void thrower(int i) throws SonException, DaughterException {
    1.28 +        if (i == 0)
    1.29 +            throw new SonException();
    1.30 +        else
    1.31 +            throw new DaughterException();
    1.32 +    }
    1.33 +
    1.34 +    private static class ParentException extends RuntimeException {}
    1.35 +
    1.36 +    private static class SonException
    1.37 +        extends ParentException
    1.38 +        implements HasFoo {
    1.39 +
    1.40 +        public void foo() {
    1.41 +            System.out.println("SonException.foo");
    1.42 +        }
    1.43 +    }
    1.44 +
    1.45 +    private static class DaughterException
    1.46 +        extends ParentException
    1.47 +        implements HasFoo {
    1.48 +
    1.49 +        public void foo() {
    1.50 +            System.out.println("DaughterException.foo");
    1.51 +        }
    1.52 +    }
    1.53 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/multicatch/Neg07.out	Wed Apr 27 17:03:24 2011 -0700
     2.3 @@ -0,0 +1,2 @@
     2.4 +Neg07.java:14:56: compiler.err.prob.found.req: (compiler.misc.incompatible.types), java.lang.Class<compiler.misc.type.captureof: 1, ? extends Neg07.ParentException>, java.lang.Class<? extends Neg07.HasFoo>
     2.5 +1 error
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/multicatch/Pos10.java	Wed Apr 27 17:03:24 2011 -0700
     3.3 @@ -0,0 +1,103 @@
     3.4 +/*
     3.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + *
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + *
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/*
    3.28 + * @test
    3.29 + * @bug 7039822
    3.30 + * @summary Verify lub of an exception parameter can be an intersection type
    3.31 + * @author Joseph D. Darcy
    3.32 + */
    3.33 +
    3.34 +public class Pos10 {
    3.35 +    public static void main(String... args) {
    3.36 +        test(0);
    3.37 +        test(1);
    3.38 +
    3.39 +        if (record != 0b11)
    3.40 +            throw new RuntimeException("Unexpected exception execution: " +
    3.41 +                                       record);
    3.42 +        if (closeRecord != 0b11)
    3.43 +            throw new RuntimeException("Unexpected close execution: " +
    3.44 +                                       closeRecord);
    3.45 +    }
    3.46 +
    3.47 +    private static int record = 0;
    3.48 +    private static int closeRecord = 0;
    3.49 +
    3.50 +    private static void test(int i) {
    3.51 +        try {
    3.52 +            thrower(i);
    3.53 +        } catch (SonException | DaughterException e) {
    3.54 +            Class<? extends ParentException> clazz = e.getClass();
    3.55 +            HasFoo m = e;
    3.56 +            e.foo();
    3.57 +
    3.58 +            try (AutoCloseable ac = e) {
    3.59 +                e.toString();
    3.60 +            } catch(Exception except) {
    3.61 +                throw new RuntimeException(except);
    3.62 +            }
    3.63 +        }
    3.64 +    }
    3.65 +
    3.66 +    private static interface HasFoo {
    3.67 +        void foo();
    3.68 +    }
    3.69 +
    3.70 +    private static void thrower(int i) throws SonException, DaughterException {
    3.71 +        if (i == 0)
    3.72 +            throw new SonException();
    3.73 +        else
    3.74 +            throw new DaughterException();
    3.75 +    }
    3.76 +
    3.77 +    private static class ParentException extends RuntimeException {}
    3.78 +
    3.79 +    private static class SonException
    3.80 +        extends ParentException
    3.81 +        implements HasFoo, AutoCloseable {
    3.82 +
    3.83 +        public void foo() {
    3.84 +            record |= 0b01;
    3.85 +        }
    3.86 +
    3.87 +        @Override
    3.88 +        public void close() {
    3.89 +            closeRecord |= 0b01;
    3.90 +        }
    3.91 +    }
    3.92 +
    3.93 +    private static class DaughterException
    3.94 +        extends ParentException
    3.95 +        implements HasFoo, AutoCloseable {
    3.96 +
    3.97 +        public void foo() {
    3.98 +            record |= 0b10;
    3.99 +        }
   3.100 +
   3.101 +        @Override
   3.102 +        public  void close() {
   3.103 +            closeRecord |= 0b10;
   3.104 +        }
   3.105 +    }
   3.106 +}

mercurial