Path: blob/master/src/java.management/share/classes/sun/management/StackTraceElementCompositeData.java
41152 views
/*1* Copyright (c) 2005, 2018, 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 sun.management;2627import javax.management.openmbean.CompositeType;28import javax.management.openmbean.CompositeData;29import javax.management.openmbean.CompositeDataSupport;30import javax.management.openmbean.OpenDataException;31import javax.management.openmbean.OpenType;32import java.util.Arrays;33import java.util.HashMap;34import java.util.Map;35import java.util.stream.Stream;3637/**38* A CompositeData for StackTraceElement for the local management support.39* This class avoids the performance penalty paid to the40* construction of a CompositeData use in the local case.41*/42public class StackTraceElementCompositeData extends LazyCompositeData {43private final StackTraceElement ste;4445private StackTraceElementCompositeData(StackTraceElement ste) {46this.ste = ste;47}4849public StackTraceElement getStackTraceElement() {50return ste;51}5253public static StackTraceElement from(CompositeData cd) {54validateCompositeData(cd);5556if (STACK_TRACE_ELEMENT_COMPOSITE_TYPE.equals(cd.getCompositeType())) {57return new StackTraceElement(getString(cd, CLASS_LOADER_NAME),58getString(cd, MODULE_NAME),59getString(cd, MODULE_VERSION),60getString(cd, CLASS_NAME),61getString(cd, METHOD_NAME),62getString(cd, FILE_NAME),63getInt(cd, LINE_NUMBER));64} else {65return new StackTraceElement(getString(cd, CLASS_NAME),66getString(cd, METHOD_NAME),67getString(cd, FILE_NAME),68getInt(cd, LINE_NUMBER));6970}71}7273public static CompositeData toCompositeData(StackTraceElement ste) {74StackTraceElementCompositeData cd = new StackTraceElementCompositeData(ste);75return cd.getCompositeData();76}7778protected CompositeData getCompositeData() {79// values may be null; so can't use Map.of80Map<String,Object> items = new HashMap<>();81items.put(CLASS_LOADER_NAME, ste.getClassLoaderName());82items.put(MODULE_NAME, ste.getModuleName());83items.put(MODULE_VERSION, ste.getModuleVersion());84items.put(CLASS_NAME, ste.getClassName());85items.put(METHOD_NAME, ste.getMethodName());86items.put(FILE_NAME, ste.getFileName());87items.put(LINE_NUMBER, ste.getLineNumber());88items.put(NATIVE_METHOD, ste.isNativeMethod());8990try {91return new CompositeDataSupport(STACK_TRACE_ELEMENT_COMPOSITE_TYPE, items);92} catch (OpenDataException e) {93// Should never reach here94throw new AssertionError(e);95}96}9798// Attribute names99private static final String CLASS_LOADER_NAME = "classLoaderName";100private static final String MODULE_NAME = "moduleName";101private static final String MODULE_VERSION = "moduleVersion";102private static final String CLASS_NAME = "className";103private static final String METHOD_NAME = "methodName";104private static final String FILE_NAME = "fileName";105private static final String LINE_NUMBER = "lineNumber";106private static final String NATIVE_METHOD = "nativeMethod";107108private static final String[] V5_ATTRIBUTES = {109CLASS_NAME,110METHOD_NAME,111FILE_NAME,112LINE_NUMBER,113NATIVE_METHOD,114};115116private static final String[] V9_ATTRIBUTES = {117CLASS_LOADER_NAME,118MODULE_NAME,119MODULE_VERSION,120};121122private static final CompositeType STACK_TRACE_ELEMENT_COMPOSITE_TYPE;123private static final CompositeType V5_COMPOSITE_TYPE;124static {125try {126STACK_TRACE_ELEMENT_COMPOSITE_TYPE = (CompositeType)127MappedMXBeanType.toOpenType(StackTraceElement.class);128129OpenType<?>[] types = new OpenType<?>[V5_ATTRIBUTES.length];130for (int i=0; i < V5_ATTRIBUTES.length; i++) {131String name = V5_ATTRIBUTES[i];132types[i] = STACK_TRACE_ELEMENT_COMPOSITE_TYPE.getType(name);133}134V5_COMPOSITE_TYPE = new CompositeType("StackTraceElement",135"JDK 5 StackTraceElement",136V5_ATTRIBUTES,137V5_ATTRIBUTES,138types);139} catch (OpenDataException e) {140// Should never reach here141throw new AssertionError(e);142}143}144145static CompositeType v5CompositeType() {146return V5_COMPOSITE_TYPE;147}148149/**150* Validate if the input CompositeData has the expected151* CompositeType (i.e. contain all attributes with expected152* names and types).153*/154public static void validateCompositeData(CompositeData cd) {155if (cd == null) {156throw new NullPointerException("Null CompositeData");157}158159CompositeType ct = cd.getCompositeType();160if (!isTypeMatched(STACK_TRACE_ELEMENT_COMPOSITE_TYPE, ct) &&161!isTypeMatched(V5_COMPOSITE_TYPE, ct)) {162throw new IllegalArgumentException(163"Unexpected composite type for StackTraceElement");164}165}166private static final long serialVersionUID = -2704607706598396827L;167}168169170