Path: blob/master/test/hotspot/jtreg/runtime/HiddenClasses/FieldInSuper.java
40942 views
/*1* Copyright (c) 2020, 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 823855026* @summary Test that verifer assignability checks involving the 'this' pointer27* work ok for hidden classes.28* @compile FieldInSuperSub.jasm29* @run main/othervm -Xverify:remote FieldInSuper30*/3132import java.io.ByteArrayOutputStream;33import java.io.File;34import java.io.FileInputStream;35import java.lang.invoke.MethodHandles;36import java.lang.invoke.MethodHandles.Lookup;37import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;38import java.nio.file.Path;39import java.nio.file.Paths;4041public class FieldInSuper {4243static final Path CLASSES_DIR = Paths.get(System.getProperty("test.classes"));44String hello = "hello";4546static byte[] readClassFile(String classFileName) throws Exception {47File classFile = new File(CLASSES_DIR + File.separator + classFileName);48try (FileInputStream in = new FileInputStream(classFile);49ByteArrayOutputStream out = new ByteArrayOutputStream()) {50int b;51while ((b = in.read()) != -1) {52out.write(b);53}54return out.toByteArray();55}56}5758public static void main(String[] args) throws Throwable {59Lookup lookup = MethodHandles.lookup();60byte[] bytes = readClassFile("FieldInSuperSub.class");6162// Define a hidden class that loads the 'this' pointer and then63// references field 'hello' using:64// getfield Field FieldInSuper.hello:"Ljava/lang/String;";65// This will cause the verifier to check that the 'this' pointer for66// the hidden class is assignable to FieldInSuper.67Class<?> c = lookup.defineHiddenClass(bytes, true, NESTMATE).lookupClass();68Object fss = c.newInstance();69c.getMethod("printMe").invoke(fss);70}7172}737475