1 /*
2  * Copyright (c) 2005, 2008, 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 sun.management;
27 
28 import javax.management.openmbean.CompositeType;
29 import javax.management.openmbean.CompositeData;
30 import javax.management.openmbean.CompositeDataSupport;
31 import javax.management.openmbean.OpenDataException;
32 
33 /**
34  * A CompositeData for StackTraceElement for the local management support.
35  * This class avoids the performance penalty paid to the
36  * construction of a CompositeData use in the local case.
37  */
38 public class StackTraceElementCompositeData extends LazyCompositeData {
39     private final StackTraceElement ste;
40 
StackTraceElementCompositeData(StackTraceElement ste)41     private StackTraceElementCompositeData(StackTraceElement ste) {
42         this.ste = ste;
43     }
44 
getStackTraceElement()45     public StackTraceElement getStackTraceElement() {
46         return ste;
47     }
48 
from(CompositeData cd)49     public static StackTraceElement from(CompositeData cd) {
50         validateCompositeData(cd);
51 
52         return new StackTraceElement(getString(cd, CLASS_NAME),
53                                      getString(cd, METHOD_NAME),
54                                      getString(cd, FILE_NAME),
55                                      getInt(cd, LINE_NUMBER));
56     }
57 
toCompositeData(StackTraceElement ste)58     public static CompositeData toCompositeData(StackTraceElement ste) {
59         StackTraceElementCompositeData cd = new StackTraceElementCompositeData(ste);
60         return cd.getCompositeData();
61     }
62 
getCompositeData()63     protected CompositeData getCompositeData() {
64         // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
65         // stackTraceElementItemNames!
66         final Object[] stackTraceElementItemValues = {
67             ste.getClassName(),
68             ste.getMethodName(),
69             ste.getFileName(),
70             new Integer(ste.getLineNumber()),
71             new Boolean(ste.isNativeMethod()),
72         };
73         try {
74             return new CompositeDataSupport(stackTraceElementCompositeType,
75                                             stackTraceElementItemNames,
76                                             stackTraceElementItemValues);
77         } catch (OpenDataException e) {
78             // Should never reach here
79             throw new AssertionError(e);
80         }
81     }
82 
83     private static final CompositeType stackTraceElementCompositeType;
84     static {
85         try {
86             stackTraceElementCompositeType = (CompositeType)
87                 MappedMXBeanType.toOpenType(StackTraceElement.class);
88         } catch (OpenDataException e) {
89             // Should never reach here
90             throw new AssertionError(e);
91         }
92     }
93 
94     // Attribute names
95     private static final String CLASS_NAME      = "className";
96     private static final String METHOD_NAME     = "methodName";
97     private static final String FILE_NAME       = "fileName";
98     private static final String LINE_NUMBER     = "lineNumber";
99     private static final String NATIVE_METHOD   = "nativeMethod";
100 
101     private static final String[] stackTraceElementItemNames = {
102         CLASS_NAME,
103         METHOD_NAME,
104         FILE_NAME,
105         LINE_NUMBER,
106         NATIVE_METHOD,
107     };
108 
109     /** Validate if the input CompositeData has the expected
110      * CompositeType (i.e. contain all attributes with expected
111      * names and types).
112      */
validateCompositeData(CompositeData cd)113     public static void validateCompositeData(CompositeData cd) {
114         if (cd == null) {
115             throw new NullPointerException("Null CompositeData");
116         }
117 
118         if (!isTypeMatched(stackTraceElementCompositeType, cd.getCompositeType())) {
119             throw new IllegalArgumentException(
120                 "Unexpected composite type for StackTraceElement");
121         }
122     }
123 
124     private static final long serialVersionUID = -2704607706598396827L;
125 }
126