jdk/src/share/classes/sun/management/MappedMXBeanType.java
author mchung
Tue, 17 Jan 2012 15:55:40 -0800
changeset 11530 a9d059c15b80
parent 5506 202f599c92aa
child 13589 da4cb574f4a6
permissions -rw-r--r--
7117570: Warnings in sun.mangement.* and its subpackages Reviewed-by: mchung, dsamersoff Contributed-by: kurchi.subhra.hazra@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4173
diff changeset
     2
 * Copyright (c) 2004, 2008, 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.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.openmbean.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import static javax.management.openmbean.SimpleType.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import com.sun.management.VMOption;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * A mapped mxbean type maps a Java type to an open type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Only the following Java types are mappable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * (currently required by the platform MXBeans):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *   1. Primitive types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *   2. Wrapper classes such java.lang.Integer, etc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   3. Classes with only getter methods and with a static "from" method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *      that takes a CompositeData argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *   4. E[] where E is a type of 1-4 (can be multi-dimensional array)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *   5. List<E> where E is a type of 1-3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *   6. Map<K, V> where K and V are a type of 1-4
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    static synchronized MappedMXBeanType getMappedType(Type t)
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
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   155
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    abstract Object toOpenTypeData(Object javaTypeData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        throws OpenDataException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    abstract Object toJavaTypeData(Object openTypeData)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        Object toOpenTypeData(Object data) throws OpenDataException {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        Object toJavaTypeData(Object data)
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 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        final Class enumClass;
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   231
        EnumMXBeanType(Class<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            this.enumClass = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            this.openType = STRING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            this.mappedTypeClass = String.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        Type getJavaType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return enumClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return enumClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        Object toOpenTypeData(Object data) throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return ((Enum) data).name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        Object toJavaTypeData(Object data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            throws OpenDataException, InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                return Enum.valueOf(enumClass, (String) data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                // missing enum constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                final InvalidObjectException ioe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    new InvalidObjectException("Enum constant named " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    (String) data + " is missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                ioe.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
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
    // Array E[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    //   Mapped open type - Array with element of OpenType for E
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    // Data Mapping:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    //   E[] <-> openTypeData(E)[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    static class ArrayMXBeanType extends MappedMXBeanType {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   272
        final Class<?> arrayClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        protected MappedMXBeanType componentType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        protected MappedMXBeanType baseElementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   276
        ArrayMXBeanType(Class<?> c) throws OpenDataException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            this.arrayClass = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            this.componentType = getMappedType(c.getComponentType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            StringBuilder className = new StringBuilder();
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   281
            Class<?> et = c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            int dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            for (dim = 0; et.isArray(); dim++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                className.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                et = et.getComponentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            baseElementType = getMappedType(et);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (et.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                className = new StringBuilder(c.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                className.append("L" + baseElementType.getTypeName() + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                mappedTypeClass = Class.forName(className.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                final OpenDataException ode =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    new OpenDataException("Cannot obtain array class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                ode.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                throw ode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   302
            openType = new ArrayType<>(dim, baseElementType.getOpenType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        protected ArrayMXBeanType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            arrayClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        Type getJavaType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return arrayClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            return arrayClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        Object toOpenTypeData(Object data) throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            // If the base element type is a basic type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            // return the data as no conversion is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            // Primitive types are not converted to wrappers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (baseElementType.isBasicType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            final Object[] array = (Object[]) data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            final Object[] openArray = (Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                Array.newInstance(componentType.getMappedTypeClass(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                  array.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            for (Object o : array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    openArray[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    openArray[i] = componentType.toOpenTypeData(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return openArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        Object toJavaTypeData(Object data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            throws OpenDataException, InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            // If the base element type is a basic type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            // return the data as no conversion is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (baseElementType.isBasicType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            final Object[] openArray = (Object[]) data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            final Object[] array = (Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                Array.newInstance((Class) componentType.getJavaType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                  openArray.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            for (Object o : openArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                if (o == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    array[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    array[i] = componentType.toJavaTypeData(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
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
    static class GenericArrayMXBeanType extends ArrayMXBeanType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        final GenericArrayType gtype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        GenericArrayMXBeanType(GenericArrayType gat) throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            this.gtype = gat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            this.componentType = getMappedType(gat.getGenericComponentType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            StringBuilder className = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            Type elementType = gat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            int dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            for (dim = 0; elementType instanceof GenericArrayType; dim++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                className.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                GenericArrayType et = (GenericArrayType) elementType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                elementType = et.getGenericComponentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            baseElementType = getMappedType(elementType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (elementType instanceof Class && ((Class) elementType).isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                className = new StringBuilder(gat.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                className.append("L" + baseElementType.getTypeName() + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                mappedTypeClass = Class.forName(className.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                final OpenDataException ode =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    new OpenDataException("Cannot obtain array class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                ode.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                throw ode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   398
            openType = new ArrayType<>(dim, baseElementType.getOpenType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        Type getJavaType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            return gtype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            return gtype.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    // List<E>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    //   Mapped open type - Array with element of OpenType for E
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    // Data Mapping:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    //   List<E> <-> openTypeData(E)[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    static class ListMXBeanType extends MappedMXBeanType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        final ParameterizedType javaType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        final MappedMXBeanType paramType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        final String typeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        ListMXBeanType(ParameterizedType pt) throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            this.javaType = pt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            final Type[] argTypes = pt.getActualTypeArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            assert(argTypes.length == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (!(argTypes[0] instanceof Class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                throw new OpenDataException("Element Type for " + pt +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                   " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   431
            final Class<?> et = (Class<?>) argTypes[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            if (et.isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                throw new OpenDataException("Element Type for " + pt +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                   " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            paramType = getMappedType(et);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            typeName = "List<" + paramType.getName() + ">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                mappedTypeClass = Class.forName(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    "[L" + paramType.getTypeName() + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                final OpenDataException ode =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    new OpenDataException("Array class not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                ode.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                throw ode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            }
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   448
            openType = new ArrayType<>(1, paramType.getOpenType());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        Type getJavaType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            return javaType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            return typeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        Object toOpenTypeData(Object data) throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            final List<Object> list = (List<Object>) data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            final Object[] openArray = (Object[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                Array.newInstance(paramType.getMappedTypeClass(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                  list.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            for (Object o : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                openArray[i++] = paramType.toOpenTypeData(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            return openArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        Object toJavaTypeData(Object data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            throws OpenDataException, InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            final Object[] openArray = (Object[]) data;
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   476
            List<Object> result = new ArrayList<>(openArray.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            for (Object o : openArray) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                result.add(paramType.toJavaTypeData(o));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    private static final String KEY   = "key";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    private static final String VALUE = "value";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    private static final String[] mapIndexNames = {KEY};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    private static final String[] mapItemNames = {KEY, VALUE};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    // Map<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    //   Mapped open type - TabularType with row type:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    //                        CompositeType:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    //                          "key"   of openDataType(K)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    //                          "value" of openDataType(V)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    //                        "key" is the index name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    // Data Mapping:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    //   Map<K,V> <-> TabularData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    static class MapMXBeanType extends MappedMXBeanType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        final ParameterizedType javaType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        final MappedMXBeanType keyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        final MappedMXBeanType valueType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        final String typeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        MapMXBeanType(ParameterizedType pt) throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            this.javaType = pt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            final Type[] argTypes = pt.getActualTypeArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            assert(argTypes.length == 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            this.keyType = getMappedType(argTypes[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            this.valueType = getMappedType(argTypes[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            // FIXME: generate typeName for generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            typeName = "Map<" + keyType.getName() + "," +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                valueType.getName() + ">";
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   517
            final OpenType<?>[] mapItemTypes = new OpenType<?>[] {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                                                keyType.getOpenType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                                valueType.getOpenType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                                            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            final CompositeType rowType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                new CompositeType(typeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                                  typeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                                  mapItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                  mapItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                                  mapItemTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            openType = new TabularType(typeName, typeName, rowType, mapIndexNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            mappedTypeClass = javax.management.openmbean.TabularData.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        Type getJavaType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            return javaType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            return typeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        Object toOpenTypeData(Object data) throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            final Map<Object,Object> map = (Map<Object,Object>) data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            final TabularType tabularType = (TabularType) openType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            final TabularData table = new TabularDataSupport(tabularType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            final CompositeType rowType = tabularType.getRowType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   546
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                final Object key = keyType.toOpenTypeData(entry.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                final Object value = valueType.toOpenTypeData(entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                final CompositeData row =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    new CompositeDataSupport(rowType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                                             mapItemNames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                                             new Object[] {key, value});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                table.put(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            return table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        Object toJavaTypeData(Object data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            throws OpenDataException, InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            final TabularData td = (TabularData) data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   563
            Map<Object, Object> result = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            for (CompositeData row : (Collection<CompositeData>) td.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                Object key = keyType.toJavaTypeData(row.get(KEY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                Object value = valueType.toJavaTypeData(row.get(VALUE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                result.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    private static final Class<?> COMPOSITE_DATA_CLASS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        javax.management.openmbean.CompositeData.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    // Classes that have a static from method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    //   Mapped open type - CompositeData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    // Data Mapping:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    //   Classes <-> CompositeData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    // The name and type of items for a class are identified from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    // the getter methods. For example, a class defines a method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    //    public FooType getFoo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    // The composite data view for this class will contain one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    // item entry for a "foo" attribute and the item type is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    // one of the open types defined in the OpenType class that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    // can be determined in the following manner:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    // o If FooType is a primitive type, the item type a wrapper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    //   class for the corresponding primitive type (such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    //   Integer, Long, Boolean, etc).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    // o If FooType is of type CompositeData or TabularData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    //   the item type is FooType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    // o If FooType is an Enum, the item type is a String and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    //   the value is the name of the enum constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    // o If FooType is a class or an interface other than the above,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    //   the item type is CompositeData. The same convention
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    //   can be recursively applied to the FooType class when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    //   constructing the composite data for the "foo" attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    // o If FooType is an array, the item type is an array and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    //   its element type is determined as described above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    static class CompositeDataMXBeanType extends MappedMXBeanType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        final Class<?> javaClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        final boolean isCompositeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        Method fromMethod = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   610
        CompositeDataMXBeanType(Class<?> c) throws OpenDataException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            this.javaClass = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            this.mappedTypeClass = COMPOSITE_DATA_CLASS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            // check if a static from method exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                fromMethod = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                        public Method run() throws NoSuchMethodException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                            return javaClass.getMethod("from", COMPOSITE_DATA_CLASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                // ignore NoSuchMethodException since we allow classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                // that has no from method to be embeded in another class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            if (COMPOSITE_DATA_CLASS.isAssignableFrom(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                // c implements CompositeData - set openType to null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                // defer generating the CompositeType
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                // until the object is constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                this.isCompositeData = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                this.openType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                this.isCompositeData = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                // Make a CompositeData containing all the getters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                final Method[] methods =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    AccessController.doPrivileged(new PrivilegedAction<Method[]>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                        public Method[] run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                            return javaClass.getMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    });
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   642
                final List<String> names = new ArrayList<>();
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   643
                final List<OpenType<?>> types = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                /* Select public methods that look like "T getX()" or "boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                   isX()", where T is not void and X is not the empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                   string.  Exclude "Class getClass()" inherited from Object.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                for (int i = 0; i < methods.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    final Method method = methods[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    final String name = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    final Type type = method.getGenericReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    final String rest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    if (name.startsWith("get")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                        rest = name.substring(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    } else if (name.startsWith("is") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                               type instanceof Class &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                               ((Class) type) == boolean.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                        rest = name.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                        // ignore non-getter methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    if (rest.equals("") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                        method.getParameterTypes().length > 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        type == void.class ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                        rest.equals("Class")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                        // ignore non-getter methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    names.add(decapitalize(rest));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    types.add(toOpenType(type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                final String[] nameArray = names.toArray(new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                openType = new CompositeType(c.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                                             c.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                                             nameArray, // field names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                                             nameArray, // field descriptions
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   681
                                             types.toArray(new OpenType<?>[0]));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        Type getJavaType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            return javaClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            return javaClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        Object toOpenTypeData(Object data) throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            if (data instanceof MemoryUsage) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                return MemoryUsageCompositeData.toCompositeData((MemoryUsage) data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            if (data instanceof ThreadInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                return ThreadInfoCompositeData.toCompositeData((ThreadInfo) data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            if (data instanceof LockInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                if (data instanceof java.lang.management.MonitorInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    return MonitorInfoCompositeData.toCompositeData((MonitorInfo) data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                return LockDataConverter.toLockInfoCompositeData((LockInfo) data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            if (data instanceof MemoryNotificationInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                return MemoryNotifInfoCompositeData.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    toCompositeData((MemoryNotificationInfo) data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            if (data instanceof VMOption) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                return VMOptionCompositeData.toCompositeData((VMOption) data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            if (isCompositeData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                // Classes that implement CompositeData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                // construct a new CompositeDataSupport object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                // so that no other classes are sent over the wire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                CompositeData cd = (CompositeData) data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                CompositeType ct = cd.getCompositeType();
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   725
                String[] itemNames = ct.keySet().toArray(new String[0]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                Object[] itemValues = cd.getAll(itemNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                return new CompositeDataSupport(ct, itemNames, itemValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            throw new OpenDataException(javaClass.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                " is not supported for platform MXBeans");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        Object toJavaTypeData(Object data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            throws OpenDataException, InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            if (fromMethod == null) {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   738
                throw new AssertionError("Does not support data conversion");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                return fromMethod.invoke(null, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                // should never reach here
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   745
                throw new AssertionError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                final OpenDataException ode =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    new OpenDataException("Failed to invoke " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                        fromMethod.getName() + " to convert CompositeData " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                        " to " + javaClass.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                ode.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                throw ode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    private static class InProgress extends OpenType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        private static final String description =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                  "Marker to detect recursive type use -- internal use only!";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        InProgress() throws OpenDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            super("java.lang.String", "java.lang.String", description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            return description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        public boolean isValue(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        }
4173
d01972926813 6895875: Missing serialVersionUID in sun.management classes
mchung
parents: 715
diff changeset
   780
        private static final long serialVersionUID = -3413063475064374490L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    }
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   782
    private static final OpenType<?> inProgress;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    static {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   784
        OpenType<?> t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            t = new InProgress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        } catch (OpenDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            // Should not reach here
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   789
            throw new AssertionError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        inProgress = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    private static final OpenType[] simpleTypes = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        BIGDECIMAL, BIGINTEGER, BOOLEAN, BYTE, CHARACTER, DATE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        DOUBLE, FLOAT, INTEGER, LONG, OBJECTNAME, SHORT, STRING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        VOID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            for (int i = 0; i < simpleTypes.length; i++) {
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   802
                final OpenType<?> t = simpleTypes[i];
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   803
                Class<?> c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                    c = Class.forName(t.getClassName(), false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                                      String.class.getClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                    MappedMXBeanType.newBasicType(c, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    // the classes that these predefined types declare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                    // must exist!
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   811
                    throw new AssertionError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                } catch (OpenDataException e) {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   813
                    throw new AssertionError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                if (c.getName().startsWith("java.lang.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                        final Field typeField = c.getField("TYPE");
11530
a9d059c15b80 7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents: 5506
diff changeset
   819
                        final Class<?> primitiveType = (Class<?>) typeField.get(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                        MappedMXBeanType.newBasicType(primitiveType, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    } catch (NoSuchFieldException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                        // OK: must not be a primitive wrapper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                    } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                        // Should not reach here
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
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        } catch (OpenDataException e) {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
   830
            throw new AssertionError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * Utility method to take a string and convert it to normal Java variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * name capitalization.  This normally means converting the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * character from upper case to lower case, but in the (unusual) special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * case when there is more than one character and both the first and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * second characters are upper case, we leave it alone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * as "URL".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * @param  name The string to be decapitalized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @return  The decapitalized version of the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    private static String decapitalize(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        if (name == null || name.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                        Character.isUpperCase(name.charAt(0))){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        char chars[] = name.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        chars[0] = Character.toLowerCase(chars[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        return new String(chars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
}