View Javadoc
1   /*
2    * Copyright (c) 2005, 2013, 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.  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  
26  package java.lang.management;
27  
28  import javax.management.openmbean.CompositeData;
29  import sun.management.MonitorInfoCompositeData;
30  
31  /**
32   * Information about an object monitor lock.  An object monitor is locked
33   * when entering a synchronization block or method on that object.
34   *
35   * <h3>MXBean Mapping</h3>
36   * <tt>MonitorInfo</tt> is mapped to a {@link CompositeData CompositeData}
37   * with attributes as specified in
38   * the {@link #from from} method.
39   *
40   * @author  Mandy Chung
41   * @since   1.6
42   */
43  public class MonitorInfo extends LockInfo {
44  
45      private int    stackDepth;
46      private StackTraceElement stackFrame;
47  
48      /**
49       * Construct a <tt>MonitorInfo</tt> object.
50       *
51       * @param className the fully qualified name of the class of the lock object.
52       * @param identityHashCode the {@link System#identityHashCode
53       *                         identity hash code} of the lock object.
54       * @param stackDepth the depth in the stack trace where the object monitor
55       *                   was locked.
56       * @param stackFrame the stack frame that locked the object monitor.
57       * @throws IllegalArgumentException if
58       *    <tt>stackDepth</tt> &ge; 0 but <tt>stackFrame</tt> is <tt>null</tt>,
59       *    or <tt>stackDepth</tt> &lt; 0 but <tt>stackFrame</tt> is not
60       *       <tt>null</tt>.
61       */
62      public MonitorInfo(String className,
63                         int identityHashCode,
64                         int stackDepth,
65                         StackTraceElement stackFrame) {
66          super(className, identityHashCode);
67          if (stackDepth >= 0 && stackFrame == null) {
68              throw new IllegalArgumentException("Parameter stackDepth is " +
69                  stackDepth + " but stackFrame is null");
70          }
71          if (stackDepth < 0 && stackFrame != null) {
72              throw new IllegalArgumentException("Parameter stackDepth is " +
73                  stackDepth + " but stackFrame is not null");
74          }
75          this.stackDepth = stackDepth;
76          this.stackFrame = stackFrame;
77      }
78  
79      /**
80       * Returns the depth in the stack trace where the object monitor
81       * was locked.  The depth is the index to the <tt>StackTraceElement</tt>
82       * array returned in the {@link ThreadInfo#getStackTrace} method.
83       *
84       * @return the depth in the stack trace where the object monitor
85       *         was locked, or a negative number if not available.
86       */
87      public int getLockedStackDepth() {
88          return stackDepth;
89      }
90  
91      /**
92       * Returns the stack frame that locked the object monitor.
93       *
94       * @return <tt>StackTraceElement</tt> that locked the object monitor,
95       *         or <tt>null</tt> if not available.
96       */
97      public StackTraceElement getLockedStackFrame() {
98          return stackFrame;
99      }
100 
101     /**
102      * Returns a <tt>MonitorInfo</tt> object represented by the
103      * given <tt>CompositeData</tt>.
104      * The given <tt>CompositeData</tt> must contain the following attributes
105      * as well as the attributes specified in the
106      * <a href="LockInfo.html#MappedType">
107      * mapped type</a> for the {@link LockInfo} class:
108      * <blockquote>
109      * <table border summary="The attributes and their types the given CompositeData contains">
110      * <tr>
111      *   <th align=left>Attribute Name</th>
112      *   <th align=left>Type</th>
113      * </tr>
114      * <tr>
115      *   <td>lockedStackFrame</td>
116      *   <td><tt>CompositeData as specified in the
117      *       <a href="ThreadInfo.html#StackTrace">stackTrace</a>
118      *       attribute defined in the {@link ThreadInfo#from
119      *       ThreadInfo.from} method.
120      *       </tt></td>
121      * </tr>
122      * <tr>
123      *   <td>lockedStackDepth</td>
124      *   <td><tt>java.lang.Integer</tt></td>
125      * </tr>
126      * </table>
127      * </blockquote>
128      *
129      * @param cd <tt>CompositeData</tt> representing a <tt>MonitorInfo</tt>
130      *
131      * @throws IllegalArgumentException if <tt>cd</tt> does not
132      *   represent a <tt>MonitorInfo</tt> with the attributes described
133      *   above.
134 
135      * @return a <tt>MonitorInfo</tt> object represented
136      *         by <tt>cd</tt> if <tt>cd</tt> is not <tt>null</tt>;
137      *         <tt>null</tt> otherwise.
138      */
139     public static MonitorInfo from(CompositeData cd) {
140         if (cd == null) {
141             return null;
142         }
143 
144         if (cd instanceof MonitorInfoCompositeData) {
145             return ((MonitorInfoCompositeData) cd).getMonitorInfo();
146         } else {
147             MonitorInfoCompositeData.validateCompositeData(cd);
148             String className = MonitorInfoCompositeData.getClassName(cd);
149             int identityHashCode = MonitorInfoCompositeData.getIdentityHashCode(cd);
150             int stackDepth = MonitorInfoCompositeData.getLockedStackDepth(cd);
151             StackTraceElement stackFrame = MonitorInfoCompositeData.getLockedStackFrame(cd);
152             return new MonitorInfo(className,
153                                    identityHashCode,
154                                    stackDepth,
155                                    stackFrame);
156         }
157     }
158 
159 }