Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/HiddenClass/events/HiddenClass.java
41161 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*/2223package nsk.jdi.HiddenClass.events;2425import java.io.File;26import java.lang.invoke.MethodHandles;27import java.lang.invoke.MethodHandles.Lookup;28import java.nio.file.Files;29import java.nio.file.Paths;3031import nsk.share.classload.ClassLoadUtils;3233/* Interface for tested hidden class to implement. */34interface HCInterf {35void hcMethod();36}3738/* Hidden class definition used to define tested hidden class39* with lookup.defineHiddenClass. */40public class HiddenClass implements HCInterf {41static String hcField = "<Not initialized>";42static String getHCField() { return hcField; }4344private String getClassName() {45return this.getClass().getName();46}4748public void hcMethod() {49hcField = getClassName();50if (hcField.indexOf("HiddenClass") == -1) {51throw new RuntimeException("Debuggee: Unexpected HiddenClass name: " + hcField);52}53}5455public static Class<?> defineHiddenClass() throws Exception {56final String HC_NAME = HiddenClass.class.getName();57final String HC_PATH = ClassLoadUtils.getClassPath(HC_NAME) + File.separator +58HC_NAME.replace(".", File.separator) + ".class";59Class<?> hc = defineHiddenClass(HC_PATH);60return hc;61}6263private static Class<?> defineHiddenClass(String classFileName) throws Exception {64try {65Lookup lookup = MethodHandles.lookup();66byte[] bytes = Files.readAllBytes(Paths.get(classFileName));67Class<?> hc = lookup.defineHiddenClass(bytes, false).lookupClass();68return hc;69} catch (Exception ex) {70throw ex;71}72}73}747576