Path: blob/master/src/java.management/share/classes/java/lang/management/MonitorInfo.java
41159 views
/*1* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.lang.management;2627import javax.management.openmbean.CompositeData;28import sun.management.MonitorInfoCompositeData;2930/**31* Information about an object monitor lock. An object monitor is locked32* when entering a synchronization block or method on that object.33*34* <h2>MXBean Mapping</h2>35* {@code MonitorInfo} is mapped to a {@link CompositeData CompositeData}36* with attributes as specified in37* the {@link #from from} method.38*39* @author Mandy Chung40* @since 1.641*/42public class MonitorInfo extends LockInfo {4344private int stackDepth;45private StackTraceElement stackFrame;4647/**48* Construct a {@code MonitorInfo} object.49*50* @param className the fully qualified name of the class of the lock object.51* @param identityHashCode the {@link System#identityHashCode52* identity hash code} of the lock object.53* @param stackDepth the depth in the stack trace where the object monitor54* was locked.55* @param stackFrame the stack frame that locked the object monitor.56* @throws IllegalArgumentException if57* {@code stackDepth} ≥ 0 but {@code stackFrame} is {@code null},58* or {@code stackDepth} < 0 but {@code stackFrame} is not59* {@code null}.60*/61public MonitorInfo(String className,62int identityHashCode,63int stackDepth,64StackTraceElement stackFrame) {65super(className, identityHashCode);66if (stackDepth >= 0 && stackFrame == null) {67throw new IllegalArgumentException("Parameter stackDepth is " +68stackDepth + " but stackFrame is null");69}70if (stackDepth < 0 && stackFrame != null) {71throw new IllegalArgumentException("Parameter stackDepth is " +72stackDepth + " but stackFrame is not null");73}74this.stackDepth = stackDepth;75this.stackFrame = stackFrame;76}7778/**79* Returns the depth in the stack trace where the object monitor80* was locked. The depth is the index to the {@code StackTraceElement}81* array returned in the {@link ThreadInfo#getStackTrace} method.82*83* @return the depth in the stack trace where the object monitor84* was locked, or a negative number if not available.85*/86public int getLockedStackDepth() {87return stackDepth;88}8990/**91* Returns the stack frame that locked the object monitor.92*93* @return {@code StackTraceElement} that locked the object monitor,94* or {@code null} if not available.95*/96public StackTraceElement getLockedStackFrame() {97return stackFrame;98}99100/**101* Returns a {@code MonitorInfo} object represented by the102* given {@code CompositeData}.103* The given {@code CompositeData} must contain the following attributes104* as well as the attributes specified in the105* <a href="LockInfo.html#MappedType">106* mapped type</a> for the {@link LockInfo} class:107* <table class="striped" style="margin-left:2em">108* <caption style="display:none">The attributes and their types the given CompositeData contains</caption>109* <thead>110* <tr>111* <th scope="col">Attribute Name</th>112* <th scope="col">Type</th>113* </tr>114* </thead>115* <tbody style="text-align:left">116* <tr>117* <th scope="row">lockedStackFrame</th>118* <td><a href="ThreadInfo.html#stackTraceElement">119* {@code CompositeData} for {@code StackTraceElement}</a> as specified120* in {@link ThreadInfo#from(CompositeData)} method.121* </td>122* </tr>123* <tr>124* <th scope="row">lockedStackDepth</th>125* <td>{@code java.lang.Integer}</td>126* </tr>127* </tbody>128* </table>129*130* @param cd {@code CompositeData} representing a {@code MonitorInfo}131*132* @throws IllegalArgumentException if {@code cd} does not133* represent a {@code MonitorInfo} with the attributes described134* above.135*136* @return a {@code MonitorInfo} object represented137* by {@code cd} if {@code cd} is not {@code null};138* {@code null} otherwise.139*/140public static MonitorInfo from(CompositeData cd) {141if (cd == null) {142return null;143}144145if (cd instanceof MonitorInfoCompositeData) {146return ((MonitorInfoCompositeData) cd).getMonitorInfo();147} else {148MonitorInfoCompositeData.validateCompositeData(cd);149String className = MonitorInfoCompositeData.getClassName(cd);150int identityHashCode = MonitorInfoCompositeData.getIdentityHashCode(cd);151int stackDepth = MonitorInfoCompositeData.getLockedStackDepth(cd);152StackTraceElement stackFrame = MonitorInfoCompositeData.getLockedStackFrame(cd);153return new MonitorInfo(className,154identityHashCode,155stackDepth,156stackFrame);157}158}159160}161162163