Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClassFileLoadHook/classfloadhk003.java
40948 views
1
/*
2
* Copyright (c) 2003, 2018, 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.jvmti.ClassFileLoadHook;
25
26
import java.io.*;
27
28
import nsk.share.*;
29
import nsk.share.jvmti.*;
30
31
/**
32
* Debuggee class of JVMTI test.
33
*/
34
public class classfloadhk003 extends DebugeeClass {
35
36
/** Load native library if required. */
37
static {
38
System.loadLibrary("classfloadhk003");
39
}
40
41
/** Run test from command line. */
42
public static void main(String argv[]) {
43
argv = nsk.share.jvmti.JVMTITest.commonInit(argv);
44
45
// JCK-compatible exit
46
System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
47
}
48
49
/** Run test from JCK-compatible environment. */
50
public static int run(String argv[], PrintStream out) {
51
return new classfloadhk003().runIt(argv, out);
52
}
53
54
/* =================================================================== */
55
56
/* constant names */
57
public static final String PACKAGE_NAME = "nsk.jvmti.ClassFileLoadHook";
58
public static final String TESTED_CLASS_NAME = PACKAGE_NAME + ".classfloadhk003r";
59
60
/* scaffold objects */
61
ArgumentHandler argHandler = null;
62
Log log = null;
63
long timeout = 0;
64
int status = Consts.TEST_PASSED;
65
66
/* bytecode of loaded class */
67
public static byte origClassBytes[] = null;
68
69
/* class loader for loaded class */
70
public static classfloadhk003ClassLoader classLoader = null;
71
72
/** Run debuggee code. */
73
public int runIt(String argv[], PrintStream out) {
74
argHandler = new ArgumentHandler(argv);
75
log = new Log(out, argHandler);
76
timeout = argHandler.getWaitTime() * 60 * 1000; // milliseconds
77
78
String args[] = argHandler.getArguments();
79
if (args.length <= 0) {
80
throw new Failure("Path for tested class file to load not specified");
81
}
82
83
String location = args[0];
84
String path = location + File.separator + "loadclass";
85
log.display("Using path to original tested class: \n\t" + path);
86
87
classLoader = new classfloadhk003ClassLoader(path);
88
89
log.display("Reading original bytecode of tested class: \n\t" + TESTED_CLASS_NAME);
90
try {
91
origClassBytes = classLoader.readBytes(path, TESTED_CLASS_NAME);
92
} catch (IOException e) {
93
throw new Failure("IOException in reading bytecode of tested class file:\n\t" + e);
94
}
95
96
log.display("Sync: debugee ready to load tested class");
97
status = checkStatus(status);
98
99
try {
100
log.display("Loading tested class: " + TESTED_CLASS_NAME);
101
Class<?> testedClass = Class.forName(TESTED_CLASS_NAME, true, classLoader);
102
} catch (ClassNotFoundException e) {
103
throw new Failure("Tested class not found: \n\t" + e);
104
}
105
106
log.display("Sync: tested class loaded");
107
status = checkStatus(status);
108
109
return status;
110
}
111
}
112
113
/* =================================================================== */
114
115
/** Classloader for tested class. */
116
class classfloadhk003ClassLoader extends ClassLoader {
117
private String path;
118
119
/** Make classloader providing path to class files. */
120
public classfloadhk003ClassLoader(String path) {
121
this.path = path;
122
}
123
124
/** Load specified class. */
125
protected Class<?> findClass(String name) throws ClassNotFoundException {
126
try {
127
byte[] bytes = readBytes(path, name);
128
return defineClass(name, bytes, 0, bytes.length);
129
} catch (IOException e) {
130
throw new ClassNotFoundException("IOException in loading class: " + name, e);
131
}
132
}
133
134
/** Read classfile for specified path and class name. */
135
public static byte[] readBytes(String path, String classname) throws IOException {
136
String filename = path + File.separator
137
+ classname.replace('.', File.separatorChar) + ".class";
138
FileInputStream in = new FileInputStream(filename);
139
byte[] bytes = new byte[in.available()];
140
in.read(bytes);
141
in.close();
142
return bytes;
143
}
144
}
145
146