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 subtypes of sealed classes
  27  * @library /tools/lib
  28  * @modules jdk.jdeps/com.sun.tools.classfile
  29  *          jdk.compiler/com.sun.tools.javac.code
  30  *          jdk.compiler/com.sun.tools.javac.api
  31  *          jdk.compiler/com.sun.tools.javac.main
  32  *          jdk.compiler/com.sun.tools.javac.util
  33  * @build toolbox.ToolBox toolbox.JavacTask
  34  * @run main CheckSubtypesOfSealedTest
  35  * @ignore
  36  */
  37 
  38 import java.io.File;
  39 import java.nio.file.Paths;
  40 
  41 import com.sun.tools.classfile.*;
  42 import com.sun.tools.classfile.ConstantPool.CONSTANT_Utf8_info;
  43 import com.sun.tools.javac.code.Flags;
  44 import com.sun.tools.javac.util.Assert;
  45 
  46 import toolbox.JavacTask;
  47 import toolbox.ToolBox;
  48 
  49 public class CheckSubtypesOfSealedTest {
  50 
  51     static final String testSource =
  52             "import java.lang.annotation.*;\n" +
  53             "public class SealedClasses {\n" +
  54             "    @Sealed abstract class SAC {}\n" +
  55             "    abstract class SAC2 extends SAC {}\n" +
  56             "    class SAC3 extends SAC {}\n" +
  57             "    class SAC4 extends SAC2 {}\n" +
  58             "    SAC sac = new SAC() {};\n" +
  59             "    @Sealed interface SI {}\n" +
  60             "    interface SSI extends SI {}\n" +
  61             "    class SAC5 implements SI {}\n" +
  62             "    @NotSealed abstract class SAC6 extends SAC {}\n" +
  63             "    @NotSealed class SAC7 extends SAC {}\n" +
  64             "}";
  65 
  66     public static void main(String[] args) throws Exception {
  67         new CheckSubtypesOfSealedTest().run();
  68     }
  69 
  70     ToolBox tb = new ToolBox();
  71 
  72     void run() throws Exception {
  73         compileTestClass();
  74         checkClassFile(new File(Paths.get(System.getProperty("user.dir"), "SealedClasses$SAC2.class").toUri()), CheckFor.SEALED);
  75         checkClassFile(new File(Paths.get(System.getProperty("user.dir"), "SealedClasses$SAC3.class").toUri()), CheckFor.FINAL);
  76         checkClassFile(new File(Paths.get(System.getProperty("user.dir"), "SealedClasses$SAC4.class").toUri()), CheckFor.FINAL);
  77         checkClassFile(new File(Paths.get(System.getProperty("user.dir"), "SealedClasses$1.class").toUri()), CheckFor.FINAL);
  78         checkClassFile(new File(Paths.get(System.getProperty("user.dir"), "SealedClasses$SSI.class").toUri()), CheckFor.SEALED);
  79         checkClassFile(new File(Paths.get(System.getProperty("user.dir"), "SealedClasses$SAC5.class").toUri()), CheckFor.FINAL);
  80         checkClassFile(new File(Paths.get(System.getProperty("user.dir"), "SealedClasses$SAC6.class").toUri()), CheckFor.NOT_SEALED);
  81         checkClassFile(new File(Paths.get(System.getProperty("user.dir"), "SealedClasses$SAC7.class").toUri()), CheckFor.NOT_SEALED, CheckFor.NON_FINAL);
  82     }
  83 
  84     void compileTestClass() throws Exception {
  85         new JavacTask(tb)
  86                 .sources(testSource)
  87                 .run();
  88     }
  89 
  90     enum CheckFor {
  91         SEALED,
  92         FINAL,
  93         NON_FINAL,
  94         NOT_SEALED
  95     }
  96 
  97     void checkClassFile(final File cfile, CheckFor... checkFor) throws Exception {
  98         ClassFile classFile = ClassFile.read(cfile);
  99         for (CheckFor whatToCheckFor : checkFor) {
 100             if (whatToCheckFor == CheckFor.SEALED) {
 101                 for (Attribute attr: classFile.attributes) {
 102                     if (attr.getName(classFile.constant_pool).equals("RuntimeVisibleAnnotations")) {
 103                         RuntimeVisibleAnnotations_attribute rtva = (RuntimeVisibleAnnotations_attribute)attr;
 104                         Assert.check(rtva.annotations.length == 1, classFile.getName() + " should have only one runtime visible annotation");
 105                         CONSTANT_Utf8_info utfInfo = (CONSTANT_Utf8_info)classFile.constant_pool.get(rtva.annotations[0].type_index);
 106                         Assert.check(utfInfo.value.equals("Ljava/lang/annotation/Sealed;"), classFile.getName() + " should be sealed");
 107                         return;
 108                     }
 109                 }
 110                 throw new AssertionError(classFile.getName() + " should be sealed");
 111             } else if (whatToCheckFor == CheckFor.FINAL && (classFile.access_flags.flags & Flags.FINAL) == 0) {
 112                 throw new AssertionError(classFile.getName() + " should be final");
 113             } else if (whatToCheckFor == CheckFor.NON_FINAL && (classFile.access_flags.flags & Flags.FINAL) != 0) {
 114                 throw new AssertionError(classFile.getName() + " should not be final");
 115             } else if (whatToCheckFor == CheckFor.NOT_SEALED) {
 116                 for (Attribute attr: classFile.attributes) {
 117                     if (attr.getName(classFile.constant_pool).equals("RuntimeVisibleAnnotations")) {
 118                         RuntimeVisibleAnnotations_attribute rtva = (RuntimeVisibleAnnotations_attribute)attr;
 119                         Assert.check(rtva.annotations.length == 1, classFile.getName() + " should have only one runtime visible annotation");
 120                         CONSTANT_Utf8_info utfInfo = (CONSTANT_Utf8_info)classFile.constant_pool.get(rtva.annotations[0].type_index);
 121                         Assert.check(utfInfo.value.equals("Ljava/lang/annotation/NotSealed;"), classFile.getName() + " should not be sealed");
 122                         return;
 123                     }
 124                 }
 125                 throw new AssertionError(classFile.getName() + " should not be sealed");
 126             }
 127         }
 128     }
 129 }