Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/TestLambdaFormRetransformation.java
41153 views
/*1* Copyright (c) 2015, 2019, 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 800867826* @summary JSR 292: constant pool reconstitution must support pseudo strings27* @requires vm.jvmti28* @library /test/lib29* @modules java.base/jdk.internal.misc30* java.instrument31* java.management32* jdk.jartool/sun.tools.jar33* @compile -XDignore.symbol.file TestLambdaFormRetransformation.java34* @run main TestLambdaFormRetransformation35*/3637import java.io.IOException;38import java.lang.instrument.ClassFileTransformer;39import java.lang.instrument.IllegalClassFormatException;40import java.lang.instrument.Instrumentation;41import java.lang.instrument.UnmodifiableClassException;42import java.nio.file.Files;43import java.nio.file.Path;44import java.nio.file.Paths;45import java.security.ProtectionDomain;46import java.util.Arrays;4748import jdk.test.lib.process.ExitCode;49import jdk.test.lib.process.OutputAnalyzer;50import jdk.test.lib.process.ProcessTools;5152public class TestLambdaFormRetransformation {53private static String MANIFEST = String.format("Manifest-Version: 1.0\n" +54"Premain-Class: %s\n" +55"Can-Retransform-Classes: true\n",56Agent.class.getName());5758private static String CP = System.getProperty("test.classes");5960public static void main(String args[]) throws Throwable {61Path agent = TestLambdaFormRetransformation.buildAgent();62OutputAnalyzer oa = ProcessTools.executeTestJvm("-javaagent:" +63agent.toAbsolutePath().toString(), "-version");64oa.shouldHaveExitValue(ExitCode.OK.value);65}6667private static Path buildAgent() throws IOException {68Path manifest = TestLambdaFormRetransformation.createManifest();69Path jar = Files.createTempFile(Paths.get("."), null, ".jar");7071String[] args = new String[] {72"-cfm",73jar.toAbsolutePath().toString(),74manifest.toAbsolutePath().toString(),75"-C",76TestLambdaFormRetransformation.CP,77Agent.class.getName() + ".class"78};7980sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");8182if (!jarTool.run(args)) {83throw new Error("jar failed: args=" + Arrays.toString(args));84}85return jar;86}8788private static Path createManifest() throws IOException {89Path manifest = Files.createTempFile(Paths.get("."), null, ".mf");90byte[] manifestBytes = TestLambdaFormRetransformation.MANIFEST.getBytes();91Files.write(manifest, manifestBytes);92return manifest;93}94}9596class Agent implements ClassFileTransformer {97private static Runnable lambda = () -> {98System.out.println("I'll crash you!");99};100101public static void premain(String args, Instrumentation instrumentation) {102if (!instrumentation.isRetransformClassesSupported()) {103System.out.println("Class retransformation is not supported.");104return;105}106System.out.println("Calling lambda to ensure that lambda forms were created");107108Agent.lambda.run();109110System.out.println("Registering class file transformer");111112instrumentation.addTransformer(new Agent());113114for (Class c : instrumentation.getAllLoadedClasses()) {115if (c.getName().contains("LambdaForm") &&116instrumentation.isModifiableClass(c)) {117System.out.format("We've found a modifiable lambda form: %s%n", c.getName());118try {119instrumentation.retransformClasses(c);120} catch (UnmodifiableClassException e) {121throw new AssertionError("Modification of modifiable class " +122"caused UnmodifiableClassException", e);123}124}125}126}127128public static void main(String args[]) {129}130131@Override132public byte[] transform(ClassLoader loader,133String className,134Class<?> classBeingRedefined,135ProtectionDomain protectionDomain,136byte[] classfileBuffer137) throws IllegalClassFormatException {138System.out.println("Transforming " + className);139return classfileBuffer.clone();140}141}142143144