throws MappingException
{
if (cls == null)
throw new MappingException("Cannot introspect a null class.");
ClassMapping clsMap;
Method[] methods;
if ( _mappings.get( cls ) != null )
return;
if ( cls.isArray() ) {
Class cType = cls.getComponentType();
if ( _mappings.get(cType) != null) return;
if (Types.isSimpleType(cType)) return;
//-- handle component type
addClass( cType );
}
if ( _forceIntrospection && (!Types.isConstructable( cls )))
throw new MappingException( "mapping.classNotConstructable", cls.getName() );
XMLClassDescriptor xmlClass;
FieldDescriptor[] fields;
ClassMapping classMap;
FieldMapping fieldMap;
boolean introspected = false;
try {
if (_forceIntrospection) {
xmlClass = _introspector.generateClassDescriptor( cls );
introspected = true;
}
else {
xmlClass = _resolver.resolve( cls );
introspected = _introspector.introspected(xmlClass);
}
}
catch ( MarshalException 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(CollectionType.ARRAY);
}
else {
//-- if the fieldType is the collection, then set appropriate
//-- collection type
String colName = CollectionHandlers.getCollectionName(fieldType);
if (colName != null) {
fieldMap.setCollection(CollectionType.valueOf(colName));
fieldType = Object.class;
}
//-- help maintain compatibility with generated
//-- descriptors
else if (_mappingLoader.returnsArray(cls, fieldName, fieldType)) {
fieldMap.setCollection( CollectionType.ARRAY );
}
else {
fieldMap.setCollection( CollectionType.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() ) );
classMap.addFieldMapping( fieldMap );
if (deep) {
if ( _mappings.get(fieldType) != null) continue;
if (Types.isSimpleType(fieldType)) continue;
//-- recursive add needed classes