getSetCollection = CollectionHandlers.isGetSetCollection( colType );
if ( colType == Object[].class ) {
if (fieldType == null) {
String error = "'type' is a required attribute for " +
"field that are array collections: " + fieldName;
throw new MappingException(error);
}
Object obj = Array.newInstance(fieldType, 0);
colType = obj.getClass();
}
}
// If get/set methods not specified, use field names to determine them.
if ( fieldMap.getDirect() ) {
// No accessor, map field directly.
Field field;
field = findField( javaClass, fieldName, ( colType == null ? fieldType : colType ) );
if ( field == null )
throw new MappingException( "mapping.fieldNotAccessible", fieldName, javaClass.getName() );
if ( fieldType == null )
fieldType = field.getType();
typeInfoRef.typeInfo = getTypeInfo(fieldType, colHandler, fieldMap);
handler = new FieldHandlerImpl( field, typeInfoRef.typeInfo );
}
else {
//-- if both methods (get/set) are not specified, then
//-- automatically determine them.
if ( fieldMap.getGetMethod() == null && fieldMap.getSetMethod() == null ) {
int point;
Vector getSeq = new Vector();
Vector setSeq = new Vector();
String methodName;
Method method;
if ( fieldName == null )
throw new MappingException( "mapping.missingFieldName", javaClass.getName() );
//-- get method normally starts with "get", but
//-- may start with "is" if it's a boolean.
String getPrefix = GET_METHOD_PREFIX;
try {
//-- handle nested fields
while ( true ) {
Class last;
point = fieldName.indexOf( '.' );
if ( point < 0 )
break;
last = javaClass;
// * getter for parent field *
String parentField = fieldName.substring(0, point);
methodName = GET_METHOD_PREFIX + capitalize( parentField );
method = javaClass.getMethod( methodName, null );
fieldName = fieldName.substring( point + 1 );
// Make sure method is not abstract/static
// (note: Class.getMethod() returns only public methods).
if ( ( method.getModifiers() & Modifier.ABSTRACT ) != 0 ||
( method.getModifiers() & Modifier.STATIC ) != 0 )
throw new MappingException( "mapping.accessorNotAccessible",
methodName, javaClass.getName() );
getSeq.addElement( method );
javaClass = method.getReturnType();
// setter; Note: javaClass already changed, use "last"
methodName = "set" + methodName.substring(getPrefix.length());
try {
method = last.getMethod( methodName, new Class[] { javaClass } );
if ( ( method.getModifiers() & Modifier.ABSTRACT ) != 0 ||
( method.getModifiers() & Modifier.STATIC ) != 0 )
method = null;
} catch ( Exception except ) {
method = null;
}
setSeq.addElement( method );
} //-- end of nested fields
//-- save method-call sequence for nested fields
if ( getSeq.size() > 0 ) {
getSequence = new Method[ getSeq.size() ];
getSeq.copyInto( getSequence );
setSequence = new Method[ setSeq.size() ];
setSeq.copyInto( setSequence );
}
//-- find get-method for actual field
methodName = getPrefix + capitalize( fieldName );
Class returnType = (colType == null) ? fieldType : colType;
getMethod = findAccessor( javaClass, methodName, returnType, true);
//-- If getMethod is null, check for boolean type
//-- method prefix might be "is".
if (getMethod == null) {
if ((fieldType == Boolean.class) ||
(fieldType == Boolean.TYPE))
{
getPrefix = IS_METHOD_PREFIX;
methodName = getPrefix + capitalize( fieldName );
getMethod = findAccessor(javaClass, methodName,
returnType, true);
}
}
} catch ( MappingException except ) {
throw except;
} catch ( Exception except ) {
// log.warn ("Unexpected exception", except);
}
if ( getMethod == null )
throw new MappingException( "mapping.accessorNotFound",
getPrefix + capitalize( fieldName ),
( colType == null ? fieldType : colType ),
javaClass.getName() );
if ( fieldType == null && colType == null )
fieldType = getMethod.getReturnType();
// We try to locate a set method anyway and we complain only if we really need one.
setMethod = findAccessor( javaClass, "set" + capitalize( fieldName ),
( colType == null ? fieldType : colType ), false );
// If we have a collection that need both set and get and that
// we don't have a set method, we fail
if ( setMethod == null && colType != null && getSetCollection )
throw new MappingException( "mapping.accessorNotFound",
"set" + capitalize( fieldName ),
( colType == null ? fieldType : colType ),
javaClass.getName() );
} else {
// First look up the get accessors
if ( fieldMap.getGetMethod() != null ) {
getMethod = findAccessor( javaClass, fieldMap.getGetMethod(),
( colType == null ? fieldType : colType ), true );
if ( getMethod == null )
throw new MappingException( "mapping.accessorNotFound",
fieldMap.getGetMethod(), ( colType == null ? fieldType : colType ),
javaClass.getName() );
if ( fieldType == null && colType == null )
fieldType = getMethod.getReturnType();
}
// Second look up the set/add accessor
if ( fieldMap.getSetMethod() != null ) {
String methodName = fieldMap.getSetMethod();
Class type = fieldType;
if (colType != null) {
if (!methodName.startsWith(ADD_METHOD_PREFIX))
type = colType;
}
//-- set via constructor?
if (methodName.startsWith("%")) {
//-- validate index value
String sIdx = methodName.substring(1);
int index = 0;
try {
index = Integer.parseInt(sIdx);
}
catch(NumberFormatException nfe) {
throw new MappingException("mapping.invalidParameterIndex", sIdx);
}
if ((index < 1) || (index > 9)) {
throw new MappingException("mapping.invalidParameterIndex", sIdx);
}
}
else {
setMethod = findAccessor( javaClass, fieldMap.getSetMethod(),
type , false );
if ( setMethod == null )
throw new MappingException( "mapping.accessorNotFound",
fieldMap.getSetMethod(), type,
javaClass.getName() );
if ( fieldType == null )
fieldType = setMethod.getParameterTypes()[ 0 ];
}
}
}
typeInfoRef.typeInfo = getTypeInfo( fieldType, colHandler, fieldMap );
fieldName = fieldMap.getName(); // Not the same for nested fields
if ( fieldName == null )
fieldName = ( getMethod == null ? setMethod.getName() : getMethod.getName() );
//-- create handler
handler = new FieldHandlerImpl( fieldName,
getSequence,
setSequence,
getMethod,
setMethod,
typeInfoRef.typeInfo );
if ((setMethod != null) && (setMethod.getName().startsWith(ADD_METHOD_PREFIX)))
handler.setAddMethod(setMethod);
}
// If there is a create method, add it to the field handler
if ( fieldMap.getCreateMethod() != null ) {
try {
Method method;
method = javaClass.getMethod( fieldMap.getCreateMethod(), null );
handler.setCreateMethod( method );
} catch ( Exception except ) {
// No such/access to method
throw new MappingException( "mapping.createMethodNotFound",
fieldMap.getCreateMethod(), javaClass.getName() );
}
} else if ( fieldName != null && ! Types.isSimpleType( fieldType ) ) {
try {
Method method;