Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: 1.2
-
Fix Version/s: 1.3
-
Component/s: modello-plugin-java
-
Labels:None
-
Complexity:Intermediate
-
Number of attachments :
Description
Generated code below does not handle null values properly, "mother instanceof Person" return false if mother==null, so the code throws ClassCastException when it should not. I'll commit the fix as soon as I remember my svn password ![]()
/**
* Set the mother field.
*
* @param mother
*/
public void setMother( IPerson mother )
{
if ( !( mother instanceof Person ) )
{
throw new ClassCastException( "Location.setMother( mother ) parameter must be instanceof " + Person.class.getName() );
}
this.mother = (Person) mother;
} //-- void setMother( IPerson )
Added explicit null check to prevent generated class cast assertion from throwing ClassCastException when new field value is null.
Note that this change affects multi-value associations, while it was not possible to add/remove null value to such fields due to ClassCastException before, it is now up to underlying collection to allow/disallow null values (see generated code below). I am not sure if we should or should not allow null values in this case, but I believe ClassCastException was not appropriate regardless.
/** * Method addRelative. * * @param person */ public void addRelative( IPerson person ) { if ( person != null && !( person instanceof Person ) ) { throw new ClassCastException( "Location.addRelatives( person ) parameter must be instanceof " + Person.class.getName() ); } getRelatives().add( ( (Person) person ) ); } //-- void addRelative( IPerson ) /** * Method removeRelative. * * @param person */ public void removeRelative( IPerson person ) { if ( person != null && !( person instanceof Person ) ) { throw new ClassCastException( "Location.removeRelatives( person ) parameter must be instanceof " + Person.class.getName() ); } getRelatives().remove( ( (Person) person ) ); } //-- void removeRelative( IPerson )