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 {@link #fromOpenValue fromOpenValue} and {@link #toOpenValue toOpenValue}methods, and optionally the {@link #checkReconstructible} method.Then either use an {@link MXBeanMappingClass} annotation on your customJava types, or include this MXBeanMapping in an {@link MXBeanMappingFactory}.
For example, suppose we have a class {@code 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 {@code getNext()}method. MXBeans do not support recursive types. So we would like to specify a mapping for {@code MyLinkedList} explicitly. When anMXBean interface contains {@code MyLinkedList}, that will be mapped into a {@code String[]}, which is a valid Open Type.
To define this mapping, we first subclass {@code 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"); } {@literal @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; } {@literal @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 ( {@code MyLinkedList.class}) and what Open Type it is mapped to ( {@code ArrayType.getArrayType(SimpleType.STRING)}). The {@code fromOpenValue} method says how we go from the Open Type ({@code String[]}) to the Java type ( {@code MyLinkedList}), and the {@code toOpenValue} method says how we go from the Java type to the OpenType.
With this mapping defined, we can annotate the {@code MyLinkedList}class appropriately:
{@literal @MXBeanMappingClass}(MyLinkedListMapping.class) public class MyLinkedList {...}
Now we can use {@code MyLinkedList} in an MXBean interface and itwill work.
If we are unable to modify the {@code MyLinkedList} class,we can define an {@link MXBeanMappingFactory}. See the documentation of that class for further details.
@see MXBean specification, section "Custom MXBean type mappings"
|
|
|
|