New MetaType annotations and changes
adrian.brock Feb 22, 2008 5:05 AMWhile doing the ManagedObjects for the classloading metadata
I've added a few "features" to the MetaType handling.
1) ManagedObject should be inheritable
When you have a base class handling metadata for different use cases
and versions its a pain (and probably error prone) to have to add
@ManagementObject to every one.
So I've made the annotation inheritable.
e.g. all these will pick up that this is a managed object from the base class
@ManagementObject
com.acme.BaseMetaData
com.acme.BaseMetaDataV10 extends BaseMetaData
com.acme.BaseMetaDataV20 extends BaseMetaDataV10
com.acme.UseCase1MetaData extends BaseMetaData
com.acme.UseCase2MetaData extends BaseMetaData
2) Ignoring implementation details
I had a case where I wanted to ignore some properties
when creating CompositeMetaTypes/Values, which
you can now do with an annotation
public class MyClass
{
@CompositeValue(ignore=true)
public void setImplementationDetail();
}
3) Choosing the property type exposed for management
This is really a form of aliasing. Basically you have a generic property
but for management purposes you want to expose an explicit type.
e.g. My use case is:
// Too generic to be useful for management so ignore
@CompositeValue(ignore=true)
public void setVersion(Object version) {}
// This is how we want it exposed (it's really an alias of setVersion)
@CompositeValue(name="version")
public void setTheVersion(Version version) { setVersion(version); )
4) Mapping of types in metatype
I also didn't want to expose the Version as a CompositeType,
e.g. major=1, minor=0, micro=0, qualifier=GA
so like JAXB, I've allowed a mapper to specified using an annotation
e.g. this exposes the Version type as a String "1.0.0.GA"
@MetaMapping(VersionMetaMapper.class) public class Version implements Serializable, Comparable<Version> public class VersionMetaMapper extends MetaMapper<Version> { public Type mapToType() { return String.class; } public MetaValue createMetaValue(MetaType<?> metaType, Version object) { return SimpleValueSupport.wrap(object.toString()); } public Version unwrapMetaValue(MetaValue metaValue) { if (SimpleMetaType.STRING.equals(metaValue.getMetaType()) == false) throw new IllegalArgumentException("Not a string: " + metaValue); SimpleValue<?> simple = (SimpleValue<?>) metaValue; String value = (String) simple.getValue(); return Version.parseVersion(value); }
The mapToType() is an "ease of use" thing. If you need a more complex type
you should override getMetaType() instead.
NOTE: This annotation is checked AFTER the MetaType/ValueBuilder
stuff. So if somebody installs a specific builder for a type
(e.g. using xml configuration), it will override this annotation.
5) Bug fix
Finally, I fixed a bug where the CompositeValue stuff had a couple places
iterating over the keySet() (only the id properties) instead of the itemSet()
(all properties). One of them wasn't really a bug, it was in the toString(), but it confused
me ;-)