author | alanb |
Thu, 01 Dec 2016 08:57:53 +0000 | |
changeset 42338 | a60f280f803c |
parent 38784 | c0a88deb692a |
child 43712 | 5dfd0950317c |
child 43694 | fcc6fff17bfa |
permissions | -rw-r--r-- |
34362 | 1 |
/* |
38784
c0a88deb692a
8152893: StackWalker#getCallerClass is not filtering hidden/ reflection frames when walker is configured to show hidden /reflection frames
bchristi
parents:
38569
diff
changeset
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
34362 | 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package java.lang; |
|
26 |
||
37363
329dba26ffd2
8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents:
36511
diff
changeset
|
27 |
import jdk.internal.reflect.CallerSensitive; |
34362 | 28 |
|
29 |
import java.util.*; |
|
30 |
import java.util.function.Consumer; |
|
31 |
import java.util.function.Function; |
|
32 |
import java.util.stream.Stream; |
|
33 |
||
34 |
/** |
|
35 |
* A stack walker. |
|
36 |
* |
|
37 |
* <p> The {@link StackWalker#walk walk} method opens a sequential stream |
|
38 |
* of {@link StackFrame StackFrame}s for the current thread and then applies |
|
39 |
* the given function to walk the {@code StackFrame} stream. |
|
40 |
* The stream reports stack frame elements in order, from the top most frame |
|
41 |
* that represents the execution point at which the stack was generated to |
|
42 |
* the bottom most frame. |
|
43 |
* The {@code StackFrame} stream is closed when the {@code walk} method returns. |
|
44 |
* If an attempt is made to reuse the closed stream, |
|
45 |
* {@code IllegalStateException} will be thrown. |
|
46 |
* |
|
47 |
* <p> The {@linkplain Option <em>stack walking options</em>} of a |
|
48 |
* {@code StackWalker} determines the information of |
|
49 |
* {@link StackFrame StackFrame} objects to be returned. |
|
50 |
* By default, stack frames of the reflection API and implementation |
|
51 |
* classes are {@linkplain Option#SHOW_HIDDEN_FRAMES hidden} |
|
52 |
* and {@code StackFrame}s have the class name and method name |
|
53 |
* available but not the {@link StackFrame#getDeclaringClass() Class reference}. |
|
54 |
* |
|
55 |
* <p> {@code StackWalker} is thread-safe. Multiple threads can share |
|
56 |
* a single {@code StackWalker} object to traverse its own stack. |
|
57 |
* A permission check is performed when a {@code StackWalker} is created, |
|
58 |
* according to the options it requests. |
|
59 |
* No further permission check is done at stack walking time. |
|
60 |
* |
|
61 |
* @apiNote |
|
62 |
* Examples |
|
63 |
* |
|
64 |
* <p>1. To find the first caller filtering a known list of implementation class: |
|
65 |
* <pre>{@code |
|
66 |
* StackWalker walker = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE); |
|
67 |
* Optional<Class<?>> callerClass = walker.walk(s -> |
|
68 |
* s.map(StackFrame::getDeclaringClass) |
|
69 |
* .filter(interestingClasses::contains) |
|
70 |
* .findFirst()); |
|
71 |
* }</pre> |
|
72 |
* |
|
73 |
* <p>2. To snapshot the top 10 stack frames of the current thread, |
|
74 |
* <pre>{@code |
|
75 |
* List<StackFrame> stack = StackWalker.getInstance().walk(s -> |
|
76 |
* s.limit(10).collect(Collectors.toList())); |
|
77 |
* }</pre> |
|
78 |
* |
|
79 |
* Unless otherwise noted, passing a {@code null} argument to a |
|
80 |
* constructor or method in this {@code StackWalker} class |
|
81 |
* will cause a {@link NullPointerException NullPointerException} |
|
82 |
* to be thrown. |
|
83 |
* |
|
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
34697
diff
changeset
|
84 |
* @since 9 |
34362 | 85 |
*/ |
86 |
public final class StackWalker { |
|
87 |
/** |
|
88 |
* A {@code StackFrame} object represents a method invocation returned by |
|
89 |
* {@link StackWalker}. |
|
90 |
* |
|
91 |
* <p> The {@link #getDeclaringClass()} method may be unsupported as determined |
|
92 |
* by the {@linkplain Option stack walking options} of a {@linkplain |
|
93 |
* StackWalker stack walker}. |
|
94 |
* |
|
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
34697
diff
changeset
|
95 |
* @since 9 |
34362 | 96 |
* @jvms 2.6 |
97 |
*/ |
|
98 |
public static interface StackFrame { |
|
99 |
/** |
|
100 |
* Gets the <a href="ClassLoader.html#name">binary name</a> |
|
101 |
* of the declaring class of the method represented by this stack frame. |
|
102 |
* |
|
103 |
* @return the binary name of the declaring class of the method |
|
104 |
* represented by this stack frame |
|
105 |
* |
|
106 |
* @jls 13.1 The Form of a Binary |
|
107 |
*/ |
|
108 |
public String getClassName(); |
|
109 |
||
110 |
/** |
|
111 |
* Gets the name of the method represented by this stack frame. |
|
112 |
* @return the name of the method represented by this stack frame |
|
113 |
*/ |
|
114 |
public String getMethodName(); |
|
115 |
||
116 |
/** |
|
117 |
* Gets the declaring {@code Class} for the method represented by |
|
118 |
* this stack frame. |
|
119 |
* |
|
120 |
* @return the declaring {@code Class} of the method represented by |
|
121 |
* this stack frame |
|
122 |
* |
|
123 |
* @throws UnsupportedOperationException if this {@code StackWalker} |
|
124 |
* is not configured with {@link Option#RETAIN_CLASS_REFERENCE |
|
125 |
* Option.RETAIN_CLASS_REFERENCE}. |
|
126 |
*/ |
|
127 |
public Class<?> getDeclaringClass(); |
|
128 |
||
129 |
/** |
|
37819
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
130 |
* Returns the index to the code array of the {@code Code} attribute |
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
131 |
* containing the execution point represented by this stack frame. |
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
132 |
* The code array gives the actual bytes of Java Virtual Machine code |
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
133 |
* that implement the method. |
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
134 |
* |
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
135 |
* @return the index to the code array of the {@code Code} attribute |
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
136 |
* containing the execution point represented by this stack frame, |
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
137 |
* or a negative number if the method is native. |
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
138 |
* |
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
139 |
* @jvms 4.7.3 The {@code Code} Attribute |
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
140 |
*/ |
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
141 |
public int getByteCodeIndex(); |
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
142 |
|
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
143 |
/** |
34362 | 144 |
* Returns the name of the source file containing the execution point |
145 |
* represented by this stack frame. Generally, this corresponds |
|
146 |
* to the {@code SourceFile} attribute of the relevant {@code class} |
|
147 |
* file as defined by <cite>The Java Virtual Machine Specification</cite>. |
|
148 |
* In some systems, the name may refer to some source code unit |
|
149 |
* other than a file, such as an entry in a source repository. |
|
150 |
* |
|
151 |
* @return the name of the file containing the execution point |
|
37819
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
152 |
* represented by this stack frame, or {@code null} if |
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
153 |
* this information is unavailable. |
34362 | 154 |
* |
155 |
* @jvms 4.7.10 The {@code SourceFile} Attribute |
|
156 |
*/ |
|
37819
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
157 |
public String getFileName(); |
34362 | 158 |
|
159 |
/** |
|
160 |
* Returns the line number of the source line containing the execution |
|
161 |
* point represented by this stack frame. Generally, this is |
|
162 |
* derived from the {@code LineNumberTable} attribute of the relevant |
|
163 |
* {@code class} file as defined by <cite>The Java Virtual Machine |
|
164 |
* Specification</cite>. |
|
165 |
* |
|
166 |
* @return the line number of the source line containing the execution |
|
37819
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
167 |
* point represented by this stack frame, or a negative number if |
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
168 |
* this information is unavailable. |
34362 | 169 |
* |
170 |
* @jvms 4.7.12 The {@code LineNumberTable} Attribute |
|
171 |
*/ |
|
37819
8a2559d6fe5b
8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
37363
diff
changeset
|
172 |
public int getLineNumber(); |
34362 | 173 |
|
174 |
/** |
|
175 |
* Returns {@code true} if the method containing the execution point |
|
176 |
* represented by this stack frame is a native method. |
|
177 |
* |
|
178 |
* @return {@code true} if the method containing the execution point |
|
179 |
* represented by this stack frame is a native method. |
|
180 |
*/ |
|
181 |
public boolean isNativeMethod(); |
|
182 |
||
183 |
/** |
|
184 |
* Gets a {@code StackTraceElement} for this stack frame. |
|
185 |
* |
|
186 |
* @return {@code StackTraceElement} for this stack frame. |
|
36511 | 187 |
*/ |
188 |
public StackTraceElement toStackTraceElement(); |
|
34362 | 189 |
} |
190 |
||
191 |
/** |
|
192 |
* Stack walker option to configure the {@linkplain StackFrame stack frame} |
|
193 |
* information obtained by a {@code StackWalker}. |
|
194 |
* |
|
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
34697
diff
changeset
|
195 |
* @since 9 |
34362 | 196 |
*/ |
197 |
public enum Option { |
|
198 |
/** |
|
199 |
* Retains {@code Class} object in {@code StackFrame}s |
|
200 |
* walked by this {@code StackWalker}. |
|
201 |
* |
|
202 |
* <p> A {@code StackWalker} configured with this option will support |
|
203 |
* {@link StackWalker#getCallerClass()} and |
|
204 |
* {@link StackFrame#getDeclaringClass() StackFrame.getDeclaringClass()}. |
|
205 |
*/ |
|
206 |
RETAIN_CLASS_REFERENCE, |
|
207 |
/** |
|
208 |
* Shows all reflection frames. |
|
209 |
* |
|
210 |
* <p>By default, reflection frames are hidden. This includes the |
|
211 |
* {@link java.lang.reflect.Method#invoke} method |
|
212 |
* and the reflection implementation classes. A {@code StackWalker} with |
|
213 |
* this {@code SHOW_REFLECT_FRAMES} option will show all reflection frames. |
|
214 |
* The {@link #SHOW_HIDDEN_FRAMES} option can also be used to show all |
|
215 |
* reflection frames and it will also show other hidden frames that |
|
216 |
* are implementation-specific. |
|
217 |
*/ |
|
218 |
SHOW_REFLECT_FRAMES, |
|
219 |
/** |
|
220 |
* Shows all hidden frames. |
|
221 |
* |
|
222 |
* <p>A Java Virtual Machine implementation may hide implementation |
|
223 |
* specific frames in addition to {@linkplain #SHOW_REFLECT_FRAMES |
|
224 |
* reflection frames}. A {@code StackWalker} with this {@code SHOW_HIDDEN_FRAMES} |
|
225 |
* option will show all hidden frames (including reflection frames). |
|
226 |
*/ |
|
227 |
SHOW_HIDDEN_FRAMES; |
|
228 |
} |
|
229 |
||
230 |
enum ExtendedOption { |
|
231 |
/** |
|
232 |
* Obtain monitors, locals and operands. |
|
233 |
*/ |
|
234 |
LOCALS_AND_OPERANDS |
|
235 |
}; |
|
236 |
||
237 |
static final EnumSet<Option> DEFAULT_EMPTY_OPTION = EnumSet.noneOf(Option.class); |
|
238 |
||
239 |
private final static StackWalker DEFAULT_WALKER = |
|
240 |
new StackWalker(DEFAULT_EMPTY_OPTION); |
|
241 |
||
242 |
private final Set<Option> options; |
|
243 |
private final ExtendedOption extendedOption; |
|
244 |
private final int estimateDepth; |
|
245 |
||
246 |
/** |
|
247 |
* Returns a {@code StackWalker} instance. |
|
248 |
* |
|
249 |
* <p> This {@code StackWalker} is configured to skip all |
|
250 |
* {@linkplain Option#SHOW_HIDDEN_FRAMES hidden frames} and |
|
251 |
* no {@linkplain Option#RETAIN_CLASS_REFERENCE class reference} is retained. |
|
252 |
* |
|
253 |
* @return a {@code StackWalker} configured to skip all |
|
254 |
* {@linkplain Option#SHOW_HIDDEN_FRAMES hidden frames} and |
|
255 |
* no {@linkplain Option#RETAIN_CLASS_REFERENCE class reference} is retained. |
|
256 |
* |
|
257 |
*/ |
|
258 |
public static StackWalker getInstance() { |
|
259 |
// no permission check needed |
|
260 |
return DEFAULT_WALKER; |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Returns a {@code StackWalker} instance with the given option specifying |
|
265 |
* the stack frame information it can access. |
|
266 |
* |
|
267 |
* <p> |
|
268 |
* If a security manager is present and the given {@code option} is |
|
269 |
* {@link Option#RETAIN_CLASS_REFERENCE Option.RETAIN_CLASS_REFERENCE}, |
|
270 |
* it calls its {@link SecurityManager#checkPermission checkPermission} |
|
271 |
* method for {@code StackFramePermission("retainClassReference")}. |
|
272 |
* |
|
273 |
* @param option {@link Option stack walking option} |
|
274 |
* |
|
275 |
* @return a {@code StackWalker} configured with the given option |
|
276 |
* |
|
277 |
* @throws SecurityException if a security manager exists and its |
|
278 |
* {@code checkPermission} method denies access. |
|
279 |
*/ |
|
280 |
public static StackWalker getInstance(Option option) { |
|
281 |
return getInstance(EnumSet.of(Objects.requireNonNull(option))); |
|
282 |
} |
|
283 |
||
284 |
/** |
|
285 |
* Returns a {@code StackWalker} instance with the given {@code options} specifying |
|
286 |
* the stack frame information it can access. If the given {@code options} |
|
287 |
* is empty, this {@code StackWalker} is configured to skip all |
|
288 |
* {@linkplain Option#SHOW_HIDDEN_FRAMES hidden frames} and no |
|
289 |
* {@linkplain Option#RETAIN_CLASS_REFERENCE class reference} is retained. |
|
290 |
* |
|
291 |
* <p> |
|
292 |
* If a security manager is present and the given {@code options} contains |
|
293 |
* {@link Option#RETAIN_CLASS_REFERENCE Option.RETAIN_CLASS_REFERENCE}, |
|
294 |
* it calls its {@link SecurityManager#checkPermission checkPermission} |
|
295 |
* method for {@code StackFramePermission("retainClassReference")}. |
|
296 |
* |
|
297 |
* @param options {@link Option stack walking option} |
|
298 |
* |
|
299 |
* @return a {@code StackWalker} configured with the given options |
|
300 |
* |
|
301 |
* @throws SecurityException if a security manager exists and its |
|
302 |
* {@code checkPermission} method denies access. |
|
303 |
*/ |
|
304 |
public static StackWalker getInstance(Set<Option> options) { |
|
305 |
if (options.isEmpty()) { |
|
306 |
return DEFAULT_WALKER; |
|
307 |
} |
|
308 |
||
38569 | 309 |
EnumSet<Option> optionSet = toEnumSet(options); |
310 |
checkPermission(optionSet); |
|
311 |
return new StackWalker(optionSet); |
|
34362 | 312 |
} |
313 |
||
314 |
/** |
|
34697 | 315 |
* Returns a {@code StackWalker} instance with the given {@code options} specifying |
316 |
* the stack frame information it can access. If the given {@code options} |
|
34362 | 317 |
* is empty, this {@code StackWalker} is configured to skip all |
318 |
* {@linkplain Option#SHOW_HIDDEN_FRAMES hidden frames} and no |
|
319 |
* {@linkplain Option#RETAIN_CLASS_REFERENCE class reference} is retained. |
|
320 |
* |
|
321 |
* <p> |
|
322 |
* If a security manager is present and the given {@code options} contains |
|
323 |
* {@link Option#RETAIN_CLASS_REFERENCE Option.RETAIN_CLASS_REFERENCE}, |
|
324 |
* it calls its {@link SecurityManager#checkPermission checkPermission} |
|
325 |
* method for {@code StackFramePermission("retainClassReference")}. |
|
326 |
* |
|
327 |
* <p> |
|
328 |
* The {@code estimateDepth} specifies the estimate number of stack frames |
|
329 |
* this {@code StackWalker} will traverse that the {@code StackWalker} could |
|
330 |
* use as a hint for the buffer size. |
|
331 |
* |
|
332 |
* @param options {@link Option stack walking options} |
|
333 |
* @param estimateDepth Estimate number of stack frames to be traversed. |
|
334 |
* |
|
335 |
* @return a {@code StackWalker} configured with the given options |
|
336 |
* |
|
337 |
* @throws IllegalArgumentException if {@code estimateDepth <= 0} |
|
338 |
* @throws SecurityException if a security manager exists and its |
|
339 |
* {@code checkPermission} method denies access. |
|
340 |
*/ |
|
341 |
public static StackWalker getInstance(Set<Option> options, int estimateDepth) { |
|
342 |
if (estimateDepth <= 0) { |
|
343 |
throw new IllegalArgumentException("estimateDepth must be > 0"); |
|
344 |
} |
|
38569 | 345 |
EnumSet<Option> optionSet = toEnumSet(options); |
346 |
checkPermission(optionSet); |
|
347 |
return new StackWalker(optionSet, estimateDepth); |
|
34362 | 348 |
} |
349 |
||
350 |
// ----- private constructors ------ |
|
351 |
private StackWalker(EnumSet<Option> options) { |
|
352 |
this(options, 0, null); |
|
353 |
} |
|
354 |
private StackWalker(EnumSet<Option> options, int estimateDepth) { |
|
355 |
this(options, estimateDepth, null); |
|
356 |
} |
|
357 |
private StackWalker(EnumSet<Option> options, int estimateDepth, ExtendedOption extendedOption) { |
|
358 |
this.options = options; |
|
359 |
this.estimateDepth = estimateDepth; |
|
360 |
this.extendedOption = extendedOption; |
|
361 |
} |
|
362 |
||
363 |
private static void checkPermission(Set<Option> options) { |
|
364 |
Objects.requireNonNull(options); |
|
365 |
SecurityManager sm = System.getSecurityManager(); |
|
366 |
if (sm != null) { |
|
367 |
if (options.contains(Option.RETAIN_CLASS_REFERENCE)) { |
|
368 |
sm.checkPermission(new StackFramePermission("retainClassReference")); |
|
369 |
} |
|
370 |
} |
|
371 |
} |
|
372 |
||
373 |
/* |
|
374 |
* Returns a defensive copy |
|
375 |
*/ |
|
376 |
private static EnumSet<Option> toEnumSet(Set<Option> options) { |
|
377 |
Objects.requireNonNull(options); |
|
378 |
if (options.isEmpty()) { |
|
379 |
return DEFAULT_EMPTY_OPTION; |
|
380 |
} else { |
|
381 |
return EnumSet.copyOf(options); |
|
382 |
} |
|
383 |
} |
|
384 |
||
385 |
/** |
|
386 |
* Applies the given function to the stream of {@code StackFrame}s |
|
387 |
* for the current thread, traversing from the top frame of the stack, |
|
388 |
* which is the method calling this {@code walk} method. |
|
389 |
* |
|
390 |
* <p>The {@code StackFrame} stream will be closed when |
|
391 |
* this method returns. When a closed {@code Stream<StackFrame>} object |
|
392 |
* is reused, {@code IllegalStateException} will be thrown. |
|
393 |
* |
|
394 |
* @apiNote |
|
395 |
* For example, to find the first 10 calling frames, first skipping those frames |
|
396 |
* whose declaring class is in package {@code com.foo}: |
|
397 |
* <blockquote> |
|
398 |
* <pre>{@code |
|
399 |
* List<StackFrame> frames = StackWalker.getInstance().walk(s -> |
|
400 |
* s.dropWhile(f -> f.getClassName().startsWith("com.foo.")) |
|
401 |
* .limit(10) |
|
402 |
* .collect(Collectors.toList())); |
|
403 |
* }</pre></blockquote> |
|
404 |
* |
|
405 |
* <p>This method takes a {@code Function} accepting a {@code Stream<StackFrame>}, |
|
406 |
* rather than returning a {@code Stream<StackFrame>} and allowing the |
|
407 |
* caller to directly manipulate the stream. The Java virtual machine is |
|
408 |
* free to reorganize a thread's control stack, for example, via |
|
409 |
* deoptimization. By taking a {@code Function} parameter, this method |
|
410 |
* allows access to stack frames through a stable view of a thread's control |
|
411 |
* stack. |
|
412 |
* |
|
413 |
* <p>Parallel execution is effectively disabled and stream pipeline |
|
414 |
* execution will only occur on the current thread. |
|
415 |
* |
|
416 |
* @implNote The implementation stabilizes the stack by anchoring a frame |
|
417 |
* specific to the stack walking and ensures that the stack walking is |
|
418 |
* performed above the anchored frame. When the stream object is closed or |
|
419 |
* being reused, {@code IllegalStateException} will be thrown. |
|
420 |
* |
|
421 |
* @param function a function that takes a stream of |
|
422 |
* {@linkplain StackFrame stack frames} and returns a result. |
|
423 |
* @param <T> The type of the result of applying the function to the |
|
424 |
* stream of {@linkplain StackFrame stack frame}. |
|
425 |
* |
|
426 |
* @return the result of applying the function to the stream of |
|
427 |
* {@linkplain StackFrame stack frame}. |
|
428 |
*/ |
|
429 |
@CallerSensitive |
|
430 |
public <T> T walk(Function<? super Stream<StackFrame>, ? extends T> function) { |
|
431 |
// Returning a Stream<StackFrame> would be unsafe, as the stream could |
|
432 |
// be used to access the stack frames in an uncontrolled manner. For |
|
433 |
// example, a caller might pass a Spliterator of stack frames after one |
|
434 |
// or more frames had been traversed. There is no robust way to detect |
|
435 |
// whether the execution point when |
|
436 |
// Spliterator.tryAdvance(java.util.function.Consumer<? super T>) is |
|
437 |
// invoked is the exact same execution point where the stack frame |
|
438 |
// traversal is expected to resume. |
|
439 |
||
440 |
Objects.requireNonNull(function); |
|
441 |
return StackStreamFactory.makeStackTraverser(this, function) |
|
442 |
.walk(); |
|
443 |
} |
|
444 |
||
445 |
/** |
|
446 |
* Performs the given action on each element of {@code StackFrame} stream |
|
447 |
* of the current thread, traversing from the top frame of the stack, |
|
448 |
* which is the method calling this {@code forEach} method. |
|
449 |
* |
|
450 |
* <p> This method is equivalent to calling |
|
451 |
* <blockquote> |
|
452 |
* {@code walk(s -> { s.forEach(action); return null; });} |
|
453 |
* </blockquote> |
|
454 |
* |
|
455 |
* @param action an action to be performed on each {@code StackFrame} |
|
456 |
* of the stack of the current thread |
|
457 |
*/ |
|
458 |
@CallerSensitive |
|
459 |
public void forEach(Consumer<? super StackFrame> action) { |
|
460 |
Objects.requireNonNull(action); |
|
461 |
StackStreamFactory.makeStackTraverser(this, s -> { |
|
462 |
s.forEach(action); |
|
463 |
return null; |
|
464 |
}).walk(); |
|
465 |
} |
|
466 |
||
467 |
/** |
|
468 |
* Gets the {@code Class} object of the caller invoking the method |
|
469 |
* that calls this {@code getCallerClass} method. |
|
470 |
* |
|
38784
c0a88deb692a
8152893: StackWalker#getCallerClass is not filtering hidden/ reflection frames when walker is configured to show hidden /reflection frames
bchristi
parents:
38569
diff
changeset
|
471 |
* <p> Reflection frames, {@link java.lang.invoke.MethodHandle}, and |
34362 | 472 |
* hidden frames are filtered regardless of the |
473 |
* {@link Option#SHOW_REFLECT_FRAMES SHOW_REFLECT_FRAMES} |
|
474 |
* and {@link Option#SHOW_HIDDEN_FRAMES SHOW_HIDDEN_FRAMES} options |
|
38784
c0a88deb692a
8152893: StackWalker#getCallerClass is not filtering hidden/ reflection frames when walker is configured to show hidden /reflection frames
bchristi
parents:
38569
diff
changeset
|
475 |
* this {@code StackWalker} has been configured with. |
34362 | 476 |
* |
477 |
* <p> This method throws {@code UnsupportedOperationException} |
|
38784
c0a88deb692a
8152893: StackWalker#getCallerClass is not filtering hidden/ reflection frames when walker is configured to show hidden /reflection frames
bchristi
parents:
38569
diff
changeset
|
478 |
* if this {@code StackWalker} is not configured with the |
c0a88deb692a
8152893: StackWalker#getCallerClass is not filtering hidden/ reflection frames when walker is configured to show hidden /reflection frames
bchristi
parents:
38569
diff
changeset
|
479 |
* {@link Option#RETAIN_CLASS_REFERENCE RETAIN_CLASS_REFERENCE} option. |
34362 | 480 |
* This method should be called when a caller frame is present. If |
38784
c0a88deb692a
8152893: StackWalker#getCallerClass is not filtering hidden/ reflection frames when walker is configured to show hidden /reflection frames
bchristi
parents:
38569
diff
changeset
|
481 |
* it is called from the last frame on the stack, |
34362 | 482 |
* {@code IllegalStateException} will be thrown. |
483 |
* |
|
484 |
* @apiNote |
|
485 |
* For example, {@code Util::getResourceBundle} loads a resource bundle |
|
486 |
* on behalf of the caller. It calls this {@code getCallerClass} method |
|
38784
c0a88deb692a
8152893: StackWalker#getCallerClass is not filtering hidden/ reflection frames when walker is configured to show hidden /reflection frames
bchristi
parents:
38569
diff
changeset
|
487 |
* to find the method calling {@code Util::getResourceBundle} and uses the caller's |
34362 | 488 |
* class loader to load the resource bundle. The caller class in this example |
489 |
* is the {@code MyTool} class. |
|
490 |
* |
|
491 |
* <pre>{@code |
|
492 |
* class Util { |
|
493 |
* private final StackWalker walker = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE); |
|
494 |
* public ResourceBundle getResourceBundle(String bundleName) { |
|
495 |
* Class<?> caller = walker.getCallerClass(); |
|
496 |
* return ResourceBundle.getBundle(bundleName, Locale.getDefault(), caller.getClassLoader()); |
|
497 |
* } |
|
498 |
* } |
|
499 |
* |
|
500 |
* class MyTool { |
|
501 |
* private final Util util = new Util(); |
|
502 |
* private void init() { |
|
503 |
* ResourceBundle rb = util.getResourceBundle("mybundle"); |
|
504 |
* } |
|
505 |
* } |
|
506 |
* }</pre> |
|
507 |
* |
|
508 |
* An equivalent way to find the caller class using the |
|
509 |
* {@link StackWalker#walk walk} method is as follows |
|
510 |
* (filtering the reflection frames, {@code MethodHandle} and hidden frames |
|
511 |
* not shown below): |
|
512 |
* <pre>{@code |
|
513 |
* Optional<Class<?>> caller = walker.walk(s -> |
|
514 |
* s.map(StackFrame::getDeclaringClass) |
|
515 |
* .skip(2) |
|
516 |
* .findFirst()); |
|
517 |
* }</pre> |
|
518 |
* |
|
519 |
* When the {@code getCallerClass} method is called from a method that |
|
520 |
* is the last frame on the stack, |
|
521 |
* for example, {@code static public void main} method launched by the |
|
38784
c0a88deb692a
8152893: StackWalker#getCallerClass is not filtering hidden/ reflection frames when walker is configured to show hidden /reflection frames
bchristi
parents:
38569
diff
changeset
|
522 |
* {@code java} launcher, or a method invoked from a JNI attached thread, |
34362 | 523 |
* {@code IllegalStateException} is thrown. |
524 |
* |
|
525 |
* @return {@code Class} object of the caller's caller invoking this method. |
|
526 |
* |
|
527 |
* @throws UnsupportedOperationException if this {@code StackWalker} |
|
528 |
* is not configured with {@link Option#RETAIN_CLASS_REFERENCE |
|
529 |
* Option.RETAIN_CLASS_REFERENCE}. |
|
530 |
* @throws IllegalStateException if there is no caller frame, i.e. |
|
531 |
* when this {@code getCallerClass} method is called from a method |
|
532 |
* which is the last frame on the stack. |
|
533 |
*/ |
|
534 |
@CallerSensitive |
|
535 |
public Class<?> getCallerClass() { |
|
536 |
if (!options.contains(Option.RETAIN_CLASS_REFERENCE)) { |
|
537 |
throw new UnsupportedOperationException("This stack walker " + |
|
538 |
"does not have RETAIN_CLASS_REFERENCE access"); |
|
539 |
} |
|
540 |
||
541 |
return StackStreamFactory.makeCallerFinder(this).findCaller(); |
|
542 |
} |
|
543 |
||
544 |
// ---- package access ---- |
|
545 |
||
546 |
static StackWalker newInstance(Set<Option> options, ExtendedOption extendedOption) { |
|
38569 | 547 |
EnumSet<Option> optionSet = toEnumSet(options); |
548 |
checkPermission(optionSet); |
|
549 |
return new StackWalker(optionSet, 0, extendedOption); |
|
34362 | 550 |
} |
551 |
||
552 |
int estimateDepth() { |
|
553 |
return estimateDepth; |
|
554 |
} |
|
555 |
||
556 |
boolean hasOption(Option option) { |
|
557 |
return options.contains(option); |
|
558 |
} |
|
559 |
||
560 |
boolean hasLocalsOperandsOption() { |
|
561 |
return extendedOption == ExtendedOption.LOCALS_AND_OPERANDS; |
|
562 |
} |
|
563 |
||
564 |
void ensureAccessEnabled(Option access) { |
|
565 |
if (!hasOption(access)) { |
|
566 |
throw new UnsupportedOperationException("No access to " + access + |
|
567 |
": " + options.toString()); |
|
568 |
} |
|
569 |
} |
|
570 |
} |