throw new MappingException("mapping.classNotConstructable", cls.getName());
}
XMLClassDescriptor xmlClass;
FieldDescriptor[] fields;
ClassMapping classMap;
FieldMapping fieldMap;
boolean introspected = false;
try {
if (_forceIntrospection) {
xmlClass = _internalContext.getIntrospector().generateClassDescriptor(cls);
introspected = true;
} else {
xmlClass = (XMLClassDescriptor) _internalContext.getXMLClassDescriptorResolver().resolve(cls);
introspected = _internalContext.getIntrospector().introspected(xmlClass);
}
} catch (Exception except) {
throw new MappingException(except);
}
classMap = new ClassMapping();
classMap.setName(cls.getName());
classMap.setDescription("Default mapping for class " + cls.getName());
// -- prevent default access from showing up in the mapping
classMap.setAccess(null);
// -- map-to
MapTo mapTo = new MapTo();
mapTo.setXml(xmlClass.getXMLName());
mapTo.setNsUri(xmlClass.getNameSpaceURI());
mapTo.setNsPrefix(xmlClass.getNameSpacePrefix());
classMap.setMapTo(mapTo);
// -- add mapping to hashtable before processing
// -- fields so we can do recursive processing
_mappings.put(cls, classMap);
fields = xmlClass.getFields();
for (int i = 0; i < fields.length; ++i) {
FieldDescriptor fdesc = fields[i];
String fieldName = fdesc.getFieldName();
boolean isContainer = false;
// -- check for collection wrapper
if (introspected && fieldName.startsWith("##container")) {
fdesc = fdesc.getClassDescriptor().getFields()[0];
fieldName = fdesc.getFieldName();
isContainer = true;
}
Class fieldType = fdesc.getFieldType();
// -- check to make sure we can find the accessors...
// -- if we used introspection we don't need to
// -- enter this block...only when descriptors
// -- were generated using the source code generator
// -- or by hand.
if ((!introspected) && fieldName.startsWith(UNDERSCORE)) {
// -- check to see if we need to remove underscore
if (!_mappingLoader.canFindAccessors(cls, fieldName, fieldType)) {
fieldName = fieldName.substring(1);
}
// -- check to see if we need to remove "List" prefix
// -- used by generated source code
if (!_mappingLoader.canFindAccessors(cls, fieldName, fieldType)) {
if (fieldName.endsWith("List")) {
int len = fieldName.length() - 4;
String tmpName = fieldName.substring(0, len);
if (_mappingLoader.canFindAccessors(cls, tmpName, fieldType)) {
fieldName = tmpName;
}
}
}
}
fieldMap = new FieldMapping();
fieldMap.setName(fieldName);
// -- unwrap arrays of objects
boolean isArray = fieldType.isArray();
while (fieldType.isArray()) {
fieldType = fieldType.getComponentType();
}
// -- To prevent outputing of optional fields...check
// -- for value first before setting
if (fdesc.isRequired()) {
fieldMap.setRequired(true);
}
if (fdesc.isTransient()) {
fieldMap.setTransient(true);
}
if (fdesc.isMultivalued()) {
// -- special case for collections
if (isContainer) {
// -- backwards than what you'd expect, but
// -- if the collection had a "container" wrapper
// -- then we specify container="false" in the
// -- mapping file.
fieldMap.setContainer(false);
}
// -- try to guess collection type
if (isArray) {
fieldMap.setCollection(FieldMappingCollectionType.ARRAY);
} else {
// -- if the fieldType is the collection, then set
// appropriate
// -- collection type
String colName = CollectionHandlers.getCollectionName(fieldType);
if (colName != null) {
fieldMap.setCollection(FieldMappingCollectionType.valueOf(colName));
fieldType = Object.class;
} else if (_mappingLoader.returnsArray(cls, fieldName, fieldType)) {
// -- help maintain compatibility with generated
// descriptors
fieldMap.setCollection(FieldMappingCollectionType.ARRAY);
} else {
fieldMap.setCollection(FieldMappingCollectionType.ENUMERATE);
}
}
}
// -- fieldType
fieldMap.setType(fieldType.getName());
// -- handle XML Specific information
fieldMap.setBindXml(new BindXml());
fieldMap.getBindXml().setName(((XMLFieldDescriptor) fdesc).getXMLName());
fieldMap.getBindXml().setNode(
BindXmlNodeType.valueOf(((XMLFieldDescriptor) fields[i]).getNodeType()
.toString()));
if (classMap.getClassChoice() == null) {
classMap.setClassChoice(new ClassChoice());
}
classMap.getClassChoice().addFieldMapping(fieldMap);
if (deep) {
if (_mappings.get(fieldType) != null) {
continue;
}