src/java.base/share/classes/java/lang/LiveStackFrame.java
author darcy
Mon, 09 Sep 2019 10:13:42 -0700
changeset 58054 ee230ad8cfef
parent 53563 a4b7ea85d668
permissions -rw-r--r--
8230723: Remove default constructors from java.lang and java.io Reviewed-by: bpb, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     1
/*
53563
a4b7ea85d668 8218022: Repeated words typos in java.base
prappo
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     4
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    10
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    15
 * accompanied this code).
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    16
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    20
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    23
 * questions.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    24
 */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    25
package java.lang;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    26
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    27
import java.lang.StackWalker.StackFrame;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    28
import java.util.EnumSet;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    29
import java.util.Set;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    30
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    31
import static java.lang.StackWalker.ExtendedOption.LOCALS_AND_OPERANDS;
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    32
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    33
/**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    34
 * <em>UNSUPPORTED</em> This interface is intended to be package-private
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    35
 * or move to an internal package.<p>
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    36
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    37
 * {@code LiveStackFrame} represents a frame storing data and partial results.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    38
 * Each frame has its own array of local variables (JVMS section 2.6.1),
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    39
 * its own operand stack (JVMS section 2.6.2) for a method invocation.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    40
 *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    41
 * @jvms 2.6 Frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    42
 */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    43
/* package-private */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    44
interface LiveStackFrame extends StackFrame {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    45
    /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    46
     * Return the monitors held by this stack frame. This method returns
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    47
     * an empty array if no monitor is held by this stack frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    48
     *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    49
     * @return the monitors held by this stack frames
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    50
     */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    51
    public Object[] getMonitors();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    52
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    53
    /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    54
     * Gets the local variable array of this stack frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    55
     *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    56
     * <p>A single local variable can hold a value of type boolean, byte, char,
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    57
     * short, int, float, reference or returnAddress.  A pair of local variables
43715
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    58
     * can hold a value of type long or double (JVMS section 2.6.1).  Primitive
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    59
     * locals are represented in the returned array as {@code PrimitiveSlot}s,
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    60
     * with longs and doubles occupying a pair of consecutive
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    61
     * {@code PrimitiveSlot}s.
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    62
     *
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    63
     * <p>The current VM implementation does not provide specific type
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    64
     * information for primitive locals.  This method simply returns the raw
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    65
     * contents of the VM's primitive locals on a best-effort basis, without
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    66
     * indicating a specific type.
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    67
     *
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    68
     * <p>The returned array may contain null entries for local variables that
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    69
     * are not live.
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    70
     *
43715
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    71
     * @implNote
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    72
     * <p> The specific subclass of {@code PrimitiveSlot} will reflect the
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    73
     * underlying architecture, and will be either {@code PrimitiveSlot32} or
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    74
     * {@code PrimitiveSlot64}.
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    75
     *
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    76
     * <p>How a long or double value is stored in the pair of
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    77
     * {@code PrimitiveSlot}s can vary based on the underlying architecture and
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    78
     * VM implementation.  On 32-bit architectures, long/double values are split
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    79
     * between the two {@code PrimitiveSlot32}s.
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    80
     * On 64-bit architectures, the entire value may be stored in one of the
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    81
     * {@code PrimitiveSlot64}s, with the other {@code PrimitiveSlot64} being
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    82
     * unused.
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    83
     *
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    84
     * <p>The contents of the unused, high-order portion of a
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    85
     * {@code PrimitiveSlot64} (when storing a primitive other than a long or
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    86
     * double) is unspecified.  In particular, the unused bits are not
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
    87
     * necessarily zeroed out.
34934
7fbff68dbc12 8144552: java/lang/StackWalker/LocalsAndOperands.java fails with java.lang.NullPointerException
bchristi
parents: 34362
diff changeset
    88
     *
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    89
     * @return  the local variable array of this stack frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    90
     */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    91
    public Object[] getLocals();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    92
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    93
    /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    94
     * Gets the operand stack of this stack frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    95
     *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    96
     * <p>
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    97
     * The 0-th element of the returned array represents the top of the operand stack.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    98
     * This method returns an empty array if the operand stack is empty.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    99
     *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   100
     * <p>Each entry on the operand stack can hold a value of any Java Virtual
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   101
     * Machine Type.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   102
     * For a value of primitive type, the element in the returned array is
43715
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
   103
     * a {@link PrimitiveSlot} object; otherwise, the element is the {@code Object}
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   104
     * on the operand stack.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   105
     *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   106
     * @return the operand stack of this stack frame.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   107
     */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   108
    public Object[] getStack();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   109
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   110
    /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   111
     * <em>UNSUPPORTED</em> This interface is intended to be package-private
43715
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
   112
     * or moved to an internal package.<p>
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   113
     *
43715
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
   114
     * Represents a local variable or an entry on the operand stack whose value is
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   115
     * of primitive type.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   116
     */
43715
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
   117
    public abstract class PrimitiveSlot {
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   118
        /**
58054
ee230ad8cfef 8230723: Remove default constructors from java.lang and java.io
darcy
parents: 53563
diff changeset
   119
         * Constructor.
ee230ad8cfef 8230723: Remove default constructors from java.lang and java.io
darcy
parents: 53563
diff changeset
   120
         */
ee230ad8cfef 8230723: Remove default constructors from java.lang and java.io
darcy
parents: 53563
diff changeset
   121
        PrimitiveSlot() {}
ee230ad8cfef 8230723: Remove default constructors from java.lang and java.io
darcy
parents: 53563
diff changeset
   122
ee230ad8cfef 8230723: Remove default constructors from java.lang and java.io
darcy
parents: 53563
diff changeset
   123
        /**
43715
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
   124
         * Returns the size, in bytes, of the slot.
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   125
         */
43715
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
   126
        public abstract int size();
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   127
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   128
        /**
43715
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
   129
         * Returns the int value if this primitive value is of size 4
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
   130
         * @return the int value if this primitive value is of size 4
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   131
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   132
         * @throws UnsupportedOperationException if this primitive value is not
43715
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
   133
         * of size 4.
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   134
         */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   135
        public int intValue() {
43715
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
   136
            throw new UnsupportedOperationException("this " + size() + "-byte primitive");
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   137
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   138
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   139
        /**
43715
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
   140
         * Returns the long value if this primitive value is of size 8
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
   141
         * @return the long value if this primitive value is of size 8
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   142
         *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   143
         * @throws UnsupportedOperationException if this primitive value is not
43715
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
   144
         * of size 8.
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   145
         */
43715
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
   146
        public long longValue() {
6d72cc84759f 8156073: 2-slot LiveStackFrame locals (long and double) are incorrect
bchristi
parents: 34934
diff changeset
   147
            throw new UnsupportedOperationException("this " + size() + "-byte primitive");
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   148
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   149
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   150
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   151
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   152
    /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   153
     * Gets {@code StackWalker} that can get locals and operands.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   154
     *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   155
     * @throws SecurityException if the security manager is present and
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   156
     * denies access to {@code RuntimePermission("liveStackFrames")}
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   157
     */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   158
    public static StackWalker getStackWalker() {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   159
        return getStackWalker(EnumSet.noneOf(StackWalker.Option.class));
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   160
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   161
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   162
    /**
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   163
     * Gets a {@code StackWalker} instance with the given options specifying
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   164
     * the stack frame information it can access, and which will traverse at most
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   165
     * the given {@code maxDepth} number of stack frames.  If no option is
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   166
     * specified, this {@code StackWalker} obtains the method name and
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   167
     * the class name with all
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   168
     * {@linkplain StackWalker.Option#SHOW_HIDDEN_FRAMES hidden frames} skipped.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   169
     * The returned {@code StackWalker} can get locals and operands.
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   170
     *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   171
     * @param options stack walk {@link StackWalker.Option options}
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   172
     *
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   173
     * @throws SecurityException if the security manager is present and
53563
a4b7ea85d668 8218022: Repeated words typos in java.base
prappo
parents: 47216
diff changeset
   174
     * it denies access to {@code RuntimePermission("liveStackFrames")};
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   175
     * or if the given {@code options} contains
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   176
     * {@link StackWalker.Option#RETAIN_CLASS_REFERENCE Option.RETAIN_CLASS_REFERENCE}
44262
bfbb47bd118d 8176815: Remove StackFramePermission and use RuntimePermission for stack walking
mchung
parents: 43715
diff changeset
   177
     * and it denies access to {@code RuntimePermission("getStackWalkerWithClassReference")}.
34362
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   178
     */
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   179
    public static StackWalker getStackWalker(Set<StackWalker.Option> options) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   180
        SecurityManager sm = System.getSecurityManager();
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   181
        if (sm != null) {
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   182
            sm.checkPermission(new RuntimePermission("liveStackFrames"));
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   183
        }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   184
        return StackWalker.newInstance(options, LOCALS_AND_OPERANDS);
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   185
    }
3396ae214e7d 8140450: Implement JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   186
}