Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/RetransformClassesZeroLength.java
66646 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* @requires vm.flagless29* @library /test/lib30* @modules java.instrument31* @compile RetransformClassesZeroLength.java32* @run driver RetransformClassesZeroLength33*/3435import java.lang.instrument.ClassFileTransformer;36import java.lang.instrument.IllegalClassFormatException;37import java.lang.instrument.Instrumentation;38import java.lang.instrument.UnmodifiableClassException;39import java.nio.file.Files;40import java.nio.file.Path;41import java.nio.file.Paths;42import java.security.ProtectionDomain;4344import jdk.test.lib.process.ProcessTools;45import jdk.test.lib.helpers.ClassFileInstaller;4647public class RetransformClassesZeroLength {4849private static String manifest =50"Premain-Class: " + RetransformClassesZeroLength.Agent.class.getName() + "\n"51+ "Can-Retransform-Classes: true\n";5253private static String CP = System.getProperty("test.classes");5455public static void main(String args[]) throws Throwable {56String agentJar = buildAgent();57ProcessTools.executeProcess(58ProcessTools.createJavaProcessBuilder(59"-javaagent:" + agentJar,60"-version")61).shouldHaveExitValue(0);62}6364private static String buildAgent() throws Exception {65Path jar = Files.createTempFile(Paths.get("."), null, ".jar");66String jarPath = jar.toAbsolutePath().toString();67ClassFileInstaller.writeJar(jarPath,68ClassFileInstaller.Manifest.fromString(manifest),69RetransformClassesZeroLength.class.getName());70return jarPath;71}727374public static class Agent implements ClassFileTransformer {75public static void premain(String args, Instrumentation inst) {76inst.addTransformer(new NoOpTransformer());77try {78inst.retransformClasses(new Class[0]);79} catch (UnmodifiableClassException ex) {80throw new AssertionError(ex);81}82}83}8485private static class NoOpTransformer implements ClassFileTransformer {86@Override87public byte[] transform(ClassLoader loader,88String className,89Class<?> classBeingRedefined,90ProtectionDomain protectionDomain,91byte[] classfileBuffer92) throws IllegalClassFormatException {93return null; // no transform94}95}96}979899