1 /*
2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package nsk.jdi.HiddenClass.events;
25 
26 import java.io.File;
27 import java.lang.invoke.MethodHandles;
28 import java.lang.invoke.MethodHandles.Lookup;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
31 
32 import nsk.share.classload.ClassLoadUtils;
33 
34 /* Interface for tested hidden class to implement. */
35 interface HCInterf {
hcMethod()36     void hcMethod();
37 }
38 
39 /* Hidden class definition used to define tested hidden class
40  * with lookup.defineHiddenClass. */
41 public class HiddenClass implements HCInterf {
42     static String hcField = "<Not initialized>";
getHCField()43     static String getHCField() { return hcField; }
44 
getClassName()45     private String getClassName() {
46         return this.getClass().getName();
47     }
48 
hcMethod()49     public void hcMethod() {
50         hcField = getClassName();
51         if (hcField.indexOf("HiddenClass") == -1) {
52             throw new RuntimeException("Debuggee: Unexpected HiddenClass name: " + hcField);
53         }
54     }
55 
defineHiddenClass()56     public static Class<?> defineHiddenClass() throws Exception {
57         final String HC_NAME = HiddenClass.class.getName();
58         final String HC_PATH = ClassLoadUtils.getClassPath(HC_NAME) + File.separator +
59                                HC_NAME.replace(".", File.separator) + ".class";
60         Class<?> hc = defineHiddenClass(HC_PATH);
61         return hc;
62     }
63 
defineHiddenClass(String classFileName)64     private static Class<?> defineHiddenClass(String classFileName) throws Exception {
65         try {
66             Lookup lookup = MethodHandles.lookup();
67             byte[] bytes = Files.readAllBytes(Paths.get(classFileName));
68             Class<?> hc = lookup.defineHiddenClass(bytes, false).lookupClass();
69             return hc;
70         } catch (Exception ex) {
71             throw ex;
72         }
73     }
74 }
75