Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/RetransformClassesZeroLength.java
41153 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 819839326* @summary Instrumentation.retransformClasses(new Class[0]) should be NOP27* @requires vm.jvmti28* @library /test/lib29* @modules java.instrument30* @compile RetransformClassesZeroLength.java31* @run driver RetransformClassesZeroLength32*/3334import java.lang.instrument.ClassFileTransformer;35import java.lang.instrument.IllegalClassFormatException;36import java.lang.instrument.Instrumentation;37import java.lang.instrument.UnmodifiableClassException;38import java.nio.file.Files;39import java.nio.file.Path;40import java.nio.file.Paths;41import java.security.ProtectionDomain;4243import jdk.test.lib.process.ProcessTools;44import jdk.test.lib.helpers.ClassFileInstaller;4546public class RetransformClassesZeroLength {4748private static String manifest =49"Premain-Class: " + RetransformClassesZeroLength.Agent.class.getName() + "\n"50+ "Can-Retransform-Classes: true\n";5152private static String CP = System.getProperty("test.classes");5354public static void main(String args[]) throws Throwable {55String agentJar = buildAgent();56ProcessTools.executeProcess(57ProcessTools.createJavaProcessBuilder(58"-javaagent:" + agentJar,59"-version")60).shouldHaveExitValue(0);61}6263private static String buildAgent() throws Exception {64Path jar = Files.createTempFile(Paths.get("."), null, ".jar");65String jarPath = jar.toAbsolutePath().toString();66ClassFileInstaller.writeJar(jarPath,67ClassFileInstaller.Manifest.fromString(manifest),68RetransformClassesZeroLength.class.getName());69return jarPath;70}717273public static class Agent implements ClassFileTransformer {74public static void premain(String args, Instrumentation inst) {75inst.addTransformer(new NoOpTransformer());76try {77inst.retransformClasses(new Class[0]);78} catch (UnmodifiableClassException ex) {79throw new AssertionError(ex);80}81}82}8384private static class NoOpTransformer implements ClassFileTransformer {85@Override86public byte[] transform(ClassLoader loader,87String className,88Class<?> classBeingRedefined,89ProtectionDomain protectionDomain,90byte[] classfileBuffer91) throws IllegalClassFormatException {92return null; // no transform93}94}95}969798