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.
| Modifier and Type | Field and Description |
|---|---|
| private final Type | |
| private final Class | |
| private final OpenType |
| Access | Constructor and Description |
|---|---|
| protected | MXBeanMapping(Type
the Java type (for example, javaType, OpenType<?> MyLinkedList).the Open Type (for example, openType)ArrayType.getArrayType(SimpleType.STRING))Construct a mapping between the given Java type and the given Open Type. |
| Modifier and Type | Method 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.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.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.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.The Open Type that was supplied to the constructor. |
| private static Class | |
| public abstract Object | Returns: the converted value.the value to be converted. javaValue)Convert an instance of the Java type into the Open Type. |
| javaType | back to summary |
|---|---|
| private final Type javaType | |
| openClass | back to summary |
|---|---|
| private final Class<?> openClass | |
| openType | back to summary |
|---|---|
| private final OpenType<?> openType | |
| MXBeanMapping | back to summary |
|---|---|
| protected MXBeanMapping(Type javaType, OpenType<?> openType) Construct a mapping between the given Java type and the given Open Type.
| |
| checkReconstructible | back 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 | back to summary |
|---|---|
| public abstract Object fromOpenValue(Object openValue) throws InvalidObjectException Convert an instance of the Open Type into the Java type.
| |
| getJavaType | back to summary |
|---|---|
| public final Type getJavaType() The Java type that was supplied to the constructor.
| |
| getOpenClass | back to summary |
|---|---|
| public final Class The Java class that corresponds to instances of the Open Type for this mapping.
| |
| getOpenType | back to summary |
|---|---|
| public final OpenType The Open Type that was supplied to the constructor.
| |
| makeOpenClass | back to summary |
|---|---|
| private static Class | |
| toOpenValue | back to summary |
|---|---|
| public abstract Object toOpenValue(Object javaValue) throws OpenDataException Convert an instance of the Java type into the Open Type.
| |