Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/FieldAccessWatch/FieldAccessWatch.java
41153 views
1
/*
2
* Copyright (c) 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
/**
25
* @test
26
* @bug 8193369
27
* @summary Tests that all FieldAccess and FieldModification notifications
28
are generated.
29
* @requires vm.jvmti
30
* @compile FieldAccessWatch.java
31
* @run main/othervm/native -agentlib:FieldAccessWatch FieldAccessWatch
32
*/
33
34
import java.lang.reflect.Field;
35
import java.util.ArrayList;
36
import java.util.Arrays;
37
import java.util.List;
38
39
40
public class FieldAccessWatch {
41
42
private static final String agentLib = "FieldAccessWatch";
43
44
private static class MyItem {
45
}
46
47
private static class MyList {
48
public List<MyItem> items = new ArrayList<>();
49
}
50
51
public static void main(String[] args) throws Exception {
52
try {
53
System.loadLibrary(agentLib);
54
} catch (UnsatisfiedLinkError ex) {
55
System.err.println("Failed to load " + agentLib + " lib");
56
System.err.println("java.library.path: " + System.getProperty("java.library.path"));
57
throw ex;
58
}
59
60
if (!initWatchers(MyList.class, MyList.class.getDeclaredField("items"))) {
61
throw new RuntimeException("Watchers initializations error");
62
}
63
64
MyList list = new MyList();
65
66
test("[1]items.add(0, object)",() -> list.items.add(0, new MyItem()));
67
test("[2]items.add(object)", () -> list.items.add(new MyItem()));
68
test("[3]items.add(1, object)", () -> list.items.add(1, new MyItem()));
69
test("[4]items.add(object)", () -> list.items.add(new MyItem()));
70
test("[5]items.add(1, object)", () -> list.items.add(1, new MyItem()));
71
}
72
73
private static void log(String msg) {
74
System.out.println(msg);
75
System.out.flush();
76
}
77
78
// For every access/modify notification native part tries to locate
79
// boolean "<field_name>_access"/"<field_name>_modify" field and sets it to true
80
private static class TestResult {
81
// MyList.items
82
public boolean items_access;
83
84
// AbstractList.modCount
85
public boolean modCount_access;
86
public boolean modCount_modify;
87
88
// ArrayList.size
89
public boolean size_access;
90
public boolean size_modify;
91
92
// ArrayList.elementData
93
public boolean elementData_access;
94
95
// verify that all fields are set to true
96
public void verify() {
97
Arrays.stream(this.getClass().getDeclaredFields()).forEach(f -> verify(f));
98
}
99
100
private void verify(Field f) {
101
try {
102
if (!f.getBoolean(this)) {
103
throw new RuntimeException(f.getName() + " notification is missed");
104
}
105
} catch (IllegalAccessException ex) {
106
throw new RuntimeException(ex);
107
}
108
}
109
}
110
111
@FunctionalInterface
112
private interface TestAction {
113
void apply();
114
}
115
116
private static void test(String descr, TestAction action) throws Exception {
117
log(descr + ": starting");
118
TestResult result = new TestResult();
119
if (!startTest(result)) {
120
throw new RuntimeException("startTest failed");
121
}
122
action.apply();
123
// wait some time to ensure all posted events are handled
124
Thread.sleep(500);
125
126
stopTest();
127
128
// check the results
129
result.verify();
130
131
log(descr + ": OK");
132
}
133
134
private static native boolean initWatchers(Class cls, Field field);
135
private static native boolean startTest(TestResult results);
136
private static native void stopTest();
137
138
}
139
140