This repository has been archived by the owner on Oct 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Basic.java
230 lines (212 loc) · 8.18 KB
/
Basic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8140450 8173898
* @summary Basic test for the StackWalker::walk method
* @run testng Basic
*/
import java.lang.StackWalker.StackFrame;
import java.lang.invoke.MethodType;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.lang.StackWalker.Option.*;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
public class Basic {
private static boolean verbose = false;
@DataProvider(name = "stackDepths")
public static Object[][] stackDepths() {
return new Object[][] {
{ new int[] { 12 }, new int[] { 4, 8, 12} },
{ new int[] { 18 }, new int[] { 8, 16, 20} },
{ new int[] { 32 }, new int[] { 16, 32, 64} },
};
}
/**
* For a stack of a given depth, it creates a StackWalker with an estimate.
* Test walking different number of frames
*/
@Test(dataProvider = "stackDepths")
public static void test(int[] depth, int[] estimates) {
Basic test = new Basic(depth[0]);
for (int estimate : estimates) {
test.walk(estimate);
}
}
@Test
public static void testWalkFromConstructor() throws Exception {
System.out.println("testWalkFromConstructor:");
List<String> found = ((ConstructorNewInstance)ConstructorNewInstance.class.getMethod("create")
.invoke(null)).collectedFrames();
assertEquals(List.of(ConstructorNewInstance.class.getName()+"::<init>",
ConstructorNewInstance.class.getName()+"::create",
Basic.class.getName()+"::testWalkFromConstructor"),
found);
}
@Test
public static void testMethodSignature() throws Exception {
List<StackFrame> frames = new StackBuilder(16, 16).build();
Map<String, MethodType> methodTypes = StackBuilder.methodTypes();
for (StackFrame f : frames) {
MethodType type = methodTypes.get(f.getMethodName());
if (type != null) {
System.out.format("%s.%s %s%n", f.getClassName(), f.getMethodName(),
f.getDescriptor());
String descriptor = f.getDescriptor();
if (!descriptor.equals(type.toMethodDescriptorString())) {
throw new RuntimeException("Expected: " + type.toMethodDescriptorString()
+ " got: " + f.getDescriptor());
}
if (!f.getMethodType().equals(type)) {
throw new RuntimeException("Expected: " + type
+ " got: " + f.getMethodType());
}
// verify descriptor returned by getDescriptor() before and after
// getMethodType() is called
if (!descriptor.equals(f.getDescriptor())) {
throw new RuntimeException("Mismatched: " + descriptor
+ " got: " + f.getDescriptor());
}
}
}
}
private final int depth;
Basic(int depth) {
this.depth = depth;
}
/*
* Setup a stack builder with the expected stack depth
* Walk the stack and count the frames.
*/
void walk(int estimate) {
int limit = Math.min(depth, 16);
List<StackFrame> frames = new StackBuilder(depth, limit).build();
System.out.format("depth=%d estimate=%d expected=%d walked=%d%n",
depth, estimate, limit, frames.size());
assertEquals(limit, frames.size());
}
static class ConstructorNewInstance {
static final StackWalker walker =
StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
List<String> testFramesOrReflectionFrames;
public ConstructorNewInstance() {
testFramesOrReflectionFrames = walker.walk(this::parse);
}
public List<String> collectedFrames() {
return testFramesOrReflectionFrames;
}
public boolean accept(StackFrame f) {
// Frames whose class names don't contain "."
// are our own test frames. These are the ones
// we expect.
// Frames whose class names contain ".reflect."
// are reflection frames. None should be present,
// since they are supposed to be filtered by
// by StackWalker. If we find any, we want to fail.
if (!f.getClassName().contains(".")
|| f.getClassName().contains(".reflect.")) {
System.out.println(" " + f);
return true;
}
// Filter out all other frames (in particular
// those from the test framework) in order to
// have predictable results.
return false;
}
public String frame(StackFrame f) {
return f.getClassName() + "::" + f.getMethodName();
}
List<String> parse(Stream<StackFrame> s) {
return s.filter(this::accept)
.map(this::frame)
.collect(Collectors.toList());
}
public static ConstructorNewInstance create() throws Exception {
return ConstructorNewInstance.class.getConstructor().newInstance();
}
}
static class StackBuilder {
private final int stackDepth;
private final int limit;
private int depth = 0;
private List<StackFrame> result;
StackBuilder(int stackDepth, int limit) {
this.stackDepth = stackDepth; // build method;
this.limit = limit;
}
List<StackFrame> build() {
trace("build");
m1();
return result;
}
void m1() {
trace("m1");
m2();
}
List m2() {
trace("m2");
m3();
return null;
}
int m3() {
trace("m3");
m4(null);
return 0;
}
void m4(Object o) {
trace("m4");
int remaining = stackDepth-depth-1;
if (remaining >= 4) {
m1();
} else {
filler(remaining);
}
}
void filler(int i) {
trace("filler");
if (i == 0)
walk();
else
filler(--i);
}
void walk() {
StackWalker walker = StackWalker.getInstance(RETAIN_CLASS_REFERENCE);
result = walker.walk(s -> s.limit(limit).collect(Collectors.toList()));
}
void trace(String methodname) {
++depth;
if (verbose)
System.out.format("%2d: %s%n", depth, methodname);
}
static Map<String, MethodType> methodTypes() throws Exception {
return Map.of("m1", MethodType.methodType(void.class),
"m2", MethodType.methodType(List.class),
"m3", MethodType.methodType(int.class),
"m4", MethodType.methodType(void.class, Object.class));
}
}
}