Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/RedefineClasses/RedefineGenericSignatureTest.java
66646 views
/*1* Copyright (c) 2022, 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 828224126* @summary Verifies class redefinition correctly updates generic_signature and source_file_name attributes27* @requires vm.jvmti28* @modules java.base/jdk.internal.org.objectweb.asm29* java.instrument30* @library /test/lib31* @run main RedefineClassHelper32* @run main/othervm -javaagent:redefineagent.jar --add-opens=java.base/java.lang=ALL-UNNAMED RedefineGenericSignatureTest33*/3435import java.io.File;36import java.io.FileOutputStream;37import java.lang.invoke.MethodHandle;38import java.lang.invoke.MethodHandles;39import java.lang.invoke.MethodType;40import java.lang.reflect.Type;41import java.nio.file.Files;42import java.util.List;4344import jdk.internal.org.objectweb.asm.ClassReader;45import jdk.internal.org.objectweb.asm.ClassVisitor;46import jdk.internal.org.objectweb.asm.ClassWriter;47import jdk.internal.org.objectweb.asm.Opcodes;48import jdk.test.lib.Asserts;49import jdk.test.lib.JDKToolLauncher;50import jdk.test.lib.compiler.InMemoryJavaCompiler;51import jdk.test.lib.process.ProcessTools;52import jdk.test.lib.process.OutputAnalyzer;5354class GenericSignatureTester {55public GenericSignatureTarget<List<String>> method1() {56return null;57}58}5960class GenericSignatureTarget<T extends List<?>> {61public GenericSignatureTarget<T> foo() { return null; }62public static void throwException() { throw new RuntimeException(); }63}6465public class RedefineGenericSignatureTest {66private static final String newTargetClassSource =67"class GenericSignatureTarget<T> {\n" +68" public GenericSignatureTarget<T> foo() { return null; }\n" +69" public static void throwException() { throw new RuntimeException(); }\n" +70"}\n";7172public static void main (String[] args) throws Throwable {73RedefineGenericSignatureTest test = new RedefineGenericSignatureTest();74test.runTest();75}7677private final static String sourceFileName = "RedefineGenericSignatureTest.java";78private final static String sourceFileNameNew = "RedefineGenericSignatureTestNew.java";79// expected signature of GenericSignatureTester.method1 return type80private final static String expectedRetType = "GenericSignatureTarget<java.util.List<java.lang.String>>";81// expected generic signature of the original GenericSignatureTarget82private final static String expectedSigOld = "<T::Ljava/util/List<*>;>Ljava/lang/Object;";83// expected generic signature of the redefined GenericSignatureTarget84private final static String expectedSigNew = "<T:Ljava/lang/Object;>Ljava/lang/Object;";8586private static void log(Object o) {87System.out.println(o);88}8990private String getTargetGenSig() throws Throwable {91MethodHandles.Lookup lookup = MethodHandles.lookup();92MethodHandles.Lookup classLookup = MethodHandles.privateLookupIn(Class.class, lookup);93MethodHandle getGenericSignature0 = classLookup.findVirtual(94Class.class, "getGenericSignature0", MethodType.methodType(String.class));95Object genericSignature = getGenericSignature0.invoke(GenericSignatureTarget.class);96return String.valueOf(genericSignature);97}9899private String getTesterRetType() throws Throwable {100Type type = GenericSignatureTester.class.getDeclaredMethod("method1").getGenericReturnType();101return String.valueOf(type);102}103104private String getTargetSourceFilename() {105try {106GenericSignatureTarget.throwException();107} catch (RuntimeException ex) {108return ex.getStackTrace()[0].getFileName();109}110return "Cannot get source file name";111}112113// Prints dissassembled class bytes.114private void printDisassembled(String description, Class cls, byte[] bytes) throws Exception {115log(description + " -------------------");116117File f = new File(cls.getSimpleName()+".class");118try (FileOutputStream fos = new FileOutputStream(f)) {119fos.write(bytes);120}121JDKToolLauncher javap = JDKToolLauncher.create("javap")122.addToolArg("-verbose")123.addToolArg("-p") // Shows all classes and members.124//.addToolArg("-c") // Prints out disassembled code125.addToolArg("-s") // Prints internal type signatures.126.addToolArg(f.toString());127ProcessBuilder pb = new ProcessBuilder(javap.getCommand());128OutputAnalyzer out = ProcessTools.executeProcess(pb);129out.shouldHaveExitValue(0);130try {131Files.delete(f.toPath());132} catch (Exception ex) {133// ignore134}135out.asLines().forEach(s -> log(s));136log("==========================================");137Files.deleteIfExists(f.toPath());138}139140private byte[] getNewClassBytes() {141byte[] bytecode = InMemoryJavaCompiler.compile(GenericSignatureTarget.class.getName(), newTargetClassSource);142143ClassWriter cw = new ClassWriter(0);144ClassReader cr = new ClassReader(bytecode);145cr.accept(new ClassVisitor(Opcodes.ASM7, cw) {146private boolean sourceSet = false;147@Override148public void visitSource(String source, String debug) {149sourceSet = true;150log("Changing source: \"" + source + "\" -> \"" + sourceFileNameNew + "\"");151super.visitSource(sourceFileNameNew, debug);152}153154@Override155public void visitEnd() {156if (!sourceSet) {157log("Set source: \"" + sourceFileNameNew + "\"");158super.visitSource(sourceFileNameNew, null);159}160super.visitEnd();161}162}, 0);163return cw.toByteArray();164}165166private void runTest() throws Throwable {167Class targetClass = GenericSignatureTarget.class;168169String oldSig = getTargetGenSig();170log("old target class sig: \"" + oldSig + "\"");171172byte[] oldClassBytes = targetClass.getResourceAsStream(targetClass.getName() + ".class").readAllBytes();173printDisassembled("Old " + targetClass.getName(), targetClass, oldClassBytes);174175log("Redefining " + targetClass.getName() + " class");176byte[] newClassBytes = getNewClassBytes();177printDisassembled("New " + targetClass.getName(), targetClass, newClassBytes);178RedefineClassHelper.redefineClass(targetClass, newClassBytes);179180String newSig = getTargetGenSig();181log("new target class sig: \"" + newSig + "\"");182183String newRetType = getTesterRetType();184log("new tester ret type: \"" + newRetType + "\"");185186String newSrcFileName = getTargetSourceFilename();187log("new source file name: \"" + newSrcFileName + "\"");188189Asserts.assertStringsEqual(expectedSigOld, oldSig, "wrong old generic signature");190Asserts.assertStringsEqual(expectedSigNew, newSig, "wrong new generic signature");191Asserts.assertStringsEqual(expectedRetType, newRetType, "wrong ret type");192Asserts.assertStringsEqual(sourceFileNameNew, newSrcFileName, "wrong new source file name");193}194}195196197