jdk/src/share/classes/javax/management/openmbean/MXBeanMapping.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 687 874e25a9844a
child 1011 2cc4873fa29f
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 687
diff changeset
     2
 * Copyright 2007-2008 Sun Microsystems, Inc.  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
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    23
 * have any questions.
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
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    26
package javax.management.openmbean;
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;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    30
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    31
/**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    32
 * <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
    33
 * 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
    34
 * {@link #fromOpenValue fromOpenValue} and {@link #toOpenValue toOpenValue}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    35
 * methods, and optionally the {@link #checkReconstructible} method.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    36
 * Then either use an {@link MXBeanMappingClass} annotation on your custom
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    37
 * Java types, or include this MXBeanMapping in an
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    38
 * {@link MXBeanMappingFactory}.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    39
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    40
 * <p>For example, suppose we have a class {@code MyLinkedList}, which looks
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    41
 * like this:</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    42
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    43
 * <pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    44
 * public class MyLinkedList {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    45
 *     public MyLinkedList(String name, MyLinkedList next) {...}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    46
 *     public String getName() {...}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    47
 *     public MyLinkedList getNext() {...}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    48
 * }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    49
 * </pre>
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
 * <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
    52
 * self-referential property "next" defined by the {@code getNext()}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    53
 * method.  MXBeans do not support recursive types.  So we would like
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    54
 * to specify a mapping for {@code MyLinkedList} explicitly. When an
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    55
 * MXBean interface contains {@code MyLinkedList}, that will be mapped
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    56
 * into a {@code String[]}, which is a valid Open Type.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    57
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    58
 * <p>To define this mapping, we first subclass {@code MXBeanMapping}:</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
 * <pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    61
 * public class MyLinkedListMapping extends MXBeanMapping {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    62
 *     public MyLinkedListMapping(Type type) throws OpenDataException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    63
 *         super(MyLinkedList.class, ArrayType.getArrayType(SimpleType.STRING));
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    64
 *         if (type != MyLinkedList.class)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    65
 *             throw new OpenDataException("Mapping only valid for MyLinkedList");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    66
 *     }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    67
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    68
 *     {@literal @Override}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    69
 *     public Object fromOpenValue(Object openValue) throws InvalidObjectException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    70
 *         String[] array = (String[]) openValue;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    71
 *         MyLinkedList list = null;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    72
 *         for (int i = array.length - 1; i &gt;= 0; i--)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    73
 *             list = new MyLinkedList(array[i], list);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    74
 *         return list;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    75
 *     }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    76
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    77
 *     {@literal @Override}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    78
 *     public Object toOpenValue(Object javaValue) throws OpenDataException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    79
 *         ArrayList&lt;String&gt; array = new ArrayList&lt;String&gt;();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    80
 *         for (MyLinkedList list = (MyLinkedList) javaValue; list != null;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    81
 *              list = list.getNext())
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    82
 *             array.add(list.getName());
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    83
 *         return array.toArray(new String[0]);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    84
 *     }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    85
 * }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    86
 * </pre>
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
 * <p>The call to the superclass constructor specifies what the
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    89
 * original Java type is ({@code MyLinkedList.class}) and what Open
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    90
 * Type it is mapped to ({@code
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    91
 * ArrayType.getArrayType(SimpleType.STRING)}). The {@code
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    92
 * fromOpenValue} method says how we go from the Open Type ({@code
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    93
 * String[]}) to the Java type ({@code MyLinkedList}), and the {@code
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    94
 * 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
    95
 * Type.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    96
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    97
 * <p>With this mapping defined, we can annotate the {@code MyLinkedList}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    98
 * class appropriately:</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    99
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   100
 * <pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   101
 * {@literal @MXBeanMappingClass}(MyLinkedListMapping.class)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   102
 * public class MyLinkedList {...}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   103
 * </pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   104
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   105
 * <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
   106
 * will work.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   107
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   108
 * <p>If we are unable to modify the {@code MyLinkedList} class,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   109
 * we can define an {@link MXBeanMappingFactory}.  See the documentation
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   110
 * of that class for further details.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   111
 */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   112
public abstract class MXBeanMapping {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   113
    private final Type javaType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   114
    private final OpenType<?> openType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   115
    private final Class<?> openClass;
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
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   118
     * <p>Construct a mapping between the given Java type and the given
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   119
     * Open Type.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   120
     *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   121
     * @param javaType the Java type (for example, {@code MyLinkedList}).
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   122
     * @param openType the Open Type (for example, {@code
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   123
     * ArrayType.getArrayType(SimpleType.STRING)})
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   124
     *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   125
     * @throws NullPointerException if either argument is null.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   126
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   127
    protected MXBeanMapping(Type javaType, OpenType<?> openType) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   128
        if (javaType == null || openType == null)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   129
            throw new NullPointerException("Null argument");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   130
        this.javaType = javaType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   131
        this.openType = openType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   132
        this.openClass = makeOpenClass(javaType, openType);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   133
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   134
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   135
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   136
     * <p>The Java type that was supplied to the constructor.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   137
     * @return the Java type that was supplied to the constructor.
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
    public final Type getJavaType() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   140
        return javaType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   141
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   142
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
     * <p>The Open Type that was supplied to the constructor.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   145
     * @return the Open Type that was supplied to the constructor.
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
    public final OpenType<?> getOpenType() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   148
        return openType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   149
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   150
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
     * <p>The Java class that corresponds to instances of the
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   153
     * {@linkplain #getOpenType() Open Type} for this mapping.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   154
     * @return the Java class that corresponds to instances of the
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   155
     * Open Type for this mapping.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   156
     * @see OpenType#getClassName
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   157
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   158
    public final Class<?> getOpenClass() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   159
        return openClass;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   160
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   161
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   162
    private static Class<?> makeOpenClass(Type javaType, OpenType<?> openType) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   163
        if (javaType instanceof Class<?> && ((Class<?>) javaType).isPrimitive())
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   164
            return (Class<?>) javaType;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   165
        try {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   166
            String className = OpenType.validClassName(openType.getClassName());
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   167
            return Class.forName(className, false, null);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   168
        } catch (ClassNotFoundException e) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   169
            throw new RuntimeException(e);  // should not happen
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   170
        } catch (OpenDataException e) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   171
            throw new IllegalArgumentException("Bad OpenType: " + openType, e);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   172
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   173
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   174
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
     * <p>Convert an instance of the Open Type into the Java type.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   177
     * @param openValue the value to be converted.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   178
     * @return the converted value.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   179
     * @throws InvalidObjectException if the value cannot be converted.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   180
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   181
    public abstract Object fromOpenValue(Object openValue)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   182
    throws InvalidObjectException;
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
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   185
     * <p>Convert an instance of the Java type into the Open Type.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   186
     * @param javaValue the value to be converted.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   187
     * @return the converted value.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   188
     * @throws OpenDataException if the value cannot be converted.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   189
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   190
    public abstract Object toOpenValue(Object javaValue)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   191
    throws OpenDataException;
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
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   194
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   195
     * <p>Throw an appropriate InvalidObjectException if we will not
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   196
     * 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
   197
     * object.  The {@link #fromOpenValue fromOpenValue} throws an
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   198
     * exception if a given open data value cannot be converted.  This
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   199
     * method throws an exception if <em>no</em> open data values can
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   200
     * be converted.  The default implementation of this method never
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   201
     * throws an exception.  Subclasses can override it as
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   202
     * appropriate.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   203
     * @throws InvalidObjectException if {@code fromOpenValue} will throw
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   204
     * an exception no matter what its argument is.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   205
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   206
    public void checkReconstructible() throws InvalidObjectException {}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   207
}