1 /*
   2  * Copyright (c) 2020, 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  * @bug 8225056
  27  * @library /test/lib
  28  * @summary Test that a class that is a sealed class can be redefined.
  29  * @modules java.base/jdk.internal.misc
  30  * @modules java.instrument
  31  *          jdk.jartool/sun.tools.jar
  32  * @compile --enable-preview -source ${jdk.version} RedefineSealedClass.java
  33  * @run main/othervm --enable-preview RedefineSealedClass buildagent
  34  * @run main/othervm/timeout=6000 --enable-preview RedefineSealedClass runtest
  35  */
  36 
  37 import java.io.FileNotFoundException;
  38 import java.io.PrintWriter;
  39 import java.lang.RuntimeException;
  40 import java.lang.instrument.ClassFileTransformer;
  41 import java.lang.instrument.Instrumentation;
  42 import java.security.ProtectionDomain;
  43 import java.lang.instrument.IllegalClassFormatException;
  44 import jdk.test.lib.process.ProcessTools;
  45 import jdk.test.lib.process.OutputAnalyzer;
  46 
  47 public class RedefineSealedClass {
  48 
  49     final class A extends Tester { }
  50     final class B extends Tester { }
  51 
  52     sealed static class Tester permits A, B {}
  53 
  54     static class LoggingTransformer implements ClassFileTransformer {
  55 
  56         public LoggingTransformer() {}
  57 
  58         public byte[] transform(ClassLoader loader, String className,
  59                                 Class classBeingRedefined, ProtectionDomain protectionDomain,
  60                                 byte[] classfileBuffer) throws IllegalClassFormatException {
  61             return null;
  62         }
  63     }
  64 
  65     public static void premain(String agentArgs, Instrumentation inst) throws Exception {
  66         LoggingTransformer t = new LoggingTransformer();
  67         inst.addTransformer(t, true);
  68         {
  69             Class demoClass = Class.forName("RedefineSealedClass$Tester");
  70             inst.retransformClasses(demoClass);
  71         }
  72     }
  73 
  74     private static void buildAgent() {
  75         try {
  76             ClassFileInstaller.main("RedefineSealedClass");
  77         } catch (Exception e) {
  78             throw new RuntimeException("Could not write agent classfile", e);
  79         }
  80 
  81         try {
  82             PrintWriter pw = new PrintWriter("MANIFEST.MF");
  83             pw.println("Premain-Class: RedefineSealedClass");
  84             pw.println("Agent-Class: RedefineSealedClass");
  85             pw.println("Can-Redefine-Classes: true");
  86             pw.println("Can-Retransform-Classes: true");
  87             pw.close();
  88         } catch (FileNotFoundException e) {
  89             throw new RuntimeException("Could not write manifest file for the agent", e);
  90         }
  91 
  92         sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");
  93         if (!jarTool.run(new String[] { "-cmf", "MANIFEST.MF", "redefineagent.jar", "RedefineSealedClass.class" })) {
  94             throw new RuntimeException("Could not write the agent jar file");
  95         }
  96     }
  97 
  98     public static void main(String argv[]) throws Exception {
  99         if (argv.length == 1 && argv[0].equals("buildagent")) {
 100             buildAgent();
 101             return;
 102         }
 103         if (argv.length == 1 && argv[0].equals("runtest")) {
 104             String[] javaArgs1 = { "-XX:MetaspaceSize=12m", "-XX:MaxMetaspaceSize=12m",
 105                                    "-javaagent:redefineagent.jar", "--enable-preview",
 106                                    "RedefineSealedClass"};
 107             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(javaArgs1);
 108             OutputAnalyzer output = new OutputAnalyzer(pb.start());
 109             output.shouldNotContain("processing of -javaagent failed");
 110         }
 111     }
 112 }