001/* MonitorInfo.java - Information on a monitor lock.
002   Copyright (C) 2006 Free Software Foundation
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038package java.lang.management;
039
040import javax.management.openmbean.CompositeData;
041import javax.management.openmbean.CompositeType;
042import javax.management.openmbean.SimpleType;
043
044/**
045 * Provides information on a monitor lock held by a thread.
046 * A monitor lock is obtained when a thread enters a synchronized
047 * block or method.
048 *
049 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
050 * @since 1.6
051 */
052public class MonitorInfo
053  extends LockInfo
054{
055
056  /**
057   * The stack depth at which the lock was obtained.
058   */
059  private int stackDepth;
060
061  /**
062   * The stack frame at which the lock was obtained.
063   */
064  private StackTraceElement stackFrame;
065
066  /**
067   * Constructs a new {@link MonitorInfo} using the specified
068   * lock class name and identity hash code, and the given
069   * stack depth and frame.
070   *
071   * @param className the class name of the lock object.
072   * @param identityHashCode the identity hash code of the lock object.
073   * @param stackDepth the depth of the stack at which the lock
074   *                   was obtained.
075   * @param stackFrame the frame of the stack at which the lock was
076   *                   obtained.
077   * @throws IllegalArgumentException if the stack depth and frame are
078   *                                  inconsistent i.e. the frame is
079   *                                  <code>null</code> but the depth is
080   *                                  &ge; 0, or the frame is not
081   *                                  <code>null</code> but the depth is
082   *                                  &lt; 0.
083   */
084  public MonitorInfo(String className, int identityHashCode, int stackDepth,
085                     StackTraceElement stackFrame)
086  {
087    super(className, identityHashCode);
088    if (stackFrame == null && stackDepth >= 0)
089      throw new IllegalArgumentException("The stack frame is null, but the " +
090                                         "stack depth is greater than or equal " +
091                                         "to zero.");
092    if (stackFrame != null && stackDepth < 0)
093      throw new IllegalArgumentException("The stack frame is not null, but the " +
094                                         "stack depth is less than zero.");
095    this.stackDepth = stackDepth;
096    this.stackFrame = stackFrame;
097  }
098
099  /**
100   * <p>
101   * Returns a {@link MonitorInfo} instance using the values
102   * given in the supplied
103   * {@link javax.management.openmbean.CompositeData} object.
104   * The composite data instance should contain the following
105   * attributes with the specified types:
106   * </p>
107   * <table>
108   * <th><td>Name</td><td>Type</td></th>
109   * <tr><td>className</td><td>java.lang.String</td></tr>
110   * <tr><td>identityHashCode</td><td>java.lang.Integer</td></tr>
111   * <tr><td>lockedStackDepth</td><td>java.lang.Integer</td></tr>
112   * <tr><td>lockedStackFrame</td><td>javax.management.openmbean.CompositeData
113   * </td></tr>
114   * </table>
115   * <p>
116   * The stack trace is further described as:
117   * </p>
118   * <table>
119   * <th><td>Name</td><td>Type</td></th>
120   * <tr><td>className</td><td>java.lang.String</td></tr>
121   * <tr><td>methodName</td><td>java.lang.String</td></tr>
122   * <tr><td>fileName</td><td>java.lang.String</td></tr>
123   * <tr><td>lineNumber</td><td>java.lang.Integer</td></tr>
124   * <tr><td>nativeMethod</td><td>java.lang.Boolean</td></tr>
125   * </table>
126   *
127   * @param data the composite data structure to take values from.
128   * @return a new instance containing the values from the
129   *         composite data structure, or <code>null</code>
130   *         if the data structure was also <code>null</code>.
131   * @throws IllegalArgumentException if the composite data structure
132   *                                  does not match the structure
133   *                                  outlined above.
134   */
135  public static MonitorInfo from(CompositeData data)
136  {
137    if (data == null)
138      return null;
139    CompositeType type = data.getCompositeType();
140    ThreadInfo.checkAttribute(type, "ClassName", SimpleType.STRING);
141    ThreadInfo.checkAttribute(type, "IdentityHashCode", SimpleType.INTEGER);
142    ThreadInfo.checkAttribute(type, "LockedStackDepth", SimpleType.INTEGER);
143    ThreadInfo.checkAttribute(type, "LockedStackFrame",
144                              ThreadInfo.getStackTraceType());
145    CompositeData frame = (CompositeData) data.get("LockedStackFrame");
146    return new MonitorInfo((String) data.get("ClassName"),
147                           (Integer) data.get("IdentityHashCode"),
148                           (Integer) data.get("LockedStackDepth"),
149                           new StackTraceElement((String) frame.get("ClassName"),
150                                                 (String) frame.get("MethodName"),
151                                                 (String) frame.get("FileName"),
152                                                 (Integer) frame.get("LineNumber")));
153  }
154
155  /**
156   * Returns the depth of the stack at which the lock was obtained.
157   * This works as an index into the array returned by
158   * {@link ThreadInfo#getStackTrace()}.
159   *
160   * @return the depth of the stack at which the lock was obtained,
161   *         or a negative number if this information is unavailable.
162   */
163  public int getLockedStackDepth()
164  {
165    return stackDepth;
166  }
167
168  /**
169   * Returns the stack frame at which the lock was obtained.
170   *
171   * @return the stack frame at which the lock was obtained,
172   *         or <code>null</code> if this informati0on is unavailable.
173   */
174  public StackTraceElement getLockedStackFrame()
175  {
176    return stackFrame;
177  }
178
179}