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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @summary checking for permitted subtypes attribute at runtime
  29  * @modules jdk.compiler/com.sun.tools.javac.util
  30  */
  31 
  32 import com.sun.tools.javac.util.Assert;
  33 
  34 public class CheckingAttributeAtRuntimeTest {
  35 
  36     sealed class Sealed1 permits Sub1 {}
  37 
  38     class Sub1 extends Sealed1 {}
  39 
  40     sealed interface SealedI1 permits Sub2 {}
  41 
  42     class Sub2 implements SealedI1 {}
  43 
  44     sealed class Sealed2 {}
  45 
  46     class Sub3 extends Sealed2 {}
  47 
  48     public static void main(String... args) {
  49         Class<?> sealedClass1 = Sealed1.class;
  50         Assert.check(sealedClass1.isSealed());
  51         Assert.check(sealedClass1.getPermittedSubtypes().length == 1);
  52         Assert.check(sealedClass1.getPermittedSubtypes()[0] == Sub1.class);
  53 
  54         Class<?> sealedI = SealedI1.class;
  55         Assert.check(sealedI.isSealed());
  56         Assert.check(sealedI.getPermittedSubtypes().length == 1);
  57         Assert.check(sealedI.getPermittedSubtypes()[0] == Sub2.class);
  58 
  59         Class<?> sealedClass2 = Sealed2.class;
  60         Assert.check(sealedClass2.isSealed());
  61         Assert.check(sealedClass2.getPermittedSubtypes().length == 1);
  62         Assert.check(sealedClass2.getPermittedSubtypes()[0] == Sub3.class);
  63 
  64         Class<?> sealedClass3 = Sealed3.class;
  65         Assert.check(sealedClass3.isSealed());
  66         Assert.check(sealedClass3.getPermittedSubtypes().length == 1);
  67         Assert.check(sealedClass3.getPermittedSubtypes()[0] == Sub4.class);
  68 
  69         Class<?> sealedClass4 = Sealed4.class;
  70         Assert.check(sealedClass4.isSealed());
  71         Assert.check(sealedClass4.getPermittedSubtypes().length == 1);
  72         Assert.check(sealedClass4.getPermittedSubtypes()[0] == Sub5.class);
  73     }
  74 }
  75 
  76 sealed class Sealed3 {}
  77 
  78 class Sub4 extends Sealed3 {}
  79 
  80 sealed class Sealed4 permits Sub5 {}
  81 
  82 class Sub5 extends Sealed4 {}