if (COLLECTION_TYPE_ARRAY.equals(fieldMap.getCollection().toString())) {
String arrayClassName = "[L" + fieldMap.getType() + ";";
try {
_colClass = ds.resolve(arrayClassName);
} catch (ClassNotFoundException e) {
throw new MappingException("mapping.classNotFound",
arrayClassName);
}
} else {
_colClass = getCollectionType(fieldMap.getCollection().toString(),
_lazy);
}
_store = false;
}
// Set field name, if it is null, we try to discover it with
// return type of set/get method.
_fType = fieldMap.getType();
Class javaClass;
try {
javaClass = ds.resolve(eMold.getName());
} catch (ClassNotFoundException e) {
throw new MappingException( "mapping.classNotFound", eMold.getName() );
}
// ssa, multi classloader feature
// set the default classloader to the hash table
// ssa, FIXME : Shoudln't we have a ref to the Classloader used
// instead of asking it to the newly created class ?
_defaultReflectService._loader = javaClass.getClassLoader();
if ( null != _defaultReflectService._loader )
_reflectServices.put (_defaultReflectService._loader, this._defaultReflectService);
String fieldName = fieldMap.getName();
String fieldType = fieldMap.getType();
Class declaredClass = null;
if ( fieldType != null ) {
try {
declaredClass = Types.typeFromName( javaClass.getClassLoader(), fieldType );
_defaultReflectService._fClass = declaredClass;
} catch ( ClassNotFoundException cnfe ) {
throw new MappingException( "mapping.classNotFound", declaredClass );
}
}
if ( fieldMap.getDirect() ) {
// No accessor, map field directly.
Class fieldClass = _colClass!=null? _colClass : null;
_defaultReflectService._field = findField( javaClass, fieldName, fieldClass );
if ( _defaultReflectService._field == null )
throw new MappingException( Messages.format("mapping.fieldNotAccessible", fieldName, javaClass.getName()) );
_defaultReflectService._fClass = _defaultReflectService._field.getType();
if ( _defaultReflectService._field.getModifiers() != Modifier.PUBLIC &&
_defaultReflectService._field.getModifiers() != ( Modifier.PUBLIC | Modifier.VOLATILE ) )
throw new MappingException( Messages.format("mapping.fieldNotAccessible", _defaultReflectService._field.getName(),
_defaultReflectService._field.getDeclaringClass().getName()) );
} else if ( fieldMap.getGetMethod() == null && fieldMap.getSetMethod() == null ) {
// Container object, map field to fields of the container
int point;
ArrayList getSeq = new ArrayList();
ArrayList setSeq = new ArrayList();
String name = fieldMap.getName();
Class last;
Method method = null;
String methodName = null;
try {
while ( true ) {
point = name.indexOf( '.' );
if ( point < 0 )
break;
last = javaClass;
if (fieldMap.getType().compareTo("boolean") == 0){
try{
methodName = METHOD_IS_PREFIX + capitalize( name.substring( 0, point ) );
method = javaClass.getMethod( methodName, null );
}
catch (NoSuchMethodException nsme) {
_log.debug (Messages.format("mapping.accessorNotFound", methodName, "boolean", getName()));
}
}
if (method == null) {
methodName = METHOD_GET_PREFIX + capitalize( name.substring(0, point) );
method = javaClass.getMethod( methodName, null );
}
name = name.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.add( method );
javaClass = method.getReturnType();
// setter; Note: javaClass already changed, use "last"
if (fieldMap.getType().compareTo("boolean") == 0) {
methodName = METHOD_SET_PREFIX + methodName.substring(2);
} else {
methodName = METHOD_SET_PREFIX + methodName.substring(3);
}
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.add( method );
method = null;
}
}
catch (Exception ex) {
throw new MappingException(Messages.format ( "mapping.accessorNotFound",
methodName, null, javaClass.getName() ), ex);
}
if ( getSeq.size() > 0 ) {
_defaultReflectService._getSequence = (Method[]) getSeq.toArray( new Method[ 0 ] );
_defaultReflectService._setSequence = (Method[]) setSeq.toArray( new Method[ 0 ] );
}
Class methodClass = _colClass!=null? _colClass: null;
_defaultReflectService._getMethod = null;
// if field is of type boolean, check whether is<Field>() is defined.
if(fieldMap.getType().compareTo("boolean") == 0) {
_defaultReflectService._getMethod = findAccessor( javaClass, METHOD_IS_PREFIX + capitalize( name ), methodClass, true );
}
if( _defaultReflectService._getMethod == null ) {
_defaultReflectService._getMethod = findAccessor( javaClass, METHOD_GET_PREFIX + capitalize( name ), methodClass, true );
}
if ( _defaultReflectService._getMethod == null ){
if ( fieldMap.getType().compareTo("boolean") == 0){
throw new MappingException( "mapping.accessorNotFound",
METHOD_GET_PREFIX + "/" + METHOD_IS_PREFIX + capitalize( name ), fieldMap.getType(), eMold.getName() );
} else {
throw new MappingException( "mapping.accessorNotFound",
METHOD_GET_PREFIX + capitalize( name ), fieldMap.getType(), eMold.getName() );
}
}
// update fClass, because we can't tell between primitive
// and primitive wrapper from the mapping
if ( _colClass == null )
_defaultReflectService._fClass = _defaultReflectService._getMethod.getReturnType();
_defaultReflectService._setMethod = findAccessor( javaClass, METHOD_SET_PREFIX + capitalize( name ), methodClass, false );
if ( _defaultReflectService._setMethod == null ) {
_defaultReflectService._addMethod
= findAccessor( javaClass, METHOD_ADD_PREFIX + capitalize( name ), declaredClass, false );
// look again, but this time without a trailing 's'
if ( _defaultReflectService._addMethod == null && name.endsWith("s") )
_defaultReflectService._addMethod
= findAccessor( javaClass, METHOD_ADD_PREFIX + capitalize( name ).substring(0,name.length()-1), declaredClass, false );
// if add<FieldName>() has been found, set _addable to true
if ( _defaultReflectService._addMethod != null )
_addable = true;
}
if ( _defaultReflectService._setMethod == null && _defaultReflectService._addMethod == null )
throw new MappingException( "mapping.accessorNotFound",
METHOD_SET_PREFIX + "/" + METHOD_ADD_PREFIX + capitalize( name ), declaredClass, javaClass.getName() );
} else {
// Bean type object, map field to get<Method>/set<Method>
Class methodClass = _defaultReflectService._fClass;
// First look up the get accessors
if ( fieldMap.getGetMethod() != null ) {
if ( _colClass != null ) {
_defaultReflectService._getMethod
= findAccessor( javaClass, fieldMap.getGetMethod(), _colClass, true );
} else {
_defaultReflectService._getMethod
= findAccessor( javaClass, fieldMap.getGetMethod(), methodClass, true );
}
if ( _defaultReflectService._getMethod == null )
throw new MappingException( "mapping.accessorNotFound",
fieldMap.getGetMethod(), methodClass, javaClass.getName() );
// set/reset the fClass to actual field class
if ( _colClass == null )
_defaultReflectService._fClass = _defaultReflectService._getMethod.getReturnType();
} else {
throw new MappingException( "mapping.getMethodMappingNotFound",
_colClass!=null?_colClass:methodClass, javaClass.getName() );
}
// Second look up the set/add accessor
if ( fieldMap.getSetMethod() != null) {
if ( _colClass != null ) {
_defaultReflectService._setMethod = findAccessor( javaClass, fieldMap.getSetMethod(), _colClass, false );
// find addXXX method only if lazy loading is turned off
if ( _defaultReflectService._setMethod == null && !fieldMap.getLazy() ) {
_defaultReflectService._addMethod =
findAccessor( javaClass, fieldMap.getSetMethod(), methodClass, false );
if ( _defaultReflectService._addMethod != null) _addable = true;
}
} else {
// find setXXX method
_defaultReflectService._setMethod = findAccessor( javaClass, fieldMap.getSetMethod(), methodClass, false );
}
if (_defaultReflectService._setMethod == null &&
_defaultReflectService._addMethod == null)
throw new MappingException("mapping.accessorNotFound",
fieldMap.getSetMethod(), methodClass, javaClass
.getName());
if ( _defaultReflectService._fClass == null )
_defaultReflectService._fClass = _defaultReflectService._setMethod.getParameterTypes()[ 0 ];
} else {
throw new MappingException( "mapping.setMethodMappingNotFound",
_colClass!=null?_colClass:methodClass, javaClass.getName() );
}
}
// If there is a create method, add it to the field handler
// Note: create method is used for enclosing object of this field to determine
// what exact instance to be created.
if ( fieldMap.getCreateMethod() != null ) {
try {
_defaultReflectService._createMethod = javaClass.getMethod( fieldMap.getCreateMethod(), null );
} catch ( Exception except ) {
// No such/access to method
throw new MappingException( "mapping.createMethodNotFound",
fieldMap.getCreateMethod(), javaClass.getName() );
}
} else if ( fieldMap.getName() != null && ! Types.isSimpleType( _defaultReflectService._fClass ) ) {
try {
Method method;
method = javaClass.getMethod( METHOD_CREATE_PREFIX + capitalize( fieldMap.getName() ), null );
_defaultReflectService._createMethod = method;
} catch ( Exception except ) {
// no explicit exception handling
}
}
// If there is an has/delete method, add them to field handler
if ( fieldMap.getName() != null ) {
Method hasMethod = null;
Method deleteMethod = null;
try {
hasMethod = javaClass.getMethod( METHOD_HAS_PREFIX + capitalize( fieldMap.getName() ), null );
if ( ( hasMethod.getModifiers() & Modifier.PUBLIC ) == 0 ||
( hasMethod.getModifiers() & Modifier.STATIC ) != 0 )
hasMethod = null;
try {
if ( ( hasMethod.getModifiers() & Modifier.PUBLIC ) == 0 ||
( hasMethod.getModifiers() & Modifier.STATIC ) != 0 )
deleteMethod = null;
deleteMethod = javaClass.getMethod( METHOD_DELETE_PREFIX + capitalize( fieldMap.getName() ), null );
} catch ( Exception except ) {
// no explicit exception handling
}
_defaultReflectService._hasMethod = hasMethod;
_defaultReflectService._deleteMethod = deleteMethod;
} catch ( Exception except ) {
// no explicit exception handling
}
}
if ( _defaultReflectService._field == null && _defaultReflectService._setMethod == null && _defaultReflectService._getMethod == null ) {
throw new MappingException( "_field or _setMethod can't be created" );
}
ds.pairFieldClass( this, _fType );
} catch ( NullPointerException e ) {
throw new MappingException("Unexpected Null pointer!\n"+e);
}
_fieldName = fieldMap.getName();
// If the field is of a primitive type we use the default value
_default = Types.getDefault( _defaultReflectService._fClass );