Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/StackWalker/Basic.java
41149 views
1
/*
2
* Copyright (c) 2015, 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
/*
25
* @test
26
* @bug 8140450 8173898
27
* @summary Basic test for the StackWalker::walk method
28
* @run testng Basic
29
*/
30
31
import java.lang.StackWalker.StackFrame;
32
import java.lang.invoke.MethodType;
33
import java.util.List;
34
import java.util.Map;
35
import java.util.stream.Collectors;
36
import java.util.stream.Stream;
37
import static java.lang.StackWalker.Option.*;
38
39
import org.testng.annotations.DataProvider;
40
import org.testng.annotations.Test;
41
import static org.testng.Assert.*;
42
43
public class Basic {
44
private static boolean verbose = false;
45
46
@DataProvider(name = "stackDepths")
47
public static Object[][] stackDepths() {
48
return new Object[][] {
49
{ new int[] { 12 }, new int[] { 4, 8, 12} },
50
{ new int[] { 18 }, new int[] { 8, 16, 20} },
51
{ new int[] { 32 }, new int[] { 16, 32, 64} },
52
};
53
}
54
55
/**
56
* For a stack of a given depth, it creates a StackWalker with an estimate.
57
* Test walking different number of frames
58
*/
59
@Test(dataProvider = "stackDepths")
60
public static void test(int[] depth, int[] estimates) {
61
Basic test = new Basic(depth[0]);
62
for (int estimate : estimates) {
63
test.walk(estimate);
64
}
65
}
66
67
@Test
68
public static void testWalkFromConstructor() throws Exception {
69
System.out.println("testWalkFromConstructor:");
70
List<String> found = ((ConstructorNewInstance)ConstructorNewInstance.class.getMethod("create")
71
.invoke(null)).collectedFrames();
72
assertEquals(List.of(ConstructorNewInstance.class.getName()+"::<init>",
73
ConstructorNewInstance.class.getName()+"::create",
74
Basic.class.getName()+"::testWalkFromConstructor"),
75
found);
76
}
77
78
@Test
79
public static void testMethodSignature() throws Exception {
80
List<StackFrame> frames = new StackBuilder(16, 16).build();
81
Map<String, MethodType> methodTypes = StackBuilder.methodTypes();
82
for (StackFrame f : frames) {
83
MethodType type = methodTypes.get(f.getMethodName());
84
if (type != null) {
85
System.out.format("%s.%s %s%n", f.getClassName(), f.getMethodName(),
86
f.getDescriptor());
87
88
String descriptor = f.getDescriptor();
89
if (!descriptor.equals(type.toMethodDescriptorString())) {
90
throw new RuntimeException("Expected: " + type.toMethodDescriptorString()
91
+ " got: " + f.getDescriptor());
92
}
93
94
if (!f.getMethodType().equals(type)) {
95
throw new RuntimeException("Expected: " + type
96
+ " got: " + f.getMethodType());
97
}
98
99
// verify descriptor returned by getDescriptor() before and after
100
// getMethodType() is called
101
if (!descriptor.equals(f.getDescriptor())) {
102
throw new RuntimeException("Mismatched: " + descriptor
103
+ " got: " + f.getDescriptor());
104
}
105
}
106
}
107
}
108
109
private final int depth;
110
Basic(int depth) {
111
this.depth = depth;
112
}
113
114
/** For TestNG */
115
public Basic() {
116
depth = 0;
117
}
118
119
/*
120
* Setup a stack builder with the expected stack depth
121
* Walk the stack and count the frames.
122
*/
123
void walk(int estimate) {
124
int limit = Math.min(depth, 16);
125
List<StackFrame> frames = new StackBuilder(depth, limit).build();
126
System.out.format("depth=%d estimate=%d expected=%d walked=%d%n",
127
depth, estimate, limit, frames.size());
128
assertEquals(limit, frames.size());
129
}
130
131
static class ConstructorNewInstance {
132
static final StackWalker walker =
133
StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
134
List<String> testFramesOrReflectionFrames;
135
public ConstructorNewInstance() {
136
testFramesOrReflectionFrames = walker.walk(this::parse);
137
}
138
public List<String> collectedFrames() {
139
return testFramesOrReflectionFrames;
140
}
141
public boolean accept(StackFrame f) {
142
// Frames whose class names don't contain "."
143
// are our own test frames. These are the ones
144
// we expect.
145
// Frames whose class names contain ".reflect."
146
// are reflection frames. None should be present,
147
// since they are supposed to be filtered by
148
// by StackWalker. If we find any, we want to fail.
149
if (!f.getClassName().contains(".")
150
|| f.getClassName().contains(".reflect.")) {
151
System.out.println(" " + f);
152
return true;
153
}
154
// Filter out all other frames (in particular
155
// those from the test framework) in order to
156
// have predictable results.
157
return false;
158
}
159
public String frame(StackFrame f) {
160
return f.getClassName() + "::" + f.getMethodName();
161
}
162
List<String> parse(Stream<StackFrame> s) {
163
return s.filter(this::accept)
164
.map(this::frame)
165
.collect(Collectors.toList());
166
}
167
public static ConstructorNewInstance create() throws Exception {
168
return ConstructorNewInstance.class.getConstructor().newInstance();
169
}
170
}
171
172
static class StackBuilder {
173
private final int stackDepth;
174
private final int limit;
175
private int depth = 0;
176
private List<StackFrame> result;
177
StackBuilder(int stackDepth, int limit) {
178
this.stackDepth = stackDepth; // build method;
179
this.limit = limit;
180
}
181
List<StackFrame> build() {
182
trace("build");
183
m1();
184
return result;
185
}
186
void m1() {
187
trace("m1");
188
m2();
189
}
190
List m2() {
191
trace("m2");
192
m3();
193
return null;
194
}
195
int m3() {
196
trace("m3");
197
m4(null);
198
return 0;
199
}
200
void m4(Object o) {
201
trace("m4");
202
int remaining = stackDepth-depth-1;
203
if (remaining >= 4) {
204
m1();
205
} else {
206
filler(remaining);
207
}
208
}
209
void filler(int i) {
210
trace("filler");
211
if (i == 0)
212
walk();
213
else
214
filler(--i);
215
}
216
217
void walk() {
218
StackWalker walker = StackWalker.getInstance(RETAIN_CLASS_REFERENCE);
219
result = walker.walk(s -> s.limit(limit).collect(Collectors.toList()));
220
}
221
void trace(String methodname) {
222
++depth;
223
if (verbose)
224
System.out.format("%2d: %s%n", depth, methodname);
225
}
226
227
static Map<String, MethodType> methodTypes() throws Exception {
228
return Map.of("m1", MethodType.methodType(void.class),
229
"m2", MethodType.methodType(List.class),
230
"m3", MethodType.methodType(int.class),
231
"m4", MethodType.methodType(void.class, Object.class));
232
}
233
}
234
235
}
236
237