APIdia Launch Partner
Top Description Fields Constructors Methods
com.sun.jmx.mbeanserver

public abstract Class MXBeanMapping

extends Object
Class Inheritance
Known Direct Subclasses
com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory.NonNullMXBeanMapping
Imports
java.io.InvalidObjectException, java.lang.reflect.Type, javax.management.openmbean.OpenDataException, .OpenType

A custom mapping between Java types and Open types for use in MXBeans. To define such a mapping, subclass this class and define at least the fromOpenValue and toOpenValue methods, and optionally the checkReconstructible method. Then either use an MXBeanMappingClass annotation on your custom Java types, or include this MXBeanMapping in an MXBeanMappingFactory.

For example, suppose we have a class MyLinkedList, which looks like this:

public class MyLinkedList {
    public MyLinkedList(String name, MyLinkedList next) {...}
    public String getName() {...}
    public MyLinkedList getNext() {...}
}

This is not a valid type for MXBeans, because it contains a self-referential property "next" defined by the getNext() method. MXBeans do not support recursive types. So we would like to specify a mapping for MyLinkedList explicitly. When an MXBean interface contains MyLinkedList, that will be mapped into a String[], which is a valid Open Type.

To define this mapping, we first subclass MXBeanMapping:

public class MyLinkedListMapping extends MXBeanMapping {
    public MyLinkedListMapping(Type type) throws OpenDataException {
        super(MyLinkedList.class, ArrayType.getArrayType(SimpleType.STRING));
        if (type != MyLinkedList.class)
            throw new OpenDataException("Mapping only valid for MyLinkedList");
    }

    @Override
    public Object fromOpenValue(Object openValue) throws InvalidObjectException {
        String[] array = (String[]) openValue;
        MyLinkedList list = null;
        for (int i = array.length - 1; i >= 0; i--)
            list = new MyLinkedList(array[i], list);
        return list;
    }

    @Override
    public Object toOpenValue(Object javaValue) throws OpenDataException {
        ArrayList<String> array = new ArrayList<String>();
        for (MyLinkedList list = (MyLinkedList) javaValue; list != null;
             list = list.getNext())
            array.add(list.getName());
        return array.toArray(new String[0]);
    }
}

The call to the superclass constructor specifies what the original Java type is (MyLinkedList.class) and what Open Type it is mapped to (ArrayType.getArrayType(SimpleType.STRING)). The fromOpenValue method says how we go from the Open Type (String[]) to the Java type (MyLinkedList), and the toOpenValue method says how we go from the Java type to the Open Type.

With this mapping defined, we can annotate the MyLinkedList class appropriately:

@MXBeanMappingClass(MyLinkedListMapping.class)
public class MyLinkedList {...}

Now we can use MyLinkedList in an MXBean interface and it will work.

If we are unable to modify the MyLinkedList class, we can define an MXBeanMappingFactory. See the documentation of that class for further details.

See Also
MXBean specification, section "Custom MXBean type mappings"

Field Summary

Modifier and TypeField and Description
private final Type
private final Class<?>
private final OpenType<?>

Constructor Summary

AccessConstructor and Description
protected
MXBeanMapping(Type
the Java type (for example, MyLinkedList).
javaType
,
OpenType<?>
the Open Type (for example, ArrayType.getArrayType(SimpleType.STRING))
openType
)

Construct a mapping between the given Java type and the given Open Type.

Method Summary

Modifier and TypeMethod and Description
public void
checkReconstructible()

Throw an appropriate InvalidObjectException if we will not be able to convert back from the open data to the original Java object.

public abstract Object

Returns:

the converted value.
fromOpenValue
(Object
the value to be converted.
openValue
)

Convert an instance of the Open Type into the Java type.

public final Type

Returns:

the Java type that was supplied to the constructor.
getJavaType
()

The Java type that was supplied to the constructor.

public final Class<?>

Returns:

the Java class that corresponds to instances of the Open Type for this mapping.
getOpenClass
()

The Java class that corresponds to instances of the Open Type for this mapping.

public final OpenType<?>

Returns:

the Open Type that was supplied to the constructor.
getOpenType
()

The Open Type that was supplied to the constructor.

private static Class<?>
makeOpenClass(Type javaType, OpenType<?> openType)

public abstract Object

Returns:

the converted value.
toOpenValue
(Object
the value to be converted.
javaValue
)

Convert an instance of the Java type into the Open Type.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

javaTypeback to summary
private final Type javaType
openClassback to summary
private final Class<?> openClass
openTypeback to summary
private final OpenType<?> openType

Constructor Detail

MXBeanMappingback to summary
protected MXBeanMapping(Type javaType, OpenType<?> openType)

Construct a mapping between the given Java type and the given Open Type.

Parameters
javaType:Type

the Java type (for example, MyLinkedList).

openType:OpenType<?>

the Open Type (for example, ArrayType.getArrayType(SimpleType.STRING))

Exceptions
NullPointerException:
if either argument is null.

Method Detail

checkReconstructibleback to summary
public void checkReconstructible() throws InvalidObjectException

Throw an appropriate InvalidObjectException if we will not be able to convert back from the open data to the original Java object. The fromOpenValue throws an exception if a given open data value cannot be converted. This method throws an exception if no open data values can be converted. The default implementation of this method never throws an exception. Subclasses can override it as appropriate.

Exceptions
InvalidObjectException:
if fromOpenValue will throw an exception no matter what its argument is.
fromOpenValueback to summary
public abstract Object fromOpenValue(Object openValue) throws InvalidObjectException

Convert an instance of the Open Type into the Java type.

Parameters
openValue:Object

the value to be converted.

Returns:Object

the converted value.

Exceptions
InvalidObjectException:
if the value cannot be converted.
getJavaTypeback to summary
public final Type getJavaType()

The Java type that was supplied to the constructor.

Returns:Type

the Java type that was supplied to the constructor.

getOpenClassback to summary
public final Class<?> getOpenClass()

The Java class that corresponds to instances of the Open Type for this mapping.

Returns:Class<?>

the Java class that corresponds to instances of the Open Type for this mapping.

See Also
OpenType#getClassName
getOpenTypeback to summary
public final OpenType<?> getOpenType()

The Open Type that was supplied to the constructor.

Returns:OpenType<?>

the Open Type that was supplied to the constructor.

makeOpenClassback to summary
private static Class<?> makeOpenClass(Type javaType, OpenType<?> openType)
toOpenValueback to summary
public abstract Object toOpenValue(Object javaValue) throws OpenDataException

Convert an instance of the Java type into the Open Type.

Parameters
javaValue:Object

the value to be converted.

Returns:Object

the converted value.

Exceptions
OpenDataException:
if the value cannot be converted.