jdk/src/share/classes/com/sun/jmx/mbeanserver/MXBeanMapping.java
author mchung
Fri, 24 Aug 2012 22:55:49 -0700
changeset 13589 da4cb574f4a6
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
7193339: Prepare system classes be defined by a non-null module loader Reviewed-by: alanb, dholmes, dsamersoff, sspitsyn, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     2
 * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     4
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
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: 4156
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    10
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    15
 * accompanied this code).
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    16
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    23
 * questions.
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    24
 */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    25
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1011
diff changeset
    26
package com.sun.jmx.mbeanserver;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    27
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    28
import java.io.InvalidObjectException;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    29
import java.lang.reflect.Type;
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1011
diff changeset
    30
import javax.management.openmbean.OpenDataException;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1011
diff changeset
    31
import javax.management.openmbean.OpenType;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    32
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    33
/**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    34
 * <p>A custom mapping between Java types and Open types for use in MXBeans.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    35
 * To define such a mapping, subclass this class and define at least the
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    36
 * {@link #fromOpenValue fromOpenValue} and {@link #toOpenValue toOpenValue}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    37
 * methods, and optionally the {@link #checkReconstructible} method.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    38
 * Then either use an {@link MXBeanMappingClass} annotation on your custom
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    39
 * Java types, or include this MXBeanMapping in an
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    40
 * {@link MXBeanMappingFactory}.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    41
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    42
 * <p>For example, suppose we have a class {@code MyLinkedList}, which looks
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    43
 * like this:</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    44
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    45
 * <pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    46
 * public class MyLinkedList {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    47
 *     public MyLinkedList(String name, MyLinkedList next) {...}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    48
 *     public String getName() {...}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    49
 *     public MyLinkedList getNext() {...}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    50
 * }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    51
 * </pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    52
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    53
 * <p>This is not a valid type for MXBeans, because it contains a
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    54
 * self-referential property "next" defined by the {@code getNext()}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    55
 * method.  MXBeans do not support recursive types.  So we would like
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    56
 * to specify a mapping for {@code MyLinkedList} explicitly. When an
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    57
 * MXBean interface contains {@code MyLinkedList}, that will be mapped
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    58
 * into a {@code String[]}, which is a valid Open Type.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    59
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    60
 * <p>To define this mapping, we first subclass {@code MXBeanMapping}:</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    61
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    62
 * <pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    63
 * public class MyLinkedListMapping extends MXBeanMapping {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    64
 *     public MyLinkedListMapping(Type type) throws OpenDataException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    65
 *         super(MyLinkedList.class, ArrayType.getArrayType(SimpleType.STRING));
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    66
 *         if (type != MyLinkedList.class)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    67
 *             throw new OpenDataException("Mapping only valid for MyLinkedList");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    68
 *     }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    69
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    70
 *     {@literal @Override}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    71
 *     public Object fromOpenValue(Object openValue) throws InvalidObjectException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    72
 *         String[] array = (String[]) openValue;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    73
 *         MyLinkedList list = null;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    74
 *         for (int i = array.length - 1; i &gt;= 0; i--)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    75
 *             list = new MyLinkedList(array[i], list);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    76
 *         return list;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    77
 *     }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    78
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    79
 *     {@literal @Override}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    80
 *     public Object toOpenValue(Object javaValue) throws OpenDataException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    81
 *         ArrayList&lt;String&gt; array = new ArrayList&lt;String&gt;();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    82
 *         for (MyLinkedList list = (MyLinkedList) javaValue; list != null;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    83
 *              list = list.getNext())
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    84
 *             array.add(list.getName());
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    85
 *         return array.toArray(new String[0]);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    86
 *     }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    87
 * }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    88
 * </pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    89
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    90
 * <p>The call to the superclass constructor specifies what the
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    91
 * original Java type is ({@code MyLinkedList.class}) and what Open
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    92
 * Type it is mapped to ({@code
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    93
 * ArrayType.getArrayType(SimpleType.STRING)}). The {@code
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    94
 * fromOpenValue} method says how we go from the Open Type ({@code
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    95
 * String[]}) to the Java type ({@code MyLinkedList}), and the {@code
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    96
 * toOpenValue} method says how we go from the Java type to the Open
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    97
 * Type.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    98
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    99
 * <p>With this mapping defined, we can annotate the {@code MyLinkedList}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   100
 * class appropriately:</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   101
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   102
 * <pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   103
 * {@literal @MXBeanMappingClass}(MyLinkedListMapping.class)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   104
 * public class MyLinkedList {...}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   105
 * </pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   106
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   107
 * <p>Now we can use {@code MyLinkedList} in an MXBean interface and it
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   108
 * will work.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   109
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   110
 * <p>If we are unable to modify the {@code MyLinkedList} class,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   111
 * we can define an {@link MXBeanMappingFactory}.  See the documentation
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   112
 * of that class for further details.</p>
1011
2cc4873fa29f 6734273: Minor updates to documentation of Custom MXBean Mappings
emcmanus
parents: 715
diff changeset
   113
 *
2cc4873fa29f 6734273: Minor updates to documentation of Custom MXBean Mappings
emcmanus
parents: 715
diff changeset
   114
 * @see <a href="../MXBean.html#custom">MXBean specification, section
2cc4873fa29f 6734273: Minor updates to documentation of Custom MXBean Mappings
emcmanus
parents: 715
diff changeset
   115
 * "Custom MXBean type mappings"</a>
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   116
 */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   117
public abstract class MXBeanMapping {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   118
    private final Type javaType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   119
    private final OpenType<?> openType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   120
    private final Class<?> openClass;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   121
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   122
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   123
     * <p>Construct a mapping between the given Java type and the given
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   124
     * Open Type.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   125
     *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   126
     * @param javaType the Java type (for example, {@code MyLinkedList}).
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   127
     * @param openType the Open Type (for example, {@code
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   128
     * ArrayType.getArrayType(SimpleType.STRING)})
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   129
     *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   130
     * @throws NullPointerException if either argument is null.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   131
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   132
    protected MXBeanMapping(Type javaType, OpenType<?> openType) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   133
        if (javaType == null || openType == null)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   134
            throw new NullPointerException("Null argument");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   135
        this.javaType = javaType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   136
        this.openType = openType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   137
        this.openClass = makeOpenClass(javaType, openType);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   138
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   139
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   140
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   141
     * <p>The Java type that was supplied to the constructor.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   142
     * @return the Java type that was supplied to the constructor.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   143
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   144
    public final Type getJavaType() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   145
        return javaType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   146
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   147
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   148
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   149
     * <p>The Open Type that was supplied to the constructor.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   150
     * @return the Open Type that was supplied to the constructor.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   151
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   152
    public final OpenType<?> getOpenType() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   153
        return openType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   154
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   155
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   156
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   157
     * <p>The Java class that corresponds to instances of the
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   158
     * {@linkplain #getOpenType() Open Type} for this mapping.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   159
     * @return the Java class that corresponds to instances of the
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   160
     * Open Type for this mapping.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   161
     * @see OpenType#getClassName
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   162
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   163
    public final Class<?> getOpenClass() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   164
        return openClass;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   165
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   166
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   167
    private static Class<?> makeOpenClass(Type javaType, OpenType<?> openType) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   168
        if (javaType instanceof Class<?> && ((Class<?>) javaType).isPrimitive())
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   169
            return (Class<?>) javaType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   170
        try {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1011
diff changeset
   171
            String className = openType.getClassName();
13589
da4cb574f4a6 7193339: Prepare system classes be defined by a non-null module loader
mchung
parents: 5506
diff changeset
   172
            return Class.forName(className, false, MXBeanMapping.class.getClassLoader());
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   173
        } catch (ClassNotFoundException e) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   174
            throw new RuntimeException(e);  // should not happen
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   175
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   176
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   177
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   178
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   179
     * <p>Convert an instance of the Open Type into the Java type.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   180
     * @param openValue the value to be converted.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   181
     * @return the converted value.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   182
     * @throws InvalidObjectException if the value cannot be converted.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   183
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   184
    public abstract Object fromOpenValue(Object openValue)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   185
    throws InvalidObjectException;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   186
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   187
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   188
     * <p>Convert an instance of the Java type into the Open Type.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   189
     * @param javaValue the value to be converted.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   190
     * @return the converted value.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   191
     * @throws OpenDataException if the value cannot be converted.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   192
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   193
    public abstract Object toOpenValue(Object javaValue)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   194
    throws OpenDataException;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   195
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   196
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   197
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   198
     * <p>Throw an appropriate InvalidObjectException if we will not
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   199
     * be able to convert back from the open data to the original Java
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   200
     * object.  The {@link #fromOpenValue fromOpenValue} throws an
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   201
     * exception if a given open data value cannot be converted.  This
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   202
     * method throws an exception if <em>no</em> open data values can
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   203
     * be converted.  The default implementation of this method never
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   204
     * throws an exception.  Subclasses can override it as
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   205
     * appropriate.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   206
     * @throws InvalidObjectException if {@code fromOpenValue} will throw
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   207
     * an exception no matter what its argument is.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   208
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   209
    public void checkReconstructible() throws InvalidObjectException {}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   210
}