+ MappingDefinition.class.getName());
}
try {
final MappingDefinition definition = (MappingDefinition) cls.newInstance();
definition.setMarshallerInstance(new DefaultDefinitionMarshaller(definition));
addDefinition(definition);
if (!envExposedClasses.contains(definition.getMappingClass())) {
definition.setLazy(true);
}
exposedClasses.add(definition.getMappingClass());
if (log.isDebugEnabled())
log.debug("loaded custom mapping class: " + cls.getName() + " (for mapping: "
+ definition.getMappingClass().getFullyQualifiedName() + ")");
if (cls.isAnnotationPresent(InheritedMappings.class)) {
final InheritedMappings inheritedMappings = cls.getAnnotation(InheritedMappings.class);
for (final Class<?> c : inheritedMappings.value()) {
final MetaClass metaClass = MetaClassFactory.get(c);
final MappingDefinition aliasMappingDef = new MappingDefinition(metaClass, definition.alreadyGenerated());
aliasMappingDef.setMarshallerInstance(new DefaultDefinitionMarshaller(aliasMappingDef));
addDefinition(aliasMappingDef);
if (!envExposedClasses.contains(metaClass)) {
aliasMappingDef.setLazy(true);
}
exposedClasses.add(metaClass);
if (log.isDebugEnabled())
log.debug("mapping inherited mapping " + c.getName() + " -> " + cls.getName());
}
}
}
catch (Throwable t) {
throw new RuntimeException("Failed to load definition", t);
}
}
for (final MappingDefinition def : mappingDefinitions.values()) {
mergeDefinition(def);
}
final Collection<MetaClass> cliMarshallers = ClassScanner.getTypesAnnotatedWith(ClientMarshaller.class, true);
final MetaClass Marshaller_MC = MetaClassFactory.get(Marshaller.class);
for (final MetaClass marshallerMetaClass : cliMarshallers) {
if (Marshaller_MC.isAssignableFrom(marshallerMetaClass)) {
final Class<? extends Marshaller> marshallerCls = marshallerMetaClass.asClass().asSubclass(Marshaller.class);
try {
final Class<?> type = marshallerMetaClass.getAnnotation(ClientMarshaller.class).value();
final MappingDefinition marshallMappingDef = new MappingDefinition(type, true);
marshallMappingDef.setClientMarshallerClass(marshallerCls);
addDefinition(marshallMappingDef);
exposedClasses.add(MetaClassFactory.get(type).asBoxed());
if (marshallerCls.isAnnotationPresent(ImplementationAliases.class)) {
for (final Class<?> aliasCls : marshallerCls.getAnnotation(ImplementationAliases.class).value()) {
final MappingDefinition aliasMappingDef = new MappingDefinition(aliasCls, true);
aliasMappingDef.setClientMarshallerClass(marshallerCls.asSubclass(Marshaller.class));
addDefinition(aliasMappingDef);
exposedClasses.add(MetaClassFactory.get(aliasCls).asBoxed());
mappingAliases.put(aliasCls.getName(), type.getName());
}
}
}
catch (Throwable t) {
throw new RuntimeException("could not instantiate marshaller class: " + marshallerCls.getName(), t);
}
}
else {
throw new RuntimeException("class annotated with " + ClientMarshaller.class.getCanonicalName()
+ " does not implement " + Marshaller.class.getName());
}
}
final Set<Class<?>> serverMarshallers = scanner.getTypesAnnotatedWith(ServerMarshaller.class, true);
for (final Class<?> marshallerCls : serverMarshallers) {
if (Marshaller.class.isAssignableFrom(marshallerCls)) {
try {
final Class<?> type = marshallerCls.getAnnotation(ServerMarshaller.class).value();
final MappingDefinition definition;
if (hasDefinition(type)) {
definition = getDefinition(type);
definition.setServerMarshallerClass(marshallerCls.asSubclass(Marshaller.class));
}
else {
definition = new MappingDefinition(type, true);
definition.setServerMarshallerClass(marshallerCls.asSubclass(Marshaller.class));
addDefinition(definition);
exposedClasses.add(MetaClassFactory.get(type).asBoxed());
}
if (marshallerCls.isAnnotationPresent(ImplementationAliases.class)) {
for (final Class<?> aliasCls : marshallerCls.getAnnotation(ImplementationAliases.class).value()) {
if (hasDefinition(aliasCls)) {
getDefinition(aliasCls).setServerMarshallerClass(marshallerCls.asSubclass(Marshaller.class));
}
else {
final MappingDefinition aliasMappingDef = new MappingDefinition(aliasCls, true);
aliasMappingDef.setServerMarshallerClass(marshallerCls.asSubclass(Marshaller.class));
addDefinition(aliasMappingDef);
exposedClasses.add(MetaClassFactory.get(aliasCls));
mappingAliases.put(aliasCls.getName(), type.getName());
}
}
}
}
catch (Throwable t) {
throw new RuntimeException("could not instantiate marshaller class: " + marshallerCls.getName(), t);
}
}
else {
throw new RuntimeException("class annotated with " + ServerMarshaller.class.getCanonicalName()
+ " does not implement " + Marshaller.class.getName());
}
}
exposedClasses.addAll(envExposedClasses);
final Map<String, String> configuredMappingAliases = new HashMap<String, String>();
configuredMappingAliases.putAll(environmentConfig.getMappingAliases());
configuredMappingAliases.putAll(defaultMappingAliases());
mappingAliases.putAll(configuredMappingAliases);
final Map<MetaClass, MetaClass> aliasToMarshaller = new HashMap<MetaClass, MetaClass>();
final List<MetaClass> enums = new ArrayList<MetaClass>();
for (final MetaClass cls : exposedClasses) {
MetaClass mappedClass;
if (cls.isArray()) {
arraySignatures.add(cls);
mappedClass = cls.getOuterComponentType();
}
else {
mappedClass = cls;
}
if (mappedClass.isSynthetic())
continue;
final Portable portable = mappedClass.getAnnotation(Portable.class);
if (portable != null && !portable.aliasOf().equals(Object.class)) {
aliasToMarshaller.put(mappedClass, MetaClassFactory.get(portable.aliasOf()));
}
else if (!hasDefinition(mappedClass)) {
final MappingDefinition def = DefaultJavaDefinitionMapper.map(mappedClass, this);
def.setMarshallerInstance(new DefaultDefinitionMarshaller(def));
addDefinition(def);
for (final Mapping mapping : def.getAllMappings()) {
if (mapping.getType().isEnum()) {
enums.add(mapping.getType());
}
}
}
}
for (final MetaClass enumType : enums) {
if (!hasDefinition(enumType)) {
final MappingDefinition enumDef = DefaultJavaDefinitionMapper
.map(MetaClassFactory.get(enumType.asClass()), this);
enumDef.setMarshallerInstance(new DefaultDefinitionMarshaller(enumDef));
addDefinition(enumDef);
exposedClasses.add(MetaClassFactory.get(enumType.asClass()));
}
}
for (final Map.Entry<String, String> entry : configuredMappingAliases.entrySet()) {
try {
aliasToMarshaller.put(MetaClassFactory.get(entry.getKey()), MetaClassFactory.get(entry.getValue()));
}
catch (Throwable t) {
throw new RuntimeException("error loading mapping alias", t);
}
}
for (final Map.Entry<MetaClass, MetaClass> entry : aliasToMarshaller.entrySet()) {
final MappingDefinition def = getDefinition(entry.getValue());
if (def == null) {
throw new InvalidMappingException("cannot alias type " + entry.getKey().getName()
+ " to " + entry.getValue().getName() + ": the specified alias type does not exist ");
}
final MappingDefinition aliasDef = new MappingDefinition(
def.getMarshallerInstance(), entry.getKey(), false
);
if (def.getMarshallerInstance() instanceof DefaultDefinitionMarshaller) {
aliasDef.setMarshallerInstance(new DefaultDefinitionMarshaller(aliasDef));
}
else {
aliasDef.setClientMarshallerClass(def.getClientMarshallerClass());
aliasDef.setServerMarshallerClass(def.getServerMarshallerClass());
}