1 /*
   2  * Copyright (c) 2014, 2019, 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 Test that PermittedSubtypes attribute cannot be redefined.
  27  * @library /test/lib
  28  * @modules java.base/jdk.internal.misc
  29  * @modules java.compiler
  30  *          java.instrument
  31  *          jdk.jartool/sun.tools.jar
  32  * @run main RedefineClassHelper
  33  * @run main/othervm -javaagent:redefineagent.jar RedefinePermittedSubtypes
  34  */
  35 
  36 // package access top-level class to avoid problem with RedefineClassHelper
  37 // and nested types.
  38 sealed class RedefinePermittedSubtypes_B permits java.lang.String, java.lang.Class { }
  39 
  40 public class RedefinePermittedSubtypes {
  41 
  42     public static String newBGood =
  43         "sealed class RedefinePermittedSubtypes_B permits java.lang.Class, java.lang.String { }";
  44 
  45     public static String newBBad =
  46         "sealed class RedefinePermittedSubtypes_B permits java.lang.String { }";
  47 
  48     public static void main(String[] args) throws Exception {
  49 
  50         // Test redefinition is okay if permitted types are the same.
  51         RedefineClassHelper.redefineClass(RedefinePermittedSubtypes_B.class, newBGood);
  52 
  53         // Test redefinition fails if permitted types are different.
  54         try {
  55             RedefineClassHelper.redefineClass(RedefinePermittedSubtypes_B.class, newBBad);
  56            throw new RuntimeException("Expected UnsupportedOperationException not thrown");
  57         } catch (java.lang.UnsupportedOperationException e) {
  58             if (!e.getMessage().contains("class redefinition failed: attempted to change the class NestHost, NestMembers, or PermittedSubtypes attribute")) {
  59                 throw new RuntimeException(
  60                     "Wrong UnsupportedOperationException thrown:" + e.getMessage());
  61             }
  62         }
  63     }
  64 }