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  * @bug 8198393
  27  * @summary Instrumentation.retransformClasses(new Class[0]) should be NOP
  28  * @library /test/lib
  29  * @modules java.instrument
  30  * @compile RetransformClassesZeroLength.java
  31  * @run main RetransformClassesZeroLength
  32  */
  33 
  34 import java.lang.instrument.ClassFileTransformer;
  35 import java.lang.instrument.IllegalClassFormatException;
  36 import java.lang.instrument.Instrumentation;
  37 import java.lang.instrument.UnmodifiableClassException;
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;
  41 import java.security.ProtectionDomain;
  42 
  43 import jdk.test.lib.process.ProcessTools;
  44 
  45 
  46 public class RetransformClassesZeroLength {
  47 
  48     private static String manifest =
  49             "Premain-Class: " + RetransformClassesZeroLength.Agent.class.getName() + "\n"
  50             + "Can-Retransform-Classes: true\n";
  51 
  52     private static String CP = System.getProperty("test.classes");
  53 
  54     public static void main(String args[]) throws Throwable {
  55         String agentJar = buildAgent();
  56         ProcessTools.executeProcess(
  57                 ProcessTools.createJavaProcessBuilder(
  58                         "-javaagent:" + agentJar,
  59                         "-version")
  60         ).shouldHaveExitValue(0);
  61     }
  62 
  63     private static String buildAgent() throws Exception {
  64         Path jar = Files.createTempFile(Paths.get("."), null, ".jar");
  65         String jarPath = jar.toAbsolutePath().toString();
  66         ClassFileInstaller.writeJar(jarPath,
  67                 ClassFileInstaller.Manifest.fromString(manifest),
  68                 RetransformClassesZeroLength.class.getName());
  69         return jarPath;
  70     }
  71 
  72 
  73     public static class Agent implements ClassFileTransformer {
  74         public static void premain(String args, Instrumentation inst) {
  75             inst.addTransformer(new NoOpTransformer());
  76             try {
  77                 inst.retransformClasses(new Class[0]);
  78             } catch (UnmodifiableClassException ex) {
  79                 throw new AssertionError(ex);
  80             }
  81         }
  82     }
  83 
  84     private static class NoOpTransformer implements ClassFileTransformer {
  85         @Override
  86         public byte[] transform(ClassLoader loader,
  87                                 String className,
  88                                 Class<?> classBeingRedefined,
  89                                 ProtectionDomain protectionDomain,
  90                                 byte[] classfileBuffer
  91                                 ) throws IllegalClassFormatException {
  92             return null;    // no transform
  93         }
  94     }
  95 }