make cast more safe by first checking with instanceof.
Authored by: mrieser 2009-04-27
Parent: [r6573]
Child: [r6575]