src/java.management/share/classes/sun/management/MappedMXBeanType.java
author lucy
Thu, 23 May 2019 18:50:48 +0200
changeset 55024 948385f851f2
parent 52902 e3398b2e1ab0
permissions -rw-r--r--
8224652: 32-bit build failures after JDK-8213084 Reviewed-by: thartmann, shade Contributed-by: shade@redhat.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
     2
 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4173
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4173
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4173
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4173
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4173
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.lang.management.MemoryUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.management.MemoryNotificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.management.MonitorInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.management.LockInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.management.ThreadInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.InvalidObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.openmbean.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import static javax.management.openmbean.SimpleType.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * A mapped mxbean type maps a Java type to an open type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Only the following Java types are mappable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * (currently required by the platform MXBeans):
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 30355
diff changeset
    48
 * <ol>
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 30355
diff changeset
    49
 *   <li>Primitive types</li>
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 30355
diff changeset
    50
 *   <li>Wrapper classes such java.lang.Integer, etc</li>
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 30355
diff changeset
    51
 *   <li>Classes with only getter methods and with a static "from" method
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 30355
diff changeset
    52
 *      that takes a CompositeData argument.</li>
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 30355
diff changeset
    53
 *   <li>{@code E[]} where {@code E} is a type of 1-4 (can be multi-dimensional array)</li>
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 30355
diff changeset
    54
 *   <li>{@code List<E>} where E is a type of 1-3</li>
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 30355
diff changeset
    55
 *   <li>{@code Map<K, V>} where {@code K} and {@code V} are a type of 1-4</li>
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 30355
diff changeset
    56
 * </ol>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * OpenDataException will be thrown if a Java type is not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
// Suppress unchecked cast warnings at line 442, 523 and 546
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
// Suppress unchecked calls at line 235, 284, 380 and 430.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
@SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public abstract class MappedMXBeanType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final WeakHashMap<Type,MappedMXBeanType> convertedTypes =
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
    65
        new WeakHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    boolean  isBasicType = false;
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
    68
    OpenType<?> openType = inProgress;
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
    69
    Class<?>    mappedTypeClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static synchronized MappedMXBeanType newMappedType(Type javaType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        MappedMXBeanType mt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (javaType instanceof Class) {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
    76
            final Class<?> c = (Class<?>) javaType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if (c.isEnum()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                mt = new EnumMXBeanType(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            } else if (c.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                mt = new ArrayMXBeanType(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                mt = new CompositeDataMXBeanType(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } else if (javaType instanceof ParameterizedType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            final ParameterizedType pt = (ParameterizedType) javaType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            final Type rawType = pt.getRawType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (rawType instanceof Class) {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
    88
                final Class<?> rc = (Class<?>) rawType;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                if (rc == List.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    mt = new ListMXBeanType(pt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                } else if (rc == Map.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    mt = new MapMXBeanType(pt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } else if (javaType instanceof GenericArrayType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
           final GenericArrayType t = (GenericArrayType) javaType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
           mt = new GenericArrayMXBeanType(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // No open type mapped for the javaType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (mt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new OpenDataException(javaType +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                " is not a supported MXBean type.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        convertedTypes.put(javaType, mt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return mt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // basic types do not require data mapping
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   109
    static synchronized MappedMXBeanType newBasicType(Class<?> c, OpenType<?> ot)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        MappedMXBeanType mt = new BasicMXBeanType(c, ot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        convertedTypes.put(c, mt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return mt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   116
    public static synchronized MappedMXBeanType getMappedType(Type t)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        MappedMXBeanType mt = convertedTypes.get(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (mt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            mt = newMappedType(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (mt.getOpenType() instanceof InProgress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throw new OpenDataException("Recursive data structure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return mt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    // Convert a class to an OpenType
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   130
    public static synchronized OpenType<?> toOpenType(Type t)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        MappedMXBeanType mt = getMappedType(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return mt.getOpenType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public static Object toJavaTypeData(Object openData, Type t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throws OpenDataException, InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (openData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        MappedMXBeanType mt = getMappedType(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return mt.toJavaTypeData(openData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public static Object toOpenTypeData(Object data, Type t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (data == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        MappedMXBeanType mt = getMappedType(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return mt.toOpenTypeData(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    // Return the mapped open type
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   155
    public OpenType<?> getOpenType() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return openType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    boolean isBasicType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return isBasicType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // Return the type name of the mapped open type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // For primitive types, the type name is the same as the javaType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // but the mapped open type is the wrapper class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    String getTypeName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return getMappedTypeClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // Return the mapped open type
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   171
    Class<?> getMappedTypeClass() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return mappedTypeClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    abstract Type getJavaType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    // return name of the class or the generic type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    abstract String getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   180
    public abstract Object toOpenTypeData(Object javaTypeData)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        throws OpenDataException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   183
    public abstract Object toJavaTypeData(Object openTypeData)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        throws OpenDataException, InvalidObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    // Basic Types - Classes that do not require data conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    //               including primitive types and all SimpleType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    //   Mapped open type: SimpleType for corresponding basic type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    // Data Mapping:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    //   T <-> T (no conversion)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    static class BasicMXBeanType extends MappedMXBeanType {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   195
        final Class<?> basicType;
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   196
        BasicMXBeanType(Class<?> c, OpenType<?> openType) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            this.basicType = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            this.openType = openType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            this.mappedTypeClass = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            this.isBasicType = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        Type getJavaType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return basicType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return basicType.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   211
        public Object toOpenTypeData(Object data) throws OpenDataException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   215
        public Object toJavaTypeData(Object data)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            throws OpenDataException, InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    // Enum subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    //   Mapped open type - String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    // Data Mapping:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    //   Enum <-> enum's name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    static class EnumMXBeanType extends MappedMXBeanType {
25405
aa7f539c3569 8049820: Fix raw and unchecked lint warnings in sun.management
darcy
parents: 14342
diff changeset
   230
        @SuppressWarnings("rawtypes")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        final Class enumClass;
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   232
        EnumMXBeanType(Class<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            this.enumClass = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            this.openType = STRING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            this.mappedTypeClass = String.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        Type getJavaType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return enumClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return enumClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   246
        public Object toOpenTypeData(Object data) throws OpenDataException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return ((Enum) data).name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   250
        public Object toJavaTypeData(Object data)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throws OpenDataException, InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                return Enum.valueOf(enumClass, (String) data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                // missing enum constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                final InvalidObjectException ioe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    new InvalidObjectException("Enum constant named " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    (String) data + " is missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                ioe.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    // Array E[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    //   Mapped open type - Array with element of OpenType for E
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    // Data Mapping:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    //   E[] <-> openTypeData(E)[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    static class ArrayMXBeanType extends MappedMXBeanType {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   273
        final Class<?> arrayClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        protected MappedMXBeanType componentType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        protected MappedMXBeanType baseElementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   277
        ArrayMXBeanType(Class<?> c) throws OpenDataException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            this.arrayClass = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            this.componentType = getMappedType(c.getComponentType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            StringBuilder className = new StringBuilder();
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   282
            Class<?> et = c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            int dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            for (dim = 0; et.isArray(); dim++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                className.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                et = et.getComponentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            baseElementType = getMappedType(et);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (et.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                className = new StringBuilder(c.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            } else {
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 25859
diff changeset
   292
                className.append('L').append(baseElementType.getTypeName()).append(';');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                mappedTypeClass = Class.forName(className.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                final OpenDataException ode =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    new OpenDataException("Cannot obtain array class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                ode.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                throw ode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   303
            openType = new ArrayType<>(dim, baseElementType.getOpenType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        protected ArrayMXBeanType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            arrayClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        Type getJavaType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return arrayClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return arrayClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   318
        public Object toOpenTypeData(Object data) throws OpenDataException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            // If the base element type is a basic type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            // return the data as no conversion is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            // Primitive types are not converted to wrappers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (baseElementType.isBasicType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            final Object[] array = (Object[]) data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            final Object[] openArray = (Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                Array.newInstance(componentType.getMappedTypeClass(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                  array.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            for (Object o : array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    openArray[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    openArray[i] = componentType.toOpenTypeData(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return openArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   343
        public Object toJavaTypeData(Object data)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            throws OpenDataException, InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            // If the base element type is a basic type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            // return the data as no conversion is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (baseElementType.isBasicType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            final Object[] openArray = (Object[]) data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            final Object[] array = (Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                Array.newInstance((Class) componentType.getJavaType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                  openArray.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            for (Object o : openArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    array[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    array[i] = componentType.toJavaTypeData(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    static class GenericArrayMXBeanType extends ArrayMXBeanType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        final GenericArrayType gtype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        GenericArrayMXBeanType(GenericArrayType gat) throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            this.gtype = gat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            this.componentType = getMappedType(gat.getGenericComponentType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            StringBuilder className = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            Type elementType = gat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            int dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            for (dim = 0; elementType instanceof GenericArrayType; dim++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                className.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                GenericArrayType et = (GenericArrayType) elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                elementType = et.getGenericComponentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            baseElementType = getMappedType(elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (elementType instanceof Class && ((Class) elementType).isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                className = new StringBuilder(gat.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            } else {
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 25859
diff changeset
   388
                className.append('L').append(baseElementType.getTypeName()).append(';');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                mappedTypeClass = Class.forName(className.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                final OpenDataException ode =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    new OpenDataException("Cannot obtain array class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                ode.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                throw ode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   399
            openType = new ArrayType<>(dim, baseElementType.getOpenType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        Type getJavaType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            return gtype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            return gtype.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    // List<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    //   Mapped open type - Array with element of OpenType for E
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    // Data Mapping:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    //   List<E> <-> openTypeData(E)[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    static class ListMXBeanType extends MappedMXBeanType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        final ParameterizedType javaType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        final MappedMXBeanType paramType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        final String typeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        ListMXBeanType(ParameterizedType pt) throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            this.javaType = pt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            final Type[] argTypes = pt.getActualTypeArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            assert(argTypes.length == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            if (!(argTypes[0] instanceof Class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                throw new OpenDataException("Element Type for " + pt +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                   " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            }
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   432
            final Class<?> et = (Class<?>) argTypes[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            if (et.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                throw new OpenDataException("Element Type for " + pt +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                   " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            paramType = getMappedType(et);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            typeName = "List<" + paramType.getName() + ">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                mappedTypeClass = Class.forName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    "[L" + paramType.getTypeName() + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                final OpenDataException ode =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    new OpenDataException("Array class not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                ode.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                throw ode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   449
            openType = new ArrayType<>(1, paramType.getOpenType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        Type getJavaType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            return javaType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            return typeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   460
        public Object toOpenTypeData(Object data) throws OpenDataException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            final List<Object> list = (List<Object>) data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            final Object[] openArray = (Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                Array.newInstance(paramType.getMappedTypeClass(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                  list.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            for (Object o : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                openArray[i++] = paramType.toOpenTypeData(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            return openArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   473
        public Object toJavaTypeData(Object data)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            throws OpenDataException, InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            final Object[] openArray = (Object[]) data;
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   477
            List<Object> result = new ArrayList<>(openArray.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            for (Object o : openArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                result.add(paramType.toJavaTypeData(o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    private static final String KEY   = "key";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    private static final String VALUE = "value";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    private static final String[] mapIndexNames = {KEY};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    private static final String[] mapItemNames = {KEY, VALUE};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    // Map<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    //   Mapped open type - TabularType with row type:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    //                        CompositeType:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    //                          "key"   of openDataType(K)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    //                          "value" of openDataType(V)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    //                        "key" is the index name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    // Data Mapping:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    //   Map<K,V> <-> TabularData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    static class MapMXBeanType extends MappedMXBeanType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        final ParameterizedType javaType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        final MappedMXBeanType keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        final MappedMXBeanType valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        final String typeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        MapMXBeanType(ParameterizedType pt) throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            this.javaType = pt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            final Type[] argTypes = pt.getActualTypeArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            assert(argTypes.length == 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            this.keyType = getMappedType(argTypes[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            this.valueType = getMappedType(argTypes[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            // FIXME: generate typeName for generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            typeName = "Map<" + keyType.getName() + "," +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                                valueType.getName() + ">";
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   518
            final OpenType<?>[] mapItemTypes = new OpenType<?>[] {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                                keyType.getOpenType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                                                valueType.getOpenType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                                            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            final CompositeType rowType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                new CompositeType(typeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                                  typeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                  mapItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                                  mapItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                  mapItemTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            openType = new TabularType(typeName, typeName, rowType, mapIndexNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            mappedTypeClass = javax.management.openmbean.TabularData.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        Type getJavaType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            return javaType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            return typeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   541
        public Object toOpenTypeData(Object data) throws OpenDataException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            final Map<Object,Object> map = (Map<Object,Object>) data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            final TabularType tabularType = (TabularType) openType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            final TabularData table = new TabularDataSupport(tabularType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            final CompositeType rowType = tabularType.getRowType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   547
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                final Object key = keyType.toOpenTypeData(entry.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                final Object value = valueType.toOpenTypeData(entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                final CompositeData row =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    new CompositeDataSupport(rowType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                                             mapItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                             new Object[] {key, value});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                table.put(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            return table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   559
        public Object toJavaTypeData(Object data)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            throws OpenDataException, InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            final TabularData td = (TabularData) data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   564
            Map<Object, Object> result = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            for (CompositeData row : (Collection<CompositeData>) td.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                Object key = keyType.toJavaTypeData(row.get(KEY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                Object value = valueType.toJavaTypeData(row.get(VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                result.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    private static final Class<?> COMPOSITE_DATA_CLASS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        javax.management.openmbean.CompositeData.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    // Classes that have a static from method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    //   Mapped open type - CompositeData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    // Data Mapping:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    //   Classes <-> CompositeData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    // The name and type of items for a class are identified from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    // the getter methods. For example, a class defines a method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    //    public FooType getFoo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    // The composite data view for this class will contain one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    // item entry for a "foo" attribute and the item type is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    // one of the open types defined in the OpenType class that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    // can be determined in the following manner:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    // o If FooType is a primitive type, the item type a wrapper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    //   class for the corresponding primitive type (such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    //   Integer, Long, Boolean, etc).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    // o If FooType is of type CompositeData or TabularData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    //   the item type is FooType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    // o If FooType is an Enum, the item type is a String and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    //   the value is the name of the enum constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    // o If FooType is a class or an interface other than the above,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    //   the item type is CompositeData. The same convention
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    //   can be recursively applied to the FooType class when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    //   constructing the composite data for the "foo" attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    // o If FooType is an array, the item type is an array and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    //   its element type is determined as described above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    static class CompositeDataMXBeanType extends MappedMXBeanType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        final Class<?> javaClass;
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   608
        boolean isCompositeData = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        Method fromMethod = null;
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   610
        Method toMethod = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   612
        CompositeDataMXBeanType(Class<?> c) throws OpenDataException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            this.javaClass = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            this.mappedTypeClass = COMPOSITE_DATA_CLASS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            // check if a static from method exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                fromMethod = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                        public Method run() throws NoSuchMethodException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                            return javaClass.getMethod("from", COMPOSITE_DATA_CLASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                // ignore NoSuchMethodException since we allow classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                // that has no from method to be embeded in another class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            if (COMPOSITE_DATA_CLASS.isAssignableFrom(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                // c implements CompositeData - set openType to null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                // defer generating the CompositeType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                // until the object is constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                this.isCompositeData = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                this.openType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                this.isCompositeData = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                // Make a CompositeData containing all the getters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                final Method[] methods =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    AccessController.doPrivileged(new PrivilegedAction<Method[]>() {
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   640
                            public Method[] run() {
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   641
                                return javaClass.getMethods();
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   642
                            }
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   643
                        });
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   644
                final List<String> names = new ArrayList<>();
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   645
                final List<OpenType<?>> types = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                /* Select public methods that look like "T getX()" or "boolean
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   648
                 isX()", where T is not void and X is not the empty
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   649
                 string.  Exclude "Class getClass()" inherited from Object.  */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                for (int i = 0; i < methods.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    final Method method = methods[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    final String name = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    final Type type = method.getGenericReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    final String rest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    if (name.startsWith("get")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                        rest = name.substring(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    } else if (name.startsWith("is") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                               type instanceof Class &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                               ((Class) type) == boolean.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                        rest = name.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                        // ignore non-getter methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 47216
diff changeset
   666
                    if (rest.isEmpty() ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                        method.getParameterTypes().length > 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                        type == void.class ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                        rest.equals("Class")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        // ignore non-getter methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    names.add(decapitalize(rest));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    types.add(toOpenType(type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                final String[] nameArray = names.toArray(new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                openType = new CompositeType(c.getName(),
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   680
                        c.getName(),
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   681
                        nameArray, // field names
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   682
                        nameArray, // field descriptions
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   683
                        types.toArray(new OpenType<?>[0]));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        Type getJavaType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            return javaClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            return javaClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   695
        public Object toOpenTypeData(Object data) throws OpenDataException {
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   696
            if (toMethod != null) {
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   697
                try {
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   698
                    return toMethod.invoke(null, data);
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   699
                } catch (IllegalAccessException e) {
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   700
                    // should never reach here
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   701
                    throw new AssertionError(e);
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   702
                } catch (InvocationTargetException e) {
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   703
                    final OpenDataException ode
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   704
                            = new OpenDataException("Failed to invoke "
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   705
                                    + toMethod.getName() + " to convert " + javaClass.getName()
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   706
                                    + " to CompositeData");
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   707
                    ode.initCause(e);
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   708
                    throw ode;
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   709
                }
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   710
            }
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   711
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            if (data instanceof MemoryUsage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                return MemoryUsageCompositeData.toCompositeData((MemoryUsage) data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            if (data instanceof ThreadInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                return ThreadInfoCompositeData.toCompositeData((ThreadInfo) data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            if (data instanceof LockInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                if (data instanceof java.lang.management.MonitorInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    return MonitorInfoCompositeData.toCompositeData((MonitorInfo) data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                }
13803
889df16bef60 7193302: Remove ConstructorProperties annotation from java.lang.management.LockInfo
mchung
parents: 13589
diff changeset
   724
                return LockInfoCompositeData.toCompositeData((LockInfo) data);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            if (data instanceof MemoryNotificationInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                return MemoryNotifInfoCompositeData.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    toCompositeData((MemoryNotificationInfo) data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            if (isCompositeData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                // Classes that implement CompositeData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                // construct a new CompositeDataSupport object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                // so that no other classes are sent over the wire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                CompositeData cd = (CompositeData) data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                CompositeType ct = cd.getCompositeType();
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   739
                String[] itemNames = ct.keySet().toArray(new String[0]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                Object[] itemValues = cd.getAll(itemNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                return new CompositeDataSupport(ct, itemNames, itemValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            throw new OpenDataException(javaClass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                " is not supported for platform MXBeans");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
30355
e37c7eba132f 8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents: 27957
diff changeset
   748
        public Object toJavaTypeData(Object data)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            throws OpenDataException, InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            if (fromMethod == null) {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   752
                throw new AssertionError("Does not support data conversion");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                return fromMethod.invoke(null, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                // should never reach here
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   759
                throw new AssertionError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                final OpenDataException ode =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                    new OpenDataException("Failed to invoke " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                        fromMethod.getName() + " to convert CompositeData " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                        " to " + javaClass.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                ode.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                throw ode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
25405
aa7f539c3569 8049820: Fix raw and unchecked lint warnings in sun.management
darcy
parents: 14342
diff changeset
   771
    private static class InProgress<T> extends OpenType<T> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        private static final String description =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                  "Marker to detect recursive type use -- internal use only!";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        InProgress() throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            super("java.lang.String", "java.lang.String", description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            return description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        public boolean isValue(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
4173
d01972926813 6895875: Missing serialVersionUID in sun.management classes
mchung
parents: 715
diff changeset
   794
        private static final long serialVersionUID = -3413063475064374490L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    }
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   796
    private static final OpenType<?> inProgress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    static {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   798
        OpenType<?> t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        try {
25405
aa7f539c3569 8049820: Fix raw and unchecked lint warnings in sun.management
darcy
parents: 14342
diff changeset
   800
            t = new InProgress<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        } catch (OpenDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            // Should not reach here
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   803
            throw new AssertionError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        inProgress = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
25405
aa7f539c3569 8049820: Fix raw and unchecked lint warnings in sun.management
darcy
parents: 14342
diff changeset
   808
    private static final OpenType<?>[] simpleTypes = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        BIGDECIMAL, BIGINTEGER, BOOLEAN, BYTE, CHARACTER, DATE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        DOUBLE, FLOAT, INTEGER, LONG, OBJECTNAME, SHORT, STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        VOID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            for (int i = 0; i < simpleTypes.length; i++) {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   816
                final OpenType<?> t = simpleTypes[i];
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   817
                Class<?> c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    c = Class.forName(t.getClassName(), false,
13589
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 11530
diff changeset
   820
                                      MappedMXBeanType.class.getClassLoader());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    MappedMXBeanType.newBasicType(c, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                    // the classes that these predefined types declare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    // must exist!
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   825
                    throw new AssertionError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                } catch (OpenDataException e) {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   827
                    throw new AssertionError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                if (c.getName().startsWith("java.lang.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                        final Field typeField = c.getField("TYPE");
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   833
                        final Class<?> primitiveType = (Class<?>) typeField.get(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                        MappedMXBeanType.newBasicType(primitiveType, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    } catch (NoSuchFieldException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                        // OK: must not be a primitive wrapper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                    } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                        // Should not reach here
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   839
                       throw new AssertionError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        } catch (OpenDataException e) {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   844
            throw new AssertionError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * Utility method to take a string and convert it to normal Java variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * name capitalization.  This normally means converting the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * character from upper case to lower case, but in the (unusual) special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * case when there is more than one character and both the first and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * second characters are upper case, we leave it alone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * as "URL".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * @param  name The string to be decapitalized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @return  The decapitalized version of the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    private static String decapitalize(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        if (name == null || name.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                        Character.isUpperCase(name.charAt(0))){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        char chars[] = name.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        chars[0] = Character.toLowerCase(chars[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        return new String(chars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
}