1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @summary check that sealed cant be extended by non-permitted subtypes
  27  * @compile -XDdontErrorIfSealedExtended SealedExtensionNegTest.java
  28  * @run main SealedExtensionNegTest
  29  */
  30 
  31 public class SealedExtensionNegTest {
  32     sealed interface SealedI permits SubI1 {}
  33     class SubI1 implements SealedI {}
  34     class SubI2 implements SealedI {}
  35     interface I1 extends SealedI {}
  36 
  37     sealed class Sealed permits Sub1 {}
  38     class Sub1 extends Sealed {}
  39     class Sub2 extends Sealed {}
  40 
  41     public static void main(String... args) {
  42         Class<?> subClass1 = SubI1.class;  // ok
  43 
  44         try {
  45             Class<?> subClass2 = SubI2.class;
  46             throw new AssertionError("error expected");
  47         } catch (VerifyError ve) {
  48             // good
  49         }
  50 
  51         try {
  52             Class<?> i1 = I1.class;
  53             throw new AssertionError("error expected");
  54         } catch (VerifyError ve) {
  55             // good
  56         }
  57 
  58         Class<?> sub1 = Sub1.class;  // ok
  59 
  60         try {
  61             Class<?> subClass2 = Sub2.class;
  62             throw new AssertionError("error expected");
  63         } catch (VerifyError ve) {
  64             // good
  65         }
  66     }
  67 }